|
21 | 21 | $results = $stats->getDownloadsGraphData($podcastId, $year, $month); |
22 | 22 |
|
23 | 23 | if ($results === false) { |
24 | | - return ['days' => []]; |
| 24 | + return new Response(json_encode(['days' => []]), 'application/json'); |
25 | 25 | } |
26 | 26 |
|
27 | 27 | $trackedDays = $results->toArray(); |
|
32 | 32 | $days[$dayAsInt] = (int)$day->downloads; |
33 | 33 | } |
34 | 34 |
|
35 | | - return ['days' => $days]; |
| 35 | + return new Response(json_encode(['days' => $days]), 'application/json'); |
36 | 36 | }, |
37 | 37 | ], |
38 | 38 | [ |
|
50 | 50 | $results = $stats->getEpisodesGraphData($podcastId); |
51 | 51 |
|
52 | 52 | if ($results === false) { |
53 | | - return []; |
| 53 | + return new Response(json_encode([]), 'application/json'); |
54 | 54 | } |
55 | 55 |
|
56 | 56 | $trackedMonths = $results->toArray(); |
|
62 | 62 | $month = (int)$entry->month; |
63 | 63 | $downloads = (int)$entry->downloads; |
64 | 64 |
|
65 | | - if(!isset($downloadArray[$year])) { |
| 65 | + if (!isset($downloadArray[$year])) { |
66 | 66 | $downloadArray[$year] = []; |
67 | 67 | } |
68 | 68 |
|
69 | 69 | $downloadArray[$year][$month] = $downloads; |
70 | 70 | } |
71 | 71 |
|
72 | | - foreach($downloadArray as $year => $months) { |
73 | | - for($i = 1; $i <= 12; $i++) { |
| 72 | + foreach ($downloadArray as $year => $months) { |
| 73 | + for ($i = 1; $i <= 12; $i++) { |
74 | 74 | $downloadJson[] = [ |
75 | 75 | 'downloads' => $months[$i] ?? 0, |
76 | 76 | 'year' => $year, |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 |
|
82 | | - return ['downloads' => $downloadJson]; |
| 82 | + return new Response(json_encode(['downloads' => $downloadJson]), 'application/json'); |
83 | 83 | }, |
84 | 84 | ], |
85 | 85 | [ |
|
97 | 97 | $results = $stats->getFeedsGraphData($podcastId); |
98 | 98 |
|
99 | 99 | if ($results === false) { |
100 | | - return []; |
| 100 | + return new Response(json_encode([]), 'application/json'); |
101 | 101 | } |
102 | 102 |
|
103 | 103 | $trackedMonths = $results->toArray(); |
|
109 | 109 | $month = (int)$entry->month; |
110 | 110 | $downloads = (int)$entry->downloads; |
111 | 111 |
|
112 | | - if(!isset($downloadArray[$year])) { |
| 112 | + if (!isset($downloadArray[$year])) { |
113 | 113 | $downloadArray[$year] = []; |
114 | 114 | } |
115 | 115 |
|
116 | 116 | $downloadArray[$year][$month] = $downloads; |
117 | 117 | } |
118 | 118 |
|
119 | | - foreach($downloadArray as $year => $months) { |
120 | | - for($i = 1; $i <= 12; $i++) { |
| 119 | + foreach ($downloadArray as $year => $months) { |
| 120 | + for ($i = 1; $i <= 12; $i++) { |
121 | 121 | $downloadJson[] = [ |
122 | 122 | 'downloads' => $months[$i] ?? 0, |
123 | 123 | 'year' => $year, |
|
126 | 126 | } |
127 | 127 | } |
128 | 128 |
|
129 | | - return ['downloads' => $downloadJson]; |
| 129 | + return new Response(json_encode(['downloads' => $downloadJson]), 'application/json'); |
130 | 130 | }, |
131 | 131 | ], |
132 | 132 | [ |
|
150 | 150 | $results = $stats->getQuickReports($podcastId, $year, $month); |
151 | 151 |
|
152 | 152 | if ($results === false) { |
153 | | - return ['reports' => []]; |
| 153 | + return new Response(json_encode(['reports' => []]), 'application/json'); |
154 | 154 | } |
155 | 155 |
|
156 | 156 | $trackedDays = $results['detailed']->toArray(); |
|
182 | 182 | 'overall' => $allTime, |
183 | 183 | ]; |
184 | 184 |
|
185 | | - return ['reports' => $reports]; |
| 185 | + return new Response(json_encode(['reports' => $reports]), 'application/json'); |
186 | 186 | }, |
187 | 187 | ], |
188 | 188 | [ |
|
200 | 200 | $results = $stats->getEpisodeGraphData($podcastId, $episode); |
201 | 201 |
|
202 | 202 | if ($results === false) { |
203 | | - return []; |
| 203 | + return new Response(json_encode([]), 'application/json'); |
204 | 204 | } |
205 | 205 |
|
206 | 206 | $cleandUpResult = []; |
207 | | - foreach($results->toArray() as $result) { |
| 207 | + foreach ($results->toArray() as $result) { |
208 | 208 | $cleandUpResult[] = [ |
209 | 209 | 'date' => $result->date, |
210 | 210 | 'downloads' => (int)$result->downloads |
211 | 211 | ]; |
212 | 212 | } |
213 | | - return $cleandUpResult; |
| 213 | + |
| 214 | + return new Response(json_encode($cleandUpResult), 'application/json'); |
214 | 215 | }, |
215 | 216 | ], |
216 | 217 | [ |
|
228 | 229 | $results = $stats->getDevicesGraphData($podcastId, $year, $month); |
229 | 230 |
|
230 | 231 | if ($results === false) { |
231 | | - return []; |
| 232 | + return new Response(json_encode([]), 'application/json'); |
232 | 233 | } |
233 | 234 |
|
234 | 235 | $total = 0; |
235 | 236 | foreach ($results->toArray() as $result) { |
236 | 237 | $total += (int)$result->downloads; |
237 | 238 | } |
238 | 239 |
|
239 | | - return [ |
| 240 | + return new Response(json_encode([ |
240 | 241 | 'total' => $total, |
241 | 242 | 'data' => $results->toArray(), |
242 | | - ]; |
| 243 | + ]), 'application/json'); |
243 | 244 | }, |
244 | 245 | ], |
245 | 246 | [ |
|
257 | 258 | $results = $stats->getUserAgentGraphData($podcastId, $year, $month); |
258 | 259 |
|
259 | 260 | if ($results === false) { |
260 | | - return []; |
| 261 | + return new Response(json_encode([]), 'application/json'); |
261 | 262 | } |
262 | 263 |
|
263 | 264 | $total = 0; |
264 | 265 | foreach ($results->toArray() as $result) { |
265 | 266 | $total += (int)$result->downloads; |
266 | 267 | } |
267 | 268 |
|
268 | | - return [ |
| 269 | + return new Response(json_encode([ |
269 | 270 | 'total' => $total, |
270 | 271 | 'data' => $results->toArray(), |
271 | | - ]; |
| 272 | + ]), 'application/json'); |
272 | 273 | }, |
273 | 274 | ], |
274 | 275 | [ |
|
286 | 287 | $results = $stats->getSystemGraphData($podcastId, $year, $month); |
287 | 288 |
|
288 | 289 | if ($results === false) { |
289 | | - return []; |
| 290 | + return new Response(json_encode([]), 'application/json'); |
290 | 291 | } |
291 | 292 |
|
292 | 293 | $total = 0; |
293 | 294 | foreach ($results->toArray() as $result) { |
294 | 295 | $total += (int)$result->downloads; |
295 | 296 | } |
296 | 297 |
|
297 | | - return [ |
| 298 | + return new Response(json_encode([ |
298 | 299 | 'total' => $total, |
299 | 300 | 'data' => $results->toArray(), |
300 | | - ]; |
| 301 | + ]), 'application/json'); |
301 | 302 | }, |
302 | 303 | ], |
303 | 304 | [ |
|
315 | 316 | $results = $stats->getTopEpisodes($podcastId); |
316 | 317 |
|
317 | 318 | if ($results === false) { |
318 | | - return []; |
| 319 | + return new Response(json_encode([]), 'application/json'); |
319 | 320 | } |
320 | 321 |
|
321 | | - return $results->toArray(); |
| 322 | + return new Response(json_encode($results->toArray()), 'application/json'); |
322 | 323 | }, |
323 | 324 | ], |
324 | 325 | [ |
|
334 | 335 | $rssFeed = $podcastTools->getPodcastFromId($podcastId); |
335 | 336 |
|
336 | 337 | if (!isset($rssFeed)) { |
337 | | - return []; |
| 338 | + return new Response(json_encode([]), 'application/json'); |
338 | 339 | } |
339 | 340 |
|
340 | 341 | $episodes = $podcastTools->getEpisodes($rssFeed); |
341 | 342 |
|
342 | 343 | if ($episodes === false) { |
343 | | - return []; |
| 344 | + return new Response(json_encode([]), 'application/json'); |
344 | 345 | } |
345 | 346 |
|
346 | 347 | $episodeList = []; |
|
351 | 352 | ]; |
352 | 353 | } |
353 | 354 |
|
354 | | - return $episodeList; |
| 355 | + return new Response(json_encode($episodeList), 'application/json'); |
355 | 356 | }, |
356 | 357 | ], |
357 | 358 | [ |
|
367 | 368 | $rssFeed = $podcastTools->getPodcastFromId($podcastId); |
368 | 369 |
|
369 | 370 | if (!isset($rssFeed)) { |
370 | | - return ['estSubscribers' => 0]; |
| 371 | + return new Response(json_encode(['estSubscribers' => 0]), 'application/json'); |
371 | 372 | } |
372 | 373 |
|
373 | 374 | $episodes = $podcastTools->getEpisodes($rssFeed); |
374 | 375 |
|
375 | 376 | if ($episodes === false || !isset($episodes)) { |
376 | | - return ['estSubscribers' => 0]; |
| 377 | + return new Response(json_encode(['estSubscribers' => 0]), 'application/json'); |
377 | 378 | } |
378 | 379 |
|
379 | 380 | $latestEpisodes = $episodes |
|
382 | 383 | }); |
383 | 384 |
|
384 | 385 | if (!isset($latestEpisodes)) { |
385 | | - return ['estSubscribers' => 0]; |
| 386 | + return new Response(json_encode(['estSubscribers' => 0]), 'application/json'); |
386 | 387 | } |
387 | 388 |
|
388 | 389 | $latestEpisodes = $latestEpisodes->limit(3); |
|
393 | 394 | } |
394 | 395 |
|
395 | 396 | if (count($episodeList) === 0) { |
396 | | - return ['estSubscribers' => 0]; |
| 397 | + return new Response(json_encode(['estSubscribers' => 0]), 'application/json'); |
397 | 398 | } |
398 | 399 |
|
399 | 400 | $dbType = option('mauricerenck.podcaster.statsType', 'sqlite'); |
|
402 | 403 | $results = $stats->getEstimatedSubscribers($podcastId, $episodeList); |
403 | 404 |
|
404 | 405 | if ($results === false || count($results) === 0) { |
405 | | - return ['estSubscribers' => 0]; |
| 406 | + return new Response(json_encode(['estSubscribers' => 0]), 'application/json'); |
406 | 407 | } |
407 | 408 |
|
408 | 409 | $estSubscribers = 0; |
|
412 | 413 |
|
413 | 414 | $subs = $estSubscribers / count($results); |
414 | 415 |
|
415 | | - return ['estSubscribers' => $subs]; |
| 416 | + return new Response(json_encode(['estSubscribers' => $subs]), 'application/json'); |
416 | 417 | }, |
417 | 418 | ], |
418 | 419 | ], |
|
0 commit comments