-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (88 loc) · 2.37 KB
/
Copy pathmain.go
File metadata and controls
103 lines (88 loc) · 2.37 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
package main
import (
"encoding/base64"
"fmt"
"github.com/ChimeraCoder/anaconda"
"github.com/OGFris/ATTBot/fetcher"
"github.com/OGFris/ATTBot/twist"
"github.com/animenotifier/kitsu"
_ "github.com/joho/godotenv/autoload"
"io/ioutil"
"net/http"
"net/url"
"os"
)
func main() {
bot := anaconda.NewTwitterApiWithCredentials(os.Getenv("ACCESS_TOKEN"),
os.Getenv("ACCESS_TOKEN_SECRET"),
os.Getenv("CONSUMER_KEY"),
os.Getenv("CONSUMER_SECRET"))
lastFetch := new(twist.EpisodesFeed)
fetcher.EveryFetch(func(animes *twist.AnimesFeed, episodes *twist.EpisodesFeed) {
if lastFetch.Lastbuilddate == "" {
lastFetch = episodes
} else {
for n, item := range episodes.Items {
if item.AnimetwistID == lastFetch.Items[0].AnimetwistID {
if n != 0 {
for i := 0; i < n; i++ {
episode := episodes.Items[i]
fmt.Println("Found a new anime release:", episode.Description)
kitsuAnime, err := kitsu.GetAnime(fmt.Sprint(episode.KitsuID))
if err != nil {
panic(err)
}
// fmt.Println("Cover link:", kitsuAnime.Attributes.PosterImage.Original)
resp, err := http.Get(kitsuAnime.Attributes.PosterImage.Original)
if err != nil {
panic(err)
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
err = resp.Body.Close()
if err != nil {
panic(err)
}
media, err := bot.UploadMedia(base64.StdEncoding.EncodeToString(bytes))
if err != nil {
panic(err)
}
// fmt.Println("Uploaded the anime cover to twitter.")
anime := new(twist.AnimesFeedItem)
for _, item := range animes.Items {
if item.AnimetwistID == episode.AnimetwistID {
// fmt.Println("Anime is:", item.AnimeTitle)
anime = &item
break
}
}
if anime.AnimeOngoing == 1 {
_, err := bot.PostTweet(episode.Description,
url.Values{
"media_ids": {media.MediaIDString},
})
if err != nil {
panic(err)
}
} else {
if episode.EpisodeNumber == 1 {
_, err := bot.PostTweet(episode.Description,
url.Values{
"media_ids": {media.MediaIDString},
})
if err != nil {
panic(err)
}
}
}
}
}
break
}
}
lastFetch = episodes
}
})
}