From 50b800971ea56c6e10fa1fe0302d5742dbc0be3b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 16 Apr 2018 16:11:32 +0200 Subject: [PATCH] Improved OpenGraph tags --- layout/layout.pixy | 3 +++ pages/amv/amv.go | 1 + pages/amv/opengraph.go | 42 +++++++++++++++++++++++++++++ pages/soundtrack/opengraph.go | 48 ++++++++++++++++++++++++++++++++++ pages/soundtrack/soundtrack.go | 41 ----------------------------- 5 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 pages/amv/opengraph.go create mode 100644 pages/soundtrack/opengraph.go diff --git a/layout/layout.pixy b/layout/layout.pixy index dbb7bca2..de8f2f98 100644 --- a/layout/layout.pixy +++ b/layout/layout.pixy @@ -18,6 +18,9 @@ component Layout(app *aero.Application, ctx *aero.Context, user *arn.User, openG //- Color of the embed sidebar in Discord.. //- This is equal to link-color in the styles. meta(name="theme-color", content="#d7260f") + + //- Facebook App ID + meta(property="fb:app_id", content="915407915202908") for _, name := range meta meta(name=name, content=openGraph.Meta[name]) diff --git a/pages/amv/amv.go b/pages/amv/amv.go index ec34971b..2da00154 100644 --- a/pages/amv/amv.go +++ b/pages/amv/amv.go @@ -19,5 +19,6 @@ func Get(ctx *aero.Context) string { return ctx.Error(http.StatusNotFound, "AMV not found", err) } + ctx.Data = getOpenGraph(ctx, amv) return ctx.HTML(components.AMVPage(amv, user)) } diff --git a/pages/amv/opengraph.go b/pages/amv/opengraph.go new file mode 100644 index 00000000..9934016e --- /dev/null +++ b/pages/amv/opengraph.go @@ -0,0 +1,42 @@ +package amv + +import ( + "strings" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" +) + +func getOpenGraph(ctx *aero.Context, amv *arn.AMV) *arn.OpenGraph { + openGraph := &arn.OpenGraph{ + Tags: map[string]string{ + "og:title": amv.Title.ByUser(nil) + " (AMV)", + "og:url": "https://" + ctx.App.Config.Domain + amv.Link(), + "og:site_name": ctx.App.Config.Domain, + "og:type": "video.other", + }, + Meta: map[string]string{}, + } + + if amv.MainAnime() != nil { + openGraph.Tags["og:image"] = amv.MainAnime().ImageLink("large") + openGraph.Tags["og:description"] = amv.MainAnime().Title.Canonical + " (" + strings.Join(amv.Tags, ", ") + ")" + } else { + openGraph.Tags["og:description"] = strings.Join(amv.Tags, ", ") + } + + if amv.File != "" { + openGraph.Tags["og:video"] = "https://" + ctx.App.Config.Domain + "/videos/amvs/" + amv.File + openGraph.Tags["og:video:type"] = "video/webm" + openGraph.Tags["og:video:width"] = "640" + openGraph.Tags["og:video:height"] = "360" + + openGraph.Meta["twitter:player"] = openGraph.Tags["og:video"] + openGraph.Meta["twitter:player:width"] = openGraph.Tags["og:video:width"] + openGraph.Meta["twitter:player:height"] = openGraph.Tags["og:video:height"] + openGraph.Meta["twitter:player:stream"] = openGraph.Tags["og:video"] + openGraph.Meta["twitter:player:stream:content_type"] = openGraph.Tags["og:video:type"] + } + + return openGraph +} diff --git a/pages/soundtrack/opengraph.go b/pages/soundtrack/opengraph.go new file mode 100644 index 00000000..7295468a --- /dev/null +++ b/pages/soundtrack/opengraph.go @@ -0,0 +1,48 @@ +package soundtrack + +import ( + "strings" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" +) + +func getOpenGraph(ctx *aero.Context, track *arn.SoundTrack) *arn.OpenGraph { + openGraph := &arn.OpenGraph{ + Tags: map[string]string{ + "og:title": track.Title.ByUser(nil), + "og:url": "https://" + ctx.App.Config.Domain + track.Link(), + "og:site_name": ctx.App.Config.Domain, + "og:type": "music.song", + }, + } + + descriptionTags := []string{} + + for _, tag := range track.Tags { + if strings.HasPrefix(tag, "anime:") { + continue + } + + descriptionTags = append(descriptionTags, tag) + } + + if track.MainAnime() != nil { + openGraph.Tags["og:image"] = track.MainAnime().ImageLink("large") + openGraph.Tags["og:description"] = track.MainAnime().Title.Canonical + " (" + strings.Join(descriptionTags, ", ") + ")" + } + + if track.File != "" { + openGraph.Tags["og:audio"] = "https://" + ctx.App.Config.Domain + "/audio/" + track.File + openGraph.Tags["og:audio:type"] = "audio/vnd.facebook.bridge" + } + + // Set video so that it can be played + youtube := track.MediaByService("Youtube") + + if len(youtube) > 0 { + openGraph.Tags["og:video"] = "https://www.youtube.com/v/" + youtube[0].ServiceID + } + + return openGraph +} diff --git a/pages/soundtrack/soundtrack.go b/pages/soundtrack/soundtrack.go index acfa6076..de1cdc80 100644 --- a/pages/soundtrack/soundtrack.go +++ b/pages/soundtrack/soundtrack.go @@ -2,7 +2,6 @@ package soundtrack import ( "net/http" - "strings" "github.com/aerogo/aero" "github.com/animenotifier/arn" @@ -23,43 +22,3 @@ func Get(ctx *aero.Context) string { ctx.Data = getOpenGraph(ctx, track) return ctx.HTML(components.SoundTrackPage(track, user)) } - -func getOpenGraph(ctx *aero.Context, track *arn.SoundTrack) *arn.OpenGraph { - openGraph := &arn.OpenGraph{ - Tags: map[string]string{ - "og:title": track.Title.ByUser(nil), - "og:url": "https://" + ctx.App.Config.Domain + track.Link(), - "og:site_name": ctx.App.Config.Domain, - "og:type": "music.song", - }, - } - - descriptionTags := []string{} - - for _, tag := range track.Tags { - if strings.HasPrefix(tag, "anime:") { - continue - } - - descriptionTags = append(descriptionTags, tag) - } - - if track.MainAnime() != nil { - openGraph.Tags["og:image"] = track.MainAnime().ImageLink("large") - openGraph.Tags["og:description"] = track.MainAnime().Title.Canonical + " (" + strings.Join(descriptionTags, ", ") + ")" - } - - if track.File != "" { - openGraph.Tags["og:audio"] = "https://" + ctx.App.Config.Domain + "/audio/" + track.File - openGraph.Tags["og:audio:type"] = "audio/vnd.facebook.bridge" - } - - // Set video so that it can be played - youtube := track.MediaByService("Youtube") - - if len(youtube) > 0 { - openGraph.Tags["og:video"] = "https://www.youtube.com/v/" + youtube[0].ServiceID - } - - return openGraph -}