|
15 | 15 | package youtube
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "fmt" |
18 | 19 | "log"
|
| 20 | + "net/http" |
19 | 21 |
|
20 | 22 | "github.com/bwmarrin/discordgo"
|
21 | 23 | )
|
@@ -53,3 +55,34 @@ func UnsubscribeChannel(channelID string) {
|
53 | 55 | log.Printf("YouTube: unsubscribed '%s' from announcements", channelID)
|
54 | 56 | }
|
55 | 57 | }
|
| 58 | + |
| 59 | +// RefreshSubscriptions sends a subscription request to the youtube hub |
| 60 | +func RefreshSubscriptions() { |
| 61 | + for id := range subscribtions { |
| 62 | + log.Printf("[YouTube] Requesting refresh subscription for id '%s'...", id) |
| 63 | + |
| 64 | + url := "https://pubsubhubbub.appspot.com/subscribe" |
| 65 | + req, err := http.NewRequest(http.MethodGet, url, nil) |
| 66 | + if err != nil { |
| 67 | + log.Printf("Error on creating refresh subscription: %v", err) |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + req.Form.Set("hub.callback", "https://webhook.cake4everyone.de/api/yt_pubsubhubbub") |
| 72 | + req.Form.Set("hub.topic", fmt.Sprintf("https://www.youtube.com/xml/feeds/videos.xml?channel_id=%s", id)) |
| 73 | + req.Form.Set("hub.verify", "sync") |
| 74 | + req.Form.Set("hub.mode", "subscribeconst") |
| 75 | + |
| 76 | + resp, err := http.DefaultClient.Do(req) |
| 77 | + if err != nil { |
| 78 | + log.Printf("[YouTube] Refresh request failed: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 82 | + log.Printf("[YouTube] Refreshing for channel '%s' failed with status %d", id, resp.StatusCode) |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + log.Printf("[YouTube] Successfully refreshed subscription for channel '%s'", id) |
| 87 | + } |
| 88 | +} |
0 commit comments