Skip to content

Commit

Permalink
fix: K4 panel redirect on api calls #62
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Nov 17, 2023
1 parent 8b6a0e1 commit cc36a89
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
11 changes: 6 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@
'action' => function ($episodeSlug) {
$podcast = new Podcast();

return json_encode($podcast->getPodloveRoles($episodeSlug));
return new Response(json_encode($podcast->getPodloveRoles($episodeSlug)), 'application/json');
}
],
[
'pattern' => 'podcaster/podlove/groups/(:all)',
'action' => function ($episodeSlug) {
$podcast = new Podcast();

return json_encode($podcast->getPodloveRoles($episodeSlug));
return new Response(json_encode($podcast->getPodloveRoles($episodeSlug)), 'application/json');
}
],
[
Expand All @@ -144,7 +144,7 @@
$podcast = new Podcast();
$episode = $podcast->getPageFromSlug($episodeSlug);

return json_encode($podcast->getPodloveConfigJson($episode));
return new Response(json_encode($podcast->getPodloveConfigJson($episode)), 'application/json');
}
],
[
Expand All @@ -153,7 +153,7 @@
$podcast = new Podcast();
$episode = $podcast->getPageFromSlug($episodeSlug);

return json_encode($podcast->getPodloveEpisodeJson($episode));
return new Response(json_encode($podcast->getPodloveEpisodeJson($episode)), 'application/json');
}
],
[
Expand All @@ -172,7 +172,8 @@
}

$json = file_get_contents(__DIR__ . '/res/' . $endpoint . '.json');
return $json;

return new Response(json_encode($json), 'application/json');
}
],
],
Expand Down
75 changes: 38 additions & 37 deletions internal/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
$results = $stats->getDownloadsGraphData($podcastId, $year, $month);

if ($results === false) {
return ['days' => []];
return new Response(json_encode(['days' => []]), 'application/json');
}

$trackedDays = $results->toArray();
Expand All @@ -32,7 +32,7 @@
$days[$dayAsInt] = (int)$day->downloads;
}

