-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_manga_parodies.php
60 lines (48 loc) · 1.64 KB
/
api_manga_parodies.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
<?php
// api_manga_parodies.php
header('Content-Type: application/json');
// Connect to the SQLite database
$db = new SQLite3('database.sqlite', SQLITE3_OPEN_READONLY);
// Function to retrieve parody counts
function getParodyCounts($db) {
// Retrieve the count of latest images 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";
// Log the query for debugging
error_log("SQL Query: " . $query);
$result = $db->query($query);
if (!$result) {
return ['error' => 'Query failed: ' . $db->lastErrorMsg()];
}
// Store the counts as an associative array
$counts = [];
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$parodyList = explode(',', $row['parodies']);
foreach ($parodyList as $parody) {
$trimmedParody = trim($parody);
if (!empty($trimmedParody)) { // Exclude empty parodies
if (!isset($counts[$trimmedParody])) {
$counts[$trimmedParody] = 0;
}
$counts[$trimmedParody] += $row['count'];
}
}
}
// Sort alphabetically and numerically
ksort($counts, SORT_NATURAL | SORT_FLAG_CASE);
// Prepare the response
$response = ['parodies' => $counts];
return $response;
}
try {
$response = getParodyCounts($db);
// Output response as JSON
echo json_encode($response, JSON_PRETTY_PRINT);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
}
?>