This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.go
63 lines (52 loc) · 1.59 KB
/
video.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package scraper
import (
"fmt"
"html"
"time"
"google.golang.org/api/youtube/v3"
)
type Video struct {
Channel *Channel `json:"channel"`
Duration string `json:"duration"`
ID string `json:"id"`
ObjectID string `json:"objectID"`
PublishedAt int64 `json:"publishedAt"`
Thumbnail *Thumbnail `json:"thumbnail,omitempty"`
Title string `json:"title"`
URL string `json:"url"`
}
func NewVideo(item *youtube.Video) *Video {
rawPublishedAt := item.Snippet.PublishedAt
if item.LiveStreamingDetails != nil {
if item.LiveStreamingDetails.ActualStartTime != "" {
rawPublishedAt = item.LiveStreamingDetails.ActualStartTime
} else if item.LiveStreamingDetails.ScheduledStartTime != "" {
rawPublishedAt = item.LiveStreamingDetails.ScheduledStartTime
}
}
publishedAt, err := time.Parse(time.RFC3339, rawPublishedAt)
if err != nil {
publishedAt = time.Now()
}
channel := &Channel{
ID: item.Snippet.ChannelId,
Title: item.Snippet.ChannelTitle,
URL: fmt.Sprintf("https://www.youtube.com/channel/%s", item.Snippet.ChannelId),
}
b := fmt.Sprintf("https://i.ytimg.com/vi/%s/", item.Id)
t, err := NewThumbnail(b + "maxresdefault.jpg")
if t == nil {
t, _ = NewThumbnail(b + "hqdefault.jpg")
}
video := &Video{
Channel: channel,
Duration: item.ContentDetails.Duration,
ID: item.Id,
ObjectID: item.Id,
PublishedAt: publishedAt.Unix(),
Thumbnail: t,
Title: html.UnescapeString(item.Snippet.Title),
URL: fmt.Sprintf("https://www.youtube.com/watch?v=%s", item.Id),
}
return video
}