Skip to content

Commit

Permalink
refactor (#3763)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan authored Oct 16, 2023
1 parent ef5bd83 commit 563c2a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
7 changes: 6 additions & 1 deletion bridges/ARDAudiothekBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ public function collectData()
$item['timestamp'] = $audio->publicationStartDateAndTime;
$item['uid'] = $audio->id;
$item['author'] = $audio->programSet->publicationService->title;
$item['categories'] = [ $audio->programSet->editorialCategories->title ];

$category = $audio->programSet->editorialCategories->title ?? null;
if ($category) {
$item['categories'] = [$category];
}

$this->items[] = $item;
}
}
Expand Down
33 changes: 11 additions & 22 deletions bridges/YoutubeBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public function collectData()

private function collectDataInternal()
{
$xml = '';
$html = '';
$url_feed = '';
$url_listing = '';
Expand All @@ -115,16 +114,13 @@ private function collectDataInternal()

if ($username) {
// user and channel
$request = $username;
$url_feed = self::URI . '/feeds/videos.xml?user=' . urlencode($request);
$url_listing = self::URI . '/user/' . urlencode($request) . '/videos';
$url_feed = self::URI . '/feeds/videos.xml?user=' . urlencode($username);
$url_listing = self::URI . '/user/' . urlencode($username) . '/videos';
} elseif ($channel) {
$request = $channel;
$url_feed = self::URI . '/feeds/videos.xml?channel_id=' . urlencode($request);
$url_listing = self::URI . '/channel/' . urlencode($request) . '/videos';
$url_feed = self::URI . '/feeds/videos.xml?channel_id=' . urlencode($channel);
$url_listing = self::URI . '/channel/' . urlencode($channel) . '/videos';
} elseif ($custom) {
$request = $custom;
$url_listing = self::URI . '/' . urlencode($request) . '/videos';
$url_listing = self::URI . '/' . urlencode($custom) . '/videos';
}

if ($url_feed || $url_listing) {
Expand Down Expand Up @@ -152,7 +148,7 @@ private function collectDataInternal()
// $jsonData = $jsonData->itemSectionRenderer->contents[0]->gridRenderer->items;
$this->fetchItemsFromFromJsonData($jsonData);
} else {
returnServerError('Unable to get data from YouTube. Username/Channel: ' . $request);
returnServerError('Unable to get data from YouTube');
}
} else {
// Fetch the xml feed
Expand All @@ -162,13 +158,8 @@ private function collectDataInternal()
$this->feedName = str_replace(' - YouTube', '', $html->find('title', 0)->plaintext);
} elseif ($playlist) {
// playlist
// TODO: this mode makes a lot of excess video query requests.
// To make less requests, we need to cache following dictionary "videoId -> datePublished, duration"
// This cache will be used to find out, which videos to fetch
// to make feed of 15 items or more, if there a lot of videos published on that date.
$request = $playlist;
$url_feed = self::URI . '/feeds/videos.xml?playlist_id=' . urlencode($request);
$url_listing = self::URI . '/playlist?list=' . urlencode($request);
$url_feed = self::URI . '/feeds/videos.xml?playlist_id=' . urlencode($playlist);
$url_listing = self::URI . '/playlist?list=' . urlencode($playlist);
$html = $this->fetch($url_listing);
$jsonData = $this->extractJsonFromHtml($html);
// TODO: this method returns only first 100 video items
Expand All @@ -194,8 +185,7 @@ private function collectDataInternal()
});
} elseif ($search) {
// search
$request = $search;
$url_listing = self::URI . '/results?search_query=' . urlencode($request) . '&sp=CAI%253D';
$url_listing = self::URI . '/results?search_query=' . urlencode($search) . '&sp=CAI%253D';
$html = $this->fetch($url_listing);
$jsonData = $this->extractJsonFromHtml($html);
$jsonData = $jsonData->contents->twoColumnSearchResultsRenderer->primaryContents;
Expand All @@ -209,10 +199,9 @@ private function collectDataInternal()
}
$this->fetchItemsFromFromJsonData($jsonData);
$this->feeduri = $url_listing;
$this->feedName = 'Search: ' . $request;
$this->feedName = 'Search: ' . $search;
} else {
returnClientError("You must either specify either:\n - YouTube
username (?u=...)\n - Channel id (?c=...)\n - Playlist id (?p=...)\n - Search (?s=...)");
returnClientError("You must either specify either:\n - YouTube username (?u=...)\n - Channel id (?c=...)\n - Playlist id (?p=...)\n - Search (?s=...)");
}
}

Expand Down

0 comments on commit 563c2a3

Please sign in to comment.