This commit is contained in:
Ash Leece 2021-03-07 03:50:52 +00:00
commit f913ab2a7e
10 changed files with 355 additions and 0 deletions

5
.dockerignore Normal file
View File

@ -0,0 +1,5 @@
*
!*.go
!*.mod
!*.sum
.vscode

1
.env Normal file
View File

@ -0,0 +1 @@
DISCORD_WEBHOOK=

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
*
!*.go
!*.mod
!*.sum
!Dockerfile
!*.yml
!\.*
.env
.vscode

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM golang:latest as builder
RUN mkdir /build
ADD . /build/
WORKDIR /build
ENV CGO_ENABLED=0
RUN go build -ldflags="-s -w" -o main .
FROM alpine:latest
WORKDIR /app
COPY --from=builder /build/main /app/
ENTRYPOINT [ "./main" ]

95
discord.go Normal file
View File

@ -0,0 +1,95 @@
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"`
}

11
docker-compose.yml Normal file
View File

@ -0,0 +1,11 @@
version: '3.1'
services:
discord-plex_bridge:
build: .
container_name: discord-plex_bridge
environment:
- DISCORD_WEBHOOK=${DISCORD_WEBHOOK}
ports:
- 8080:8080
restart: unless-stopped

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module plex-webhook
go 1.16
require github.com/gin-gonic/gin v1.6.3

47
go.sum Normal file
View File

@ -0,0 +1,47 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

21
main.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"log"
"os"
"github.com/gin-gonic/gin"
)
var e *gin.Engine
func main() {
e := gin.Default()
gin.SetMode(gin.ReleaseMode)
log.Println("Discord webhook:", os.Getenv("DISCORD_WEBHOOK"))
e.POST("/plex", recv)
e.Run(":8080")
}

150
plex.go Normal file
View File

@ -0,0 +1,150 @@
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"regexp"
"github.com/gin-gonic/gin"
)
func recv(c *gin.Context) {
c.Request.Header.Set("Content-Type", "application/json")
log.Println(c.Request.Header.Get("Content-Type"))
txt, _ := ioutil.ReadAll(c.Request.Body)
plain := string(txt)
regex := regexp.MustCompile(`{.*`)
js := regex.FindStringSubmatch(plain)
var p Plex
json.Unmarshal([]byte(js[0]), &p)
postToDiscord(p)
c.String(http.StatusOK, "ok")
}
func getEvent(e string) string {
switch e {
case "media.play":
return "started playing"
case "media.resume":
return "resumed playing"
case "media.stop":
return "stopped playing"
case "media.pause":
return "paused"
case "media.rate":
return "rated"
}
return e
}
type Plex struct {
Event string `json:"event"`
User bool `json:"user"`
Owner bool `json:"owner"`
Account Account `json:"Account"`
Server Server `json:"Server"`
Player Player `json:"Player"`
Metadata Metadata `json:"Metadata"`
}
type Account struct {
ID int `json:"id"`
Thumb string `json:"thumb"`
Title string `json:"title"`
}
type Server struct {
Title string `json:"title"`
UUID string `json:"uuid"`
}
type Player struct {
Local bool `json:"local"`
PublicAddress string `json:"publicAddress"`
Title string `json:"title"`
UUID string `json:"uuid"`
}
type Genre struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
Count int `json:"count"`
}
type Director struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
}
type Writer struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
Count int `json:"count,omitempty"`
}
type Producer struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
Count int `json:"count"`
}
type Country struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
Count int `json:"count"`
}
type Role struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
Count int `json:"count,omitempty"`
Role string `json:"role"`
Thumb string `json:"thumb,omitempty"`
}
type Similar struct {
ID int `json:"id"`
Filter string `json:"filter"`
Tag string `json:"tag"`
Count int `json:"count"`
}
type Metadata struct {
LibrarySectionType string `json:"librarySectionType"`
RatingKey string `json:"ratingKey"`
Key string `json:"key"`
GUID string `json:"guid"`
Studio string `json:"studio"`
Type string `json:"type"`
Title string `json:"title"`
LibrarySectionTitle string `json:"librarySectionTitle"`
LibrarySectionID int `json:"librarySectionID"`
LibrarySectionKey string `json:"librarySectionKey"`
ContentRating string `json:"contentRating"`
Summary string `json:"summary"`
Rating float64 `json:"rating"`
AudienceRating float64 `json:"audienceRating"`
ViewOffset int `json:"viewOffset"`
LastViewedAt int `json:"lastViewedAt"`
Year int `json:"year"`
Tagline string `json:"tagline"`
Thumb string `json:"thumb"`
Art string `json:"art"`
Duration int `json:"duration"`
OriginallyAvailableAt string `json:"originallyAvailableAt"`
AddedAt int `json:"addedAt"`
UpdatedAt int `json:"updatedAt"`
AudienceRatingImage string `json:"audienceRatingImage"`
PrimaryExtraKey string `json:"primaryExtraKey"`
RatingImage string `json:"ratingImage"`
Genre []Genre `json:"Genre"`
Director []Director `json:"Director"`
Writer []Writer `json:"Writer"`
Producer []Producer `json:"Producer"`
Country []Country `json:"Country"`
Role []Role `json:"Role"`
Similar []Similar `json:"Similar"`
}