-
Notifications
You must be signed in to change notification settings - Fork 0
/
anime.go
197 lines (183 loc) · 5.91 KB
/
anime.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package database
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/greenmochi/ultimate/services/myanimelist/myanimelist/data"
)
// InsertAnime inserts an Anime struct into the anime table
func (d *Database) InsertAnime(anime *data.Anime) error {
tx, err := d.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(`
INSERT INTO anime (
id, url, title, img_src, img_blob, description,
synonyms, english, japanese,
type, episodes, status, aired, premiered, broadcast, producers, licensors, studios, source, genres, duration, rating,
score, ranked, popularity, members, favorites
)
VALUES (
?, ?, ?, ?, ?, ?,
?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?
)
ON CONFLICT DO NOTHING;
`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(
anime.ID, anime.URL, anime.Title, anime.ImgSrc, anime.ImgBlob, anime.Description,
anime.AltTitles.Synonyms, anime.AltTitles.English, anime.AltTitles.Japanese,
anime.Info.Type, anime.Info.Episodes, anime.Info.Status, anime.Info.Aired, anime.Info.Premiered, anime.Info.Broadcast, anime.Info.Producers, anime.Info.Licensors, anime.Info.Studios, anime.Info.Source, anime.Info.Genres, anime.Info.Duration, anime.Info.Rating,
anime.Stats.Score, anime.Stats.Ranked, anime.Stats.Popularity, anime.Stats.Members, anime.Stats.Favorites,
)
if err != nil {
return err
}
tx.Commit()
return nil
}
func (d *Database) retrieveAnime(query string, args ...interface{}) (*data.Anime, error) {
rows, err := d.db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
if ok := rows.Next(); !ok {
return nil, fmt.Errorf("No rows in anime table found for this query")
}
anime := &data.Anime{
ImgBlob: make([]byte, 0),
}
err = rows.Scan(
&anime.ID, &anime.URL, &anime.Title, &anime.ImgSrc, &anime.ImgBlob, &anime.Description,
&anime.AltTitles.Synonyms, &anime.AltTitles.English, &anime.AltTitles.Japanese,
&anime.Info.Type, &anime.Info.Episodes, &anime.Info.Status, &anime.Info.Aired, &anime.Info.Premiered, &anime.Info.Broadcast, &anime.Info.Producers, &anime.Info.Licensors, &anime.Info.Studios, &anime.Info.Source, &anime.Info.Genres, &anime.Info.Duration, &anime.Info.Rating,
&anime.Stats.Score, &anime.Stats.Ranked, &anime.Stats.Popularity, &anime.Stats.Members, &anime.Stats.Favorites,
)
if err != nil {
return nil, err
}
return anime, nil
}
// RetrieveAnimeByID retreives a row by an anime id from the anime table
func (d *Database) RetrieveAnimeByID(id int) (*data.Anime, error) {
query := `
SELECT
id, url, title, img_src, img_blob, description,
synonyms, english, japanese,
type, episodes, status, aired, premiered, broadcast, producers, licensors, studios, source, genres, duration, rating,
score, ranked, popularity, members, favorites
FROM
anime
WHERE
id = ?;
`
anime, err := d.retrieveAnime(query, id)
if err != nil {
log.Errorf("Failed to retrieve anime for %d", id)
return nil, err
}
return anime, nil
}
// RetrieveAnimeByTitle retreives a row by an anime id from the anime table
func (d *Database) RetrieveAnimeByTitle(title string) (*data.Anime, error) {
query := `
SELECT
id, url, title, img_src, img_blob, description,
synonyms, english, japanese,
type, episodes, status, aired, premiered, broadcast, producers, licensors, studios, source, genres, duration, rating,
score, ranked, popularity, members, favorites
FROM
anime
WHERE
title = ?;
`
anime, err := d.retrieveAnime(query, title)
if err != nil {
log.Errorf("Failed to retrieve anime for %s", title)
return nil, err
}
return anime, nil
}
// RetrieveAnimeByLink retreives a row by an anime link from the anime table
func (d *Database) RetrieveAnimeByLink(link string) (*data.Anime, error) {
query := `
SELECT
id, url, title, img_src, img_blob, description,
synonyms, english, japanese,
type, episodes, status, aired, premiered, broadcast, producers, licensors, studios, source, genres, duration, rating,
score, ranked, popularity, members, favorites
FROM
anime
WHERE
url = ?;
`
anime, err := d.retrieveAnime(query, link)
if err != nil {
log.Errorf("Failed to retrieve anime for %s", link)
return nil, err
}
return anime, nil
}
func (d *Database) animeExists(query string, args ...interface{}) (bool, error) {
rows, err := d.db.Query(query, args...)
if err != nil {
return false, fmt.Errorf("Failed to query when checking if an anime exists")
}
defer rows.Close()
if ok := rows.Next(); !ok {
return false, fmt.Errorf("Failed to find a row when checking if an anime exists")
}
var exists bool
if err := rows.Scan(&exists); err != nil {
return false, fmt.Errorf("Scanning went wrong when checking if an anime exists in the anime table. %s", err)
}
if err := rows.Err(); err != nil {
return false, fmt.Errorf("Error when checking if an anime exists in the anime table. %s", err)
}
return exists, nil
}
// AnimeByIDExists checks if an anime by id is in the anime table
func (d *Database) AnimeByIDExists(id int) bool {
query := `
SELECT
EXISTS(SELECT 1 FROM anime WHERE id = ?);
`
exists, err := d.animeExists(query, id)
if err != nil {
log.Errorf("Trying to check if an anime (id=%d) exists failed. %s", id, err)
return false
}
return exists
}
// AnimeByTitleExists checks if an anime by id is in the anime table
func (d *Database) AnimeByTitleExists(title string) bool {
query := `
SELECT
EXISTS(SELECT 1 FROM anime WHERE title = ?);
`
exists, err := d.animeExists(query, title)
if err != nil {
log.Errorf("Trying to check if an anime (title=%s) exists failed. %s", title, err)
return false
}
return exists
}
// AnimeByLinkExists checks if an anime by link is in the anime table
func (d *Database) AnimeByLinkExists(link string) bool {
query := `
SELECT
EXISTS(SELECT 1 FROM anime WHERE url = ?);
`
exists, err := d.animeExists(query, link)
if err != nil {
log.Errorf("Trying to check if an anime (link=%s) exists failed. %s", link, err)
return false
}
return exists
}