-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.go
177 lines (148 loc) · 4.12 KB
/
youtube.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/recoilme/pudge"
"github.com/spf13/viper"
)
const (
rssURL = "https://www.youtube.com/feeds/videos.xml"
)
type rssFeed struct {
Title string `xml:"title"`
ID string `xml:"id"`
ChannelID string `xml:"channelId"`
Published string `xml:"published"`
Author struct {
Name string `xml:"name"`
URI string `xml:"uri"`
} `xml:"author"`
Entries []rssEntry `xml:"entry"`
}
type rssEntry struct {
ID string `xml:"id"`
Title string `xml:"title"`
Link struct {
Href string `xml:"href,attr"`
} `xml:"link"`
Author struct {
Name string `xml:"name"`
URI string `xml:"uri"`
} `xml:"author"`
Group struct {
Title string `xml:"title"`
Content string `xml:"content"`
Thumbnail string `xml:"thumbnail"`
Description string `xml:"description"`
} `xml:"group"`
}
func parseRSSFeed(tubeName string, tubeConfig *viper.Viper) {
channels := tubeConfig.GetStringSlice("channels")
tubeDataPath := fmt.Sprintf("./data/%s", tubeName)
tubeDB, err := pudge.Open(tubeDataPath, nil)
if err != nil {
log.Println(err)
return
}
defer tubeDB.Close()
log.Printf("(%s) Start RSS parsing... %d channels", tubeName, len(channels))
posted := 0
for _, id := range channels {
isChanExists, err := tubeDB.Has(id)
if err != nil {
log.Println(err)
continue
}
if !isChanExists {
log.Printf("(%s) Detect new channel: %s\n", tubeName, id)
if err := tubeDB.Set(id, true); err != nil {
log.Println(err)
continue
}
}
endpoint := fmt.Sprintf("%s?channel_id=%s", rssURL, id)
request, err := newRequest("GET", endpoint, nil)
if err != nil {
log.Println(err)
continue
}
client, err := newClient()
if err != nil {
log.Println(err)
continue
}
response, err := client.Do(request)
if err != nil {
log.Println(fmt.Sprintf("Read RSS feed ERROR: channel ID: %s", id), err.Error())
continue
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
err := errors.New(fmt.Sprintf("Read RSS feed ERROR: Unexpected status code %d, channel ID: %s", response.StatusCode, id))
log.Println(err)
continue
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Println("Read RSS body ERROR:", err.Error())
continue
}
var feed rssFeed
xml.Unmarshal(body, &feed)
for i := range feed.Entries {
entry := feed.Entries[len(feed.Entries)-1-i]
exists, err := tubeDB.Has(entry.ID)
if err != nil {
log.Println("DB check record ERROR:", err.Error())
continue
}
if !exists {
if config().isSkipMode() || !isChanExists {
tubeDB.Set(entry.ID, time.Now().Unix())
log.Println("Stored with skip mode:", entry.Title)
} else {
if err = postingEntry(&entry, tubeConfig); err == nil {
tubeDB.Set(entry.ID, time.Now().Unix())
posted++
log.Printf("(%s) [%d][%s] Posted: %s\n", tubeName, posted, id, entry.Title)
// Post limitation check here
if posted == tubeConfig.GetInt("postLimit") {
return
}
} else {
log.Println(err)
return
}
}
}
}
}
return
}
func postingEntry(entry *rssEntry, tubeConfig *viper.Viper) error {
accessToken := tubeConfig.GetString("vk.accessToken")
groupID := tubeConfig.GetString("vk.groupID")
video, err := saveVideo(entry.Link.Href, accessToken, groupID)
if err != nil {
return errors.Wrap(err, "VK Save Video ERROR:")
}
time.Sleep(time.Second) // for rate limit!
if err = addPost(video.Response.OwnerID, video.Response.VideoID, entry, accessToken); err != nil {
if delErr := deleteVideo(video.Response.OwnerID, video.Response.VideoID, accessToken); delErr != nil {
return errors.Wrap(delErr, "VK Delete video ERROR:")
}
return errors.Wrap(err, "VK Add Post ERROR:")
}
telegramToken := tubeConfig.GetString("telegram.token")
telegramChannel := tubeConfig.GetString("telegram.channel")
if err = sendMessage(entry.Link.Href, entry.Group.Title, telegramToken, telegramChannel); err != nil {
log.Println("Telegram Add Post ERROR:", err)
// Ignore telegram error returns...
}
return nil
}