From 87d218296e513c08a1a3ff71062b46a9bdcb1d1a Mon Sep 17 00:00:00 2001
From: Eugene Molotov <eugene.molotov@yandex.ru>
Date: Sat, 20 Oct 2018 15:43:48 +0500
Subject: [PATCH] [YoutubeBridge] Fix playlist mode (#876)

* Corrected duration text selector
* Request YouTube page with English localization
* Filter video items in the beginning of the loop
---
 bridges/YoutubeBridge.php | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/bridges/YoutubeBridge.php b/bridges/YoutubeBridge.php
index c021fdc1..67e95668 100644
--- a/bridges/YoutubeBridge.php
+++ b/bridges/YoutubeBridge.php
@@ -147,12 +147,19 @@ class YoutubeBridge extends BridgeAbstract {
 				$time = 0;
 				$vid = str_replace('/watch?v=', '', $element->find('a', 0)->href);
 				$vid = substr($vid, 0, strpos($vid, '&') ?: strlen($vid));
-				$title = $this->ytBridgeFixTitle($element->find($title_selector, 0)->plaintext);
+				$title = trim($this->ytBridgeFixTitle($element->find($title_selector, 0)->plaintext));
+
+				if (strpos($vid, 'googleads') !== false
+					|| $title == '[Private video]'
+					|| $title == '[Deleted video]'
+				) {
+					continue;
+				}
 
 				// The duration comes in one of the formats:
 				// hh:mm:ss / mm:ss / m:ss
 				// 01:03:30 / 15:06 / 1:24
-				$durationText = trim($element->find('span[class="video-time"]', 0)->plaintext);
+				$durationText = trim($element->find('div.timestamp span', 0)->plaintext);
 				$durationText = preg_replace('/([\d]{1,2})\:([\d]{2})/', '00:$1:$2', $durationText);
 
 				sscanf($durationText, '%d:%d:%d', $hours, $minutes, $seconds);
@@ -162,13 +169,11 @@ class YoutubeBridge extends BridgeAbstract {
 					continue;
 				}
 
-				if($title != '[Private Video]' && strpos($vid, 'googleads') === false) {
-					if ($add_parsed_items) {
-						$this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
-						$this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
-					}
-					$count++;
+				if ($add_parsed_items) {
+					$this->ytBridgeQueryVideoInfo($vid, $author, $desc, $time);
+					$this->ytBridgeAddItem($vid, $title, $author, $desc, $time);
 				}
+				$count++;
 			}
 		}
 		return $count;
@@ -181,7 +186,9 @@ class YoutubeBridge extends BridgeAbstract {
 
 	private function ytGetSimpleHTMLDOM($url){
 		return getSimpleHTMLDOM($url,
-			$header = array(),
+			$header = array(
+				'Accept-Language: en-US'
+			),
 			$opts = array(),
 			$lowercase = true,
 			$forceTagsClosed = true,