Skip to content

Commit

Permalink
Add "age restricted" label to age restricted YouTube videos (#251)
Browse files Browse the repository at this point in the history
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
  • Loading branch information
zneix and pajlada authored Dec 19, 2021
1 parent 44e0792 commit 3a090cb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- YouTube: Added a red `AGE RESTRICTED` label to the YouTube video tooltip. (#251)
- YouTube: Removed dislike count from rich tooltips since YouTube removed it. (#243)
- Twitter: Blacklist special pages from being resolved as user pages. (#220)
- Twitch: Handle Twitch clips from `m.twitch.tv` domain. (#239)
Expand Down
21 changes: 15 additions & 6 deletions internal/resolvers/youtube/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ func loadVideos(videoID string, r *http.Request) (interface{}, time.Duration, er
return &resolver.Response{Status: 500, Message: "video unavailable"}, cache.NoSpecialDur, nil
}

// Check if a video is age resricted: https://stackoverflow.com/a/33750307
var ageRestricted = false
if video.ContentDetails.ContentRating != nil {
if video.ContentDetails.ContentRating.YtRating == "ytAgeRestricted" {
ageRestricted = true
}
}

data := youtubeVideoTooltipData{
Title: video.Snippet.Title,
ChannelTitle: video.Snippet.ChannelTitle,
Duration: humanize.DurationPT(video.ContentDetails.Duration),
PublishDate: humanize.CreationDateRFC3339(video.Snippet.PublishedAt),
Views: humanize.Number(video.Statistics.ViewCount),
LikeCount: humanize.Number(video.Statistics.LikeCount),
Title: video.Snippet.Title,
ChannelTitle: video.Snippet.ChannelTitle,
Duration: humanize.DurationPT(video.ContentDetails.Duration),
PublishDate: humanize.CreationDateRFC3339(video.Snippet.PublishedAt),
Views: humanize.Number(video.Statistics.ViewCount),
LikeCount: humanize.Number(video.Statistics.LikeCount),
AgeRestricted: ageRestricted,
}

var tooltip bytes.Buffer
Expand Down
13 changes: 7 additions & 6 deletions internal/resolvers/youtube/model.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package youtube

type youtubeVideoTooltipData struct {
Title string
ChannelTitle string
Duration string
PublishDate string
Views string
LikeCount string
Title string
ChannelTitle string
Duration string
PublishDate string
Views string
LikeCount string
AgeRestricted bool
}

type youtubeChannelTooltipData struct {
Expand Down
1 change: 1 addition & 0 deletions internal/resolvers/youtube/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
<br><b>Duration:</b> {{.Duration}}
<br><b>Published:</b> {{.PublishDate}}
<br><b>Views:</b> {{.Views}}
{{ if .AgeRestricted }}<br><b><span style="color: red;">AGE RESTRICTED</span></b>{{ end }}
<br><span style="color: #2ecc71;">{{.LikeCount}} likes</span>
</div>
`
Expand Down

0 comments on commit 3a090cb

Please sign in to comment.