-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
194 lines (168 loc) · 5.9 KB
/
Copy pathapi.php
File metadata and controls
194 lines (168 loc) · 5.9 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
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
<?php
// Start output buffering and turn off error output
ob_start();
error_reporting(E_ERROR); // Only report fatal errors
ini_set('display_errors', 0); // Don't display errors in output
// Include your existing manga scraper code
require_once 'manga_scraper_manager.php';
// Now set headers
header('Content-Type: application/json');
// Function to sanitize data for JSON encoding
function sanitizeForJson($data) {
if (is_array($data)) {
$clean = [];
foreach ($data as $key => $value) {
$clean[$key] = sanitizeForJson($value);
}
return $clean;
} else if (is_object($data) && !is_a($data, 'Facebook\WebDriver\Remote\RemoteWebElement')) {
// Convert objects to arrays, but skip WebDriver elements
return sanitizeForJson((array)$data);
} else if (is_string($data)) {
// Clean strings - remove invalid UTF-8 sequences
return mb_convert_encoding($data, 'UTF-8', 'UTF-8');
} else if (is_a($data, 'Facebook\WebDriver\Remote\RemoteWebElement')) {
// Skip WebDriver elements entirely
return "[WebDriver Element]";
} else {
// Return other types as is
return $data;
}
}
// Initialize the manager
$manager = new MangaScraperManager();
$response = ['success' => false, 'error' => 'Action not specified'];
// Handle GET requests
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$action = $_GET['action'] ?? '';
switch ($action) {
case 'search':
$mangaTitle = $_GET['title'] ?? '';
$selectedSite = $_GET['site'] ?? 'all';
if (empty($mangaTitle)) {
$response = ['success' => false, 'error' => 'Manga title is required'];
break;
}
if ($selectedSite !== 'all') {
// If a specific site is selected, only search that one
$scraper = new MangaScraper($selectedSite);
$result = $scraper->searchManga($mangaTitle);
if ($result) {
$response = [
'success' => true,
'results' => [
[
'site' => $selectedSite,
'found' => true,
'manga_url' => $result['manga_url'],
'chapter_count' => count($result['chapters']),
'latest_chapter' => $result['latest_chapter'] ?? 0,
'chapters' => $result['chapters'],
'log' => $scraper->getLog()
]
]
];
} else {
$response = [
'success' => true,
'results' => [
[
'site' => $selectedSite,
'found' => false,
'log' => $scraper->getLog()
]
]
];
}
} else {
// Search all sites
$results = $manager->searchManga($mangaTitle);
$response = [
'success' => true,
'results' => $results,
'log' => $manager->getLog()
];
}
break;
case 'google_search':
$mangaTitle = $_GET['title'] ?? '';
if (empty($mangaTitle)) {
$response = ['success' => false, 'error' => 'Manga title is required'];
break;
}
$results = $manager->searchOnGoogle($mangaTitle);
if ($results) {
$response = [
'success' => true,
'results' => $results,
'log' => $manager->getLog()
];
} else {
$response = [
'success' => false,
'error' => 'No results found',
'log' => $manager->getLog()
];
}
break;
case 'list_sites':
$sites = $manager->listSites();
$response = [
'success' => true,
'sites' => $sites,
'log' => $manager->getLog()
];
break;
default:
$response = [
'success' => false,
'error' => 'Invalid action'
];
}
}
// Handle POST requests
else if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$action = $_POST['action'] ?? '';
switch ($action) {
case 'add_site':
$url = $_POST['url'] ?? '';
if (empty($url)) {
$response = ['success' => false, 'error' => 'URL is required'];
break;
}
$result = $manager->addSite($url);
$response = [
'success' => $result,
'log' => $manager->getLog()
];
break;
case 'remove_site':
$url = $_POST['url'] ?? '';
if (empty($url)) {
$response = ['success' => false, 'error' => 'URL is required'];
break;
}
$result = $manager->removeSite($url);
$response = [
'success' => $result,
'log' => $manager->getLog()
];
break;
default:
$response = [
'success' => false,
'error' => 'Invalid action'
];
}
} else {
$response = [
'success' => false,
'error' => 'Invalid request method'
];
}
// Clear any previous output
ob_end_clean();
// Send ONLY ONE JSON response
header('Content-Type: application/json');
echo json_encode(sanitizeForJson($response), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
exit;