96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func postToDiscord(p Plex) {
|
|
var event string
|
|
|
|
event = getEvent(p.Event)
|
|
|
|
var d Discord
|
|
d.Username = "Plex"
|
|
d.AvatarURL = "https://www.plex.tv/wp-content/uploads/2018/02/pms-icon-800x800.png"
|
|
d.Content = p.Account.Title + " has " + event + " " + p.Metadata.Title
|
|
|
|
var e Embeds
|
|
|
|
e.Author.Name = "Plex"
|
|
e.Author.URL = "https://app.plex.tv"
|
|
e.Author.IconURL = p.Account.Thumb
|
|
|
|
e.Title = p.Metadata.Title + " - " + strconv.Itoa(p.Metadata.Year)
|
|
e.URL = "https://app.plex.tv"
|
|
e.Description = p.Metadata.Summary
|
|
e.Image.URL = p.Account.Thumb
|
|
e.Color = 16557056
|
|
|
|
d.Embeds = append(d.Embeds, e)
|
|
|
|
m, _ := json.Marshal(d)
|
|
|
|
if strings.Contains(p.Event, "media") {
|
|
req, err := http.NewRequest("POST", os.Getenv("DISCORD_WEBHOOK"), bytes.NewReader(m))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{Timeout: time.Second * 10}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
log.Println(resp.Status)
|
|
} else {
|
|
log.Println("Not a media event")
|
|
}
|
|
|
|
}
|
|
|
|
type Discord struct {
|
|
Username string `json:"username"`
|
|
AvatarURL string `json:"avatar_url"`
|
|
Content string `json:"content"`
|
|
Embeds []Embeds `json:"embeds"`
|
|
}
|
|
type Author struct {
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
IconURL string `json:"icon_url"`
|
|
}
|
|
type Fields struct {
|
|
Name string `json:"name"`
|
|
Value string `json:"value"`
|
|
Inline bool `json:"inline,omitempty"`
|
|
}
|
|
type Thumbnail struct {
|
|
URL string `json:"url"`
|
|
}
|
|
type Image struct {
|
|
URL string `json:"url"`
|
|
}
|
|
type Footer struct {
|
|
Text string `json:"text"`
|
|
IconURL string `json:"icon_url"`
|
|
}
|
|
type Embeds struct {
|
|
Author Author `json:"author"`
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
Description string `json:"description"`
|
|
Color int `json:"color"`
|
|
Fields []Fields `json:"fields"`
|
|
Thumbnail Thumbnail `json:"thumbnail"`
|
|
Image Image `json:"image"`
|
|
Footer Footer `json:"footer"`
|
|
}
|