-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (128 loc) · 3.65 KB
/
Copy pathmain.go
File metadata and controls
143 lines (128 loc) · 3.65 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"log"
"strings"
"time"
"github.com/fedir/yrank/youtube"
)
// version is overridden at build time via -ldflags "-X main.version=...".
var version = "dev"
func main() {
cid, pid, topSearch, of, sorting, strategy, from, weightsRaw, outFile, m, minLength, maxLength, minViews, d, localTest, inFile, checkFile := cliParameters()
// -check mode: validate an existing CSV export locally, no API key required.
if checkFile != "" {
if err := checkCSVFile(checkFile); err != nil {
log.Fatalf("check failed: %v", err)
}
return
}
// -in mode: filter an existing CSV export locally, no API key required.
if inFile != "" {
if err := filterCSVFile(inFile, outFile, minViews, minLength, maxLength); err != nil {
log.Fatalf("filter file: %v", err)
}
return
}
c := configuration()
if localTest {
youtube.SetHTTPClient(youtube.NewMockClient("testdata"))
}
if d {
fmt.Printf("API key: %s\n", c.apikey)
}
rankedVideos := fetchVideos(c.apikey, cid, pid, topSearch, m, d)
if from != "" {
fromDate, _ := time.Parse("2006-01-02", from)
rankedVideos = filterFrom(rankedVideos, fromDate)
}
rankedVideos = filterByLength(rankedVideos, minLength, maxLength)
rankedVideos = filterByViews(rankedVideos, minViews)
allStrategies := strategy == "all"
if allStrategies {
youtube.ApplyAllStrategies(rankedVideos)
} else if strategy != "" {
env := envWeights()
cli := parseWeightsFlag(weightsRaw)
weights := youtube.ResolveWeights(strategy, env, cli)
youtube.ApplyStrategy(rankedVideos, strategy, weights)
} else {
youtube.SortBy(rankedVideos, sorting)
}
if m > 0 && m <= len(rankedVideos) {
rankedVideos = rankedVideos[:m]
}
if outFile != "" {
if err := printToFile(outFile, rankedVideos, of, strategy != "", allStrategies); err != nil {
log.Fatalf("write output: %v", err)
}
} else {
print(rankedVideos, of, strategy != "", allStrategies)
}
}
// fetchVideos resolves the chosen input source (channel, playlist or search)
// to its ranked video statistics.
func fetchVideos(apikey, cid, pid, topSearch string, m int, d bool) []youtube.VideoStatistics {
switch {
case cid != "":
if strings.HasPrefix(cid, "@") {
cid = youtube.ResolveHandle(cid, apikey)
}
if d {
fmt.Printf("Channel ID: %s\n", cid)
}
return youtube.ChannelStatistics(cid, apikey, d)
case pid != "":
if d {
fmt.Printf("Playlist ID: %s\n", pid)
}
return youtube.PlaylistStatistics(pid, apikey, "", d)
case topSearch != "":
if d {
fmt.Printf("Search query: %s\n", topSearch)
}
return youtube.SearchStatistics(topSearch, apikey, m, d)
}
return nil
}
// filterByViews keeps videos with at least min views. A min of 0 is a no-op.
func filterByViews(videos []youtube.VideoStatistics, min int) []youtube.VideoStatistics {
if min <= 0 {
return videos
}
out := videos[:0]
for _, v := range videos {
if v.ViewCount >= min {
out = append(out, v)
}
}
return out
}
func filterFrom(videos []youtube.VideoStatistics, from time.Time) []youtube.VideoStatistics {
out := videos[:0]
for _, v := range videos {
if !v.PublishedAt.Before(from) {
out = append(out, v)
}
}
return out
}
// filterByLength keeps videos whose duration (seconds) falls within
// [min, max]. A bound of 0 means "no limit" for that side. Videos with an
// unknown duration (0, e.g. live placeholders) are dropped only when min > 0.
func filterByLength(videos []youtube.VideoStatistics, min, max int) []youtube.VideoStatistics {
if min <= 0 && max <= 0 {
return videos
}
out := videos[:0]
for _, v := range videos {
if min > 0 && v.Duration < min {
continue
}
if max > 0 && v.Duration > max {
continue
}
out = append(out, v)
}
return out
}