This repository has been archived by the owner on Jun 2, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (111 loc) · 2.69 KB
/
main.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
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
package main // import "github.com/ykzts/tea-crawler"
import (
"context"
"flag"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
var (
target = flag.String("target", "", "")
)
func search(service *youtube.Service, channelID string, pageToken string) (*youtube.SearchListResponse, error) {
searchCall := service.Search.List("id,snippet").
ChannelId(channelID).
MaxResults(50).
Order("date").
PageToken(pageToken).
SafeSearch("none").
Type("video")
searchResponse, err := searchCall.Do()
if err != nil {
return nil, err
}
return searchResponse, nil
}
func normalize(p string) string {
s := strings.Replace(p, "?", "", -1)
s = strings.Replace(s, "\"", "'", -1)
s = strings.Replace(s, ":", " -", -1)
s = strings.Replace(s, "\\", "_", -1)
s = strings.Replace(s, "/", "_", -1)
s = strings.Replace(s, "|", "_", -1)
s = strings.Replace(s, "*", "_", -1)
s = strings.Replace(s, "<", "_", -1)
s = strings.Replace(s, ">", "_", -1)
s = strings.Replace(s, "|", "_", -1)
s = strings.Replace(s, "|", "_", -1)
s = strings.Replace(s, "|", "_", -1)
s = strings.Replace(s, "|", "_", -1)
return s
}
func download(url string, name string) (string, error) {
p := normalize(name)
f, err := os.Create(p + filepath.Ext(url))
if err != nil {
return "", err
}
defer f.Close()
response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
_, err = io.Copy(f, response.Body)
if err != nil {
return "", err
}
return name, nil
}
func crawl(service *youtube.Service, channelID string) {
nextPageToken := ""
for {
response, err := search(service, channelID, nextPageToken)
if err != nil {
log.Printf("Error: %v", err)
continue
}
for _, item := range response.Items {
t := item.Snippet.Thumbnails.High.Url
t = strings.TrimSuffix(t, "hqdefault.jpg")
t += "maxresdefault.jpg"
p, err := time.Parse(time.RFC3339, item.Snippet.PublishedAt)
if err != nil {
log.Printf("Error: %v", err)
continue
}
f := p.Format("20060102") + "-" + item.Id.VideoId + "-" + item.Snippet.Title
_, err = download(t, f)
if err != nil {
log.Printf("Error: %v", err)
}
}
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
}
func main() {
flag.Parse()
ctx := context.Background()
apiKey := os.Getenv("YOUTUBE_API_KEY")
if apiKey == "" {
log.Fatal("error: API key is required")
}
channelID := *target
if channelID == "" {
log.Fatal("error: Channel ID is required")
}
service, err := youtube.NewService(ctx, option.WithAPIKey(apiKey))
if err != nil {
log.Fatalf("error: %v", err)
}
crawl(service, channelID)
}