|
@@ -21,10 +21,11 @@ youtubeAPI module earlier."""
|
|
|
|
|
|
import subprocess
|
|
|
import sys
|
|
|
-import lxml.html
|
|
|
import urllib.parse
|
|
|
import urllib.request
|
|
|
+import urllib.error
|
|
|
|
|
|
+import lxml.html
|
|
|
from lib_stov import stov_exceptions
|
|
|
|
|
|
|
|
@@ -61,9 +62,12 @@ class Connector(object):
|
|
|
"""Constructs the URL to request from youtube-dl according to the
|
|
|
subscription type and the given parameters.
|
|
|
"""
|
|
|
- if self._type == "channel":
|
|
|
+ if self._type == "user":
|
|
|
self._url = "https://www.youtube.com/user/%s" \
|
|
|
% urllib.parse.quote(self._name)
|
|
|
+ elif self._type == "channel":
|
|
|
+ self._url = "https://www.youtube.com/channel/%s" \
|
|
|
+ % urllib.parse.quote(self._name)
|
|
|
elif self._type == "search":
|
|
|
self._url = "https://www.youtube.com/results?search_query=%s"\
|
|
|
% urllib.parse.quote(self._search)
|
|
@@ -74,7 +78,28 @@ class Connector(object):
|
|
|
def _fetch_title(self):
|
|
|
"""Retrieves the title of the HTML page to use as a title for the
|
|
|
subscription."""
|
|
|
- data = urllib.request.urlopen(self._url)
|
|
|
+ try:
|
|
|
+ data = urllib.request.urlopen(self._url)
|
|
|
+ except urllib.error.HTTPError as err:
|
|
|
+ if err.code == 404 and self._type == "user":
|
|
|
+ self._type = "channel"
|
|
|
+ self._construct_url()
|
|
|
+ try:
|
|
|
+ data = urllib.request.urlopen(self._url)
|
|
|
+ except urllib.error.HTTPError:
|
|
|
+ raise stov_exceptions.ChannelNotFound()
|
|
|
+ else:
|
|
|
+ self._parse_title(data)
|
|
|
+ else:
|
|
|
+ raise stov_exceptions.ChannelNotFound()
|
|
|
+ else:
|
|
|
+ self._parse_title(data)
|
|
|
+
|
|
|
+ def _parse_title(self, data):
|
|
|
+ """ Parses the title from a HTML document
|
|
|
+ :param data: HTTP connection to the document
|
|
|
+ :type data: http.client.HTTPResponse
|
|
|
+ """
|
|
|
parsed_html = lxml.html.parse(data)
|
|
|
data.close()
|
|
|
i = 0
|
|
@@ -82,7 +107,7 @@ class Connector(object):
|
|
|
if i == 0:
|
|
|
self._title = item.text_content().strip().replace("\n", "")
|
|
|
i += 1
|
|
|
- if self._search != "" and self._type == "channel":
|
|
|
+ if self._search != "" and self._type == "user":
|
|
|
self._title += _(" search %s") % self._search
|
|
|
|
|
|
def _fetch_videos(self, existing_videos):
|
|
@@ -92,7 +117,7 @@ class Connector(object):
|
|
|
stderr = sys.stderr
|
|
|
else:
|
|
|
stderr = open("/dev/null", "w")
|
|
|
- if self._type == "channel" and self._search != "":
|
|
|
+ if self._type == "user" and self._search != "":
|
|
|
try:
|
|
|
video_ids = subprocess.check_output([
|
|
|
self._conf.values["youtube-dl"],
|