return ['days' => $days];
return new Response(json_encode(['days' => $days]), 'application/json');
},
],
[
Expand All @@ -50,7 +50,7 @@
$results = $stats->getEpisodesGraphData($podcastId);

if ($results === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

$trackedMonths = $results->toArray();
Expand All @@ -62,15 +62,15 @@
$month = (int)$entry->month;
$downloads = (int)$entry->downloads;

if(!isset($downloadArray[$year])) {
if (!isset($downloadArray[$year])) {
$downloadArray[$year] = [];
}

$downloadArray[$year][$month] = $downloads;
}

foreach($downloadArray as $year => $months) {
for($i = 1; $i <= 12; $i++) {
foreach ($downloadArray as $year => $months) {
for ($i = 1; $i <= 12; $i++) {
$downloadJson[] = [
'downloads' => $months[$i] ?? 0,
'year' => $year,
Expand All @@ -79,7 +79,7 @@
}
}

return ['downloads' => $downloadJson];
return new Response(json_encode(['downloads' => $downloadJson]), 'application/json');
},
],
[
Expand All @@ -97,7 +97,7 @@
$results = $stats->getFeedsGraphData($podcastId);

if ($results === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

$trackedMonths = $results->toArray();
Expand All @@ -109,15 +109,15 @@
$month = (int)$entry->month;
$downloads = (int)$entry->downloads;

if(!isset($downloadArray[$year])) {
if (!isset($downloadArray[$year])) {
$downloadArray[$year] = [];
}

$downloadArray[$year][$month] = $downloads;
}

foreach($downloadArray as $year => $months) {
for($i = 1; $i <= 12; $i++) {
foreach ($downloadArray as $year => $months) {
for ($i = 1; $i <= 12; $i++) {
$downloadJson[] = [
'downloads' => $months[$i] ?? 0,
'year' => $year,
Expand All @@ -126,7 +126,7 @@
}
}

return ['downloads' => $downloadJson];
return new Response(json_encode(['downloads' => $downloadJson]), 'application/json');
},
],
[
Expand All @@ -150,7 +150,7 @@
$results = $stats->getQuickReports($podcastId, $year, $month);

if ($results === false) {
return ['reports' => []];
return new Response(json_encode(['reports' => []]), 'application/json');
}

$trackedDays = $results['detailed']->toArray();
Expand Down Expand Up @@ -182,7 +182,7 @@
'overall' => $allTime,
];

return ['reports' => $reports];
return new Response(json_encode(['reports' => $reports]), 'application/json');
},
],
[
Expand All @@ -200,17 +200,18 @@
$results = $stats->getEpisodeGraphData($podcastId, $episode);

if ($results === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

$cleandUpResult = [];
foreach($results->toArray() as $result) {
foreach ($results->toArray() as $result) {
$cleandUpResult[] = [
'date' => $result->date,
'downloads' => (int)$result->downloads
];
}
return $cleandUpResult;

return new Response(json_encode($cleandUpResult), 'application/json');
},
],
[
Expand All @@ -228,18 +229,18 @@
$results = $stats->getDevicesGraphData($podcastId, $year, $month);

if ($results === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

$total = 0;
foreach ($results->toArray() as $result) {
$total += (int)$result->downloads;
}

return [
return new Response(json_encode([
'total' => $total,
'data' => $results->toArray(),
];
]), 'application/json');
},
],
[
Expand All @@ -257,18 +258,18 @@
$results = $stats->getUserAgentGraphData($podcastId, $year, $month);

if ($results === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

$total = 0;
foreach ($results->toArray() as $result) {
$total += (int)$result->downloads;
}

return [
return new Response(json_encode([
'total' => $total,
'data' => $results->toArray(),
];
]), 'application/json');
},
],
[
Expand All @@ -286,18 +287,18 @@
$results = $stats->getSystemGraphData($podcastId, $year, $month);

if ($results === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

$total = 0;
foreach ($results->toArray() as $result) {
$total += (int)$result->downloads;
}

return [
return new Response(json_encode([
'total' => $total,
'data' => $results->toArray(),
];
]), 'application/json');
},
],
[
Expand All @@ -315,10 +316,10 @@
$results = $stats->getTopEpisodes($podcastId);

if ($results === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

return $results->toArray();
return new Response(json_encode($results->toArray()), 'application/json');
},
],
[
Expand All @@ -334,13 +335,13 @@
$rssFeed = $podcastTools->getPodcastFromId($podcastId);

if (!isset($rssFeed)) {
return [];
return new Response(json_encode([]), 'application/json');
}

$episodes = $podcastTools->getEpisodes($rssFeed);

if ($episodes === false) {
return [];
return new Response(json_encode([]), 'application/json');
}

$episodeList = [];
Expand All @@ -351,7 +352,7 @@
];
}

return $episodeList;
return new Response(json_encode($episodeList), 'application/json');
},
],
[
Expand All @@ -367,13 +368,13 @@
$rssFeed = $podcastTools->getPodcastFromId($podcastId);

if (!isset($rssFeed)) {
return ['estSubscribers' => 0];
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
}

$episodes = $podcastTools->getEpisodes($rssFeed);

if ($episodes === false || !isset($episodes)) {
return ['estSubscribers' => 0];
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
}

$latestEpisodes = $episodes
Expand All @@ -382,7 +383,7 @@
});

if (!isset($latestEpisodes)) {
return ['estSubscribers' => 0];
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
}

$latestEpisodes = $latestEpisodes->limit(3);
Expand All @@ -393,7 +394,7 @@
}

if (count($episodeList) === 0) {
return ['estSubscribers' => 0];
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
}

$dbType = option('mauricerenck.podcaster.statsType', 'sqlite');
Expand All @@ -402,7 +403,7 @@
$results = $stats->getEstimatedSubscribers($podcastId, $episodeList);

if ($results === false || count($results) === 0) {
return ['estSubscribers' => 0];
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
}

$estSubscribers = 0;
Expand All @@ -412,7 +413,7 @@

$subs = $estSubscribers / count($results);

return ['estSubscribers' => $subs];
return new Response(json_encode(['estSubscribers' => $subs]), 'application/json');
},
],
],
Expand Down

0 comments on commit cc36a89

Please sign in to comment.