-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_manga_title.php
347 lines (324 loc) · 11.5 KB
/
api_manga_title.php
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
// api_manga_title.php
header('Content-Type: application/json');
// Check if title and uid parameters are provided
if (isset($_GET['title']) && isset($_GET['uid'])) {
$episode_name = $_GET['title'];
$user_id = $_GET['uid'];
try {
// Connect to the SQLite database
$db = new PDO('sqlite:database.sqlite');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query to get the latest image for the specified episode and user
$queryLatest = "
SELECT
images.*,
users.id as userid,
users.artist
FROM images
JOIN users ON images.email = users.email
WHERE artwork_type = 'manga'
AND episode_name = :episode_name
AND users.id = :user_id
ORDER BY images.id DESC
LIMIT 1
";
$stmtLatest = $db->prepare($queryLatest);
$stmtLatest->bindParam(':episode_name', $episode_name);
$stmtLatest->bindParam(':user_id', $user_id);
$stmtLatest->execute();
// Fetch the latest image result
$latest_cover = $stmtLatest->fetch(PDO::FETCH_ASSOC);
// Query to get the first image for the specified episode and user
$queryFirst = "
SELECT
images.*,
users.id as userid,
users.artist
FROM images
JOIN users ON images.email = users.email
WHERE artwork_type = 'manga'
AND episode_name = :episode_name
AND users.id = :user_id
ORDER BY images.id ASC
LIMIT 1
";
$stmtFirst = $db->prepare($queryFirst);
$stmtFirst->bindParam(':episode_name', $episode_name);
$stmtFirst->bindParam(':user_id', $user_id);
$stmtFirst->execute();
// Fetch the first image result
$first_cover = $stmtFirst->fetch(PDO::FETCH_ASSOC);
// Remove email field from results (optional, if you fetched it)
// This is not necessary if you properly excluded it from the SELECT query
if (isset($latest_cover['email'])) {
unset($latest_cover['email']);
}
if (isset($first_cover['email'])) {
unset($first_cover['email']);
}
// Query to count the total number of images from images and image_child tables
$query = "
SELECT COUNT(*) as total_count
FROM (
SELECT id, filename, email
FROM images
WHERE artwork_type = 'manga'
AND episode_name = :episode_name
UNION ALL
SELECT image_child.id, image_child.filename, images.email
FROM image_child
JOIN images ON image_child.image_id = images.id
WHERE images.artwork_type = 'manga'
AND images.episode_name = :episode_name
) AS all_images
";
$stmt = $db->prepare($query);
$stmt->bindParam(':episode_name', $episode_name);
$stmt->execute();
// Fetch the total count result
$total_count = $stmt->fetchColumn();
// Query to get all images for the specified episode and user
$query = "
SELECT
images.*,
users.id as userid,
users.artist
FROM images
JOIN users ON images.email = users.email
WHERE artwork_type = 'manga'
AND episode_name = :episode_name
AND users.id = :user_id
ORDER BY images.id DESC
";
$stmt = $db->prepare($query);
$stmt->bindParam(':episode_name', $episode_name);
$stmt->bindParam(':user_id', $user_id);
$stmt->execute();
// Fetch all results
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Remove email field from results
foreach ($results as &$result) {
unset($result['email']);
}
// Calculate the total view_count from all images
$total_view_count = 0;
foreach ($results as $image) {
$total_view_count += $image['view_count'];
}
// Get the tags from the current title
$tags = [];
foreach ($results as $image) {
$imageTags = explode(',', $image['tags']);
foreach ($imageTags as $tag) {
$tag = trim($tag);
if (!empty($tag)) {
if (!isset($tags[$tag])) {
$tags[$tag] = 0;
}
}
}
}
// Get the count of latest images by episode_name for each tag
$query = "
SELECT tags, COUNT(*) as count FROM (
SELECT tags, episode_name, MAX(id) as latest_image_id
FROM images
WHERE artwork_type = 'manga'
GROUP BY tags, episode_name
) GROUP BY tags
";
$stmt = $db->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$tagList = explode(',', $row['tags']);
foreach ($tagList as $tag) {
$tag = trim($tag);
if (isset($tags[$tag])) {
$tags[$tag] += $row['count'];
}
}
}
// Get the parodies from the current title
$parodies = [];
foreach ($results as $image) {
$imageParodies = explode(',', $image['parodies']);
foreach ($imageParodies as $parody) {
$parody = trim($parody);
if (!empty($parody)) {
if (!isset($parodies[$parody])) {
$parodies[$parody] = 0;
}
}
}
}
// Get the count of latest images by episode_name for each parody
$query = "
SELECT parodies, COUNT(*) as count FROM (
SELECT parodies, episode_name, MAX(id) as latest_image_id
FROM images
WHERE artwork_type = 'manga'
GROUP BY parodies, episode_name
) GROUP BY parodies
";
$stmt = $db->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$parodyList = explode(',', $row['parodies']);
foreach ($parodyList as $parody) {
$parody = trim($parody); // Fix variable name here
if (isset($parodies[$parody])) {
$parodies[$parody] += $row['count'];
}
}
}
// Get the characters from the current title
$characters = [];
foreach ($results as $image) {
$imageCharacters = explode(',', $image['characters']);
foreach ($imageCharacters as $character) {
$character = trim($character);
if (!empty($character)) {
if (!isset($characters[$character])) {
$characters[$character] = 0;
}
}
}
}
// Get the count of latest images by episode_name for each character
$query = "
SELECT characters, COUNT(*) as count FROM (
SELECT characters, episode_name, MAX(id) as latest_image_id
FROM images
WHERE artwork_type = 'manga'
GROUP BY characters, episode_name
) GROUP BY characters
";
$stmt = $db->query($query);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$characterList = explode(',', $row['characters']);
foreach ($characterList as $character) {
$character = trim($character); // Fix variable name here
if (isset($characters[$character])) {
$characters[$character] += $row['count'];
}
}
}
// Get the count of all non-empty "group" images grouped by the "group" column based on all episode_name
$queryGroupCounts = "
SELECT images.`group`, COUNT(*) as count
FROM (
SELECT DISTINCT episode_name, MAX(id) as latest_image_id
FROM images
WHERE artwork_type = 'manga'
AND email = (SELECT email FROM users WHERE id = :user_id)
GROUP BY episode_name
) AS latest_images
JOIN images ON latest_images.latest_image_id = images.id
JOIN users ON images.email = users.email
WHERE images.artwork_type = 'manga'
AND users.id = :user_id
AND images.`group` IS NOT NULL AND images.`group` <> ''
AND images.`group` IN (
SELECT DISTINCT images.`group`
FROM images
WHERE artwork_type = 'manga'
AND episode_name = :episode_name
AND email = (SELECT email FROM users WHERE id = :user_id)
)
GROUP BY images.`group`
";
$stmtGroupCounts = $db->prepare($queryGroupCounts);
$stmtGroupCounts->bindParam(':user_id', $user_id);
$stmtGroupCounts->bindParam(':episode_name', $episode_name);
$stmtGroupCounts->execute();
$groupCounts = $stmtGroupCounts->fetchAll(PDO::FETCH_ASSOC);
// Get the count of all images grouped by the "categories" column based on the latest image in current tags for the current episode_name
$queryCategoriesCounts = "
SELECT images.categories, COUNT(*) as count
FROM (
SELECT DISTINCT episode_name, MAX(id) as latest_image_id
FROM images
WHERE artwork_type = 'manga'
GROUP BY episode_name
) AS latest_images
JOIN images ON latest_images.latest_image_id = images.id
WHERE images.artwork_type = 'manga'
AND images.categories IS NOT NULL AND images.categories <> ''
AND images.categories IN (
SELECT DISTINCT images.categories
FROM images
WHERE artwork_type = 'manga'
AND episode_name = :episode_name
)
GROUP BY images.categories
";
$stmtCategoriesCounts = $db->prepare($queryCategoriesCounts);
$stmtCategoriesCounts->bindParam(':episode_name', $episode_name);
$stmtCategoriesCounts->execute();
$categoriesCounts = $stmtCategoriesCounts->fetchAll(PDO::FETCH_ASSOC);
// Get the count of all images grouped by the "language" column based on the latest image in current tags for the current episode_name
$queryLanguageCounts = "
SELECT images.language, COUNT(*) as count
FROM (
SELECT DISTINCT episode_name, MAX(id) as latest_image_id
FROM images
WHERE artwork_type = 'manga'
GROUP BY episode_name
) AS latest_images
JOIN images ON latest_images.latest_image_id = images.id
WHERE images.artwork_type = 'manga'
AND images.language IS NOT NULL AND images.language <> ''
AND images.language IN (
SELECT DISTINCT images.language
FROM images
WHERE artwork_type = 'manga'
AND episode_name = :episode_name
)
GROUP BY images.language
";
$stmtLanguageCounts = $db->prepare($queryLanguageCounts);
$stmtLanguageCounts->bindParam(':episode_name', $episode_name);
$stmtLanguageCounts->execute();
$languageCounts = $stmtLanguageCounts->fetchAll(PDO::FETCH_ASSOC);
// Get the number of latest images by the current artist grouped by episode_name
$query = "
SELECT COUNT(*) AS count
FROM (
SELECT MAX(id) as latest_image_id
FROM images
WHERE artwork_type = 'manga'
AND email = (
SELECT email
FROM users
WHERE id = :user_id
)
GROUP BY episode_name
) latest_images
";
$stmt = $db->prepare($query);
$stmt->bindParam(':user_id', $user_id);
$stmt->execute();
$artistImageCount = $stmt->fetchColumn();
// Prepare the response data
$response = [
'latest_cover' => $latest_cover,
'first_cover' => $first_cover,
'images' => $results,
'tags' => $tags,
'parodies' => $parodies,
'characters' => $characters,
'artist_image_count' => $artistImageCount,
'total_count' => $total_count,
'total_view_count' => $total_view_count,
'group_counts' => $groupCounts,
'categories_counts' => $categoriesCounts,
'language_counts' => $languageCounts
];
// Output response as JSON
echo json_encode($response, JSON_PRETTY_PRINT);
} catch (PDOException $e) {
echo json_encode(['error' => $e->getMessage()]);
}
} else {
echo json_encode(['error' => 'Missing title or uid parameter']);
}
?>