Skip to content

Commit

Permalink
[YoutubeBridge] Playlist mode: faster feed generating if item count i…
Browse files Browse the repository at this point in the history
…s less or equal to 15 (RSS-Bridge#648)

* [YoutubeBridge] Playlist mode: faster feed generating if item count is less or equal to 15
  • Loading branch information
em92 authored and teromene committed Mar 19, 2018
1 parent dfcad1a commit 7b2b1ae
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions bridges/YoutubeBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ private function ytBridgeParseXmlFeed($xml) {
$this->feedName = $this->ytBridgeFixTitle($xml->find('feed > title', 0)->plaintext); // feedName will be used by getName()
}

private function ytBridgeParseHtmlListing($html, $element_selector, $title_selector){
$limit = 10;
private function ytBridgeParseHtmlListing($html, $element_selector, $title_selector, $add_parsed_items = true) {
$limit = $add_parsed_items ? 10 : INF;
$count = 0;
foreach($html->find($element_selector) as $element) {
if($count < $limit) {
Expand All @@ -122,12 +122,15 @@ private function ytBridgeParseHtmlListing($html, $element_selector, $title_selec
$vid = substr($vid, 0, strpos($vid, '&') ?: strlen($vid));
$title = $this->ytBridgeFixTitle($element->find($title_selector, 0)->plaintext);
if($title != '[Private Video]' && strpos($vid, 'googleads') === false) {
$this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
$this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
if ($add_parsed_items) {
$this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
$this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
}
$count++;
}
}
}
return $count;
}

private function ytBridgeFixTitle($title) {
Expand Down Expand Up @@ -176,10 +179,16 @@ public function collectData(){
}
} elseif($this->getInput('p')) { /* playlist mode */
$this->request = $this->getInput('p');
$url_feed = self::URI . 'feeds/videos.xml?playlist_id=' . urlencode($this->request);
$url_listing = self::URI . 'playlist?list=' . urlencode($this->request);
$html = $this->ytGetSimpleHTMLDOM($url_listing)
or returnServerError("Could not request YouTube. Tried:\n - $url_listing");
$this->ytBridgeParseHtmlListing($html, 'tr.pl-video', '.pl-video-title a');
$item_count = $this->ytBridgeParseHtmlListing($html, 'tr.pl-video', '.pl-video-title a', false);
if ($item_count <= 15 && ($xml = $this->ytGetSimpleHTMLDOM($url_feed))) {
$this->ytBridgeParseXmlFeed($xml);
} else {
$this->ytBridgeParseHtmlListing($html, 'tr.pl-video', '.pl-video-title a');
}
$this->feedName = 'Playlist: ' . str_replace(' - YouTube', '', $html->find('title', 0)->plaintext); // feedName will be used by getName()
usort($this->items, function ($item1, $item2) {
return $item2['timestamp'] - $item1['timestamp'];
Expand Down

0 comments on commit 7b2b1ae

Please sign in to comment.