-
Notifications
You must be signed in to change notification settings - Fork 3
/
video_statistics.go
61 lines (51 loc) · 1.63 KB
/
video_statistics.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
package youtube
import (
"encoding/json"
"fmt"
"log"
"strconv"
"sync"
"time"
)
func videoStatistics(vid string, title string, publishedAt string, apiKey string, dataChan chan VideoStatistics, wg *sync.WaitGroup, debug bool) {
defer wg.Done()
vs := new(VideoStatistics)
url := "https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" + vid + "&key=" + apiKey
if debug {
fmt.Printf("Video URL: %s\n", url)
}
video := video(url)
for _, item := range video.Items {
vs.Key = item.ID
vs.URL = "https://www.youtube.com/watch?v=" + item.ID
var err error
vs.PublishedAt, err = time.Parse(time.RFC3339, publishedAt)
if err != nil {
panic(err)
}
vs.ViewCount, _ = strconv.Atoi(item.Statistics.ViewCount)
vs.LikeCount, _ = strconv.Atoi(item.Statistics.LikeCount)
vs.DislikeCount, _ = strconv.Atoi(item.Statistics.DislikeCount)
vs.CommentCount, _ = strconv.Atoi(item.Statistics.CommentCount)
vs.TotalReaction = vs.LikeCount + vs.DislikeCount + vs.CommentCount
vs.Title = title
vs.PositiveInterestingness = float64(vs.LikeCount-vs.DislikeCount) / float64(vs.ViewCount)
vs.PositiveNegativeCoefficient = float64(vs.LikeCount) / float64(1+vs.DislikeCount)
vs.TotalInterestingness = float64(vs.LikeCount+vs.DislikeCount+vs.CommentCount) / float64(vs.ViewCount)
vs.GlobalBuzzIndex = vs.ViewCount * (vs.LikeCount + vs.DislikeCount + vs.CommentCount)
}
dataChan <- *vs
}
func video(url string) Video {
resp, _, err := httpRequest(url)
if err != nil {
panic(err)
}
jsonResponse, err := readResp(resp)
if err != nil {
log.Println(err)
}
video := Video{}
json.Unmarshal(jsonResponse, &video)
return video
}