1
0

noapi.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2015.
  3. #
  4. # stov is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 2 of the License.
  7. #
  8. # stov is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  15. # -*- coding: utf8 -*-
  16. from __future__ import unicode_literals
  17. import subprocess
  18. import sys
  19. import lxml.html
  20. if sys.version_info >= (3,):
  21. import urllib.request as urllib2
  22. else:
  23. import urllib2
  24. from lib_stov import youtubeAPI
  25. from lib_stov import stov_exceptions
  26. class Connector(object):
  27. """This class will retrieve all the necessary data from youtube using
  28. youtube-dl, thus bypassing the API.
  29. """
  30. def __init__(self, type, name, conf, search=""):
  31. """Populates the object with all necessary data."""
  32. self._type = type
  33. self._name = name
  34. self._search = search
  35. self._conf = conf
  36. self._title = ""
  37. self._url = ""
  38. self._construct_url()
  39. def _construct_url(self):
  40. if self._type == "channel":
  41. self._url = "https://www.youtube.com/user/%s" \
  42. % urllib2.quote(self._name)
  43. elif self._type == "search":
  44. self._url = "https://www.youtube.com/results?search_query=%s"\
  45. % urllib2.quote(self._search)
  46. elif self._type == "playlist":
  47. self._url = "https://www.youtube.com/playlist?list=%s" \
  48. % urllib2.quote(self._name)
  49. def _fetch_title(self):
  50. """Retrieves the title of the HTML page to use as a title for the
  51. subscription."""
  52. data = urllib2.urlopen(self._url)
  53. parsed_html = lxml.html.parse(data)
  54. data.close()
  55. i = 0
  56. for item in parsed_html.iter("title"):
  57. if i == 0:
  58. self._title = item.text_content().strip().replace("\n", "")
  59. i += 1
  60. if self._search != "" and self._type == "channel":
  61. self._title += _(" search %s") % self._search
  62. def _fetch_videos(self, existing_videos):
  63. """Retrieves all the relevant videos in a subscription."""
  64. videos_list = []
  65. if self._conf.outputlevel == "verbose":
  66. stderr = sys.stderr
  67. else:
  68. stderr = open("/dev/null", "w")
  69. if self._type == "channel" and self._search != "":
  70. try:
  71. video_ids = subprocess.check_output([
  72. self._conf.values["youtube-dl"],
  73. "--max-downloads",
  74. self._conf.values["maxvideos"],
  75. "--match-title",
  76. self._search,
  77. "--get-id",
  78. self._url], stderr=stderr).strip()
  79. except subprocess.CalledProcessError as e:
  80. video_ids = e.output.strip()
  81. else:
  82. try:
  83. video_ids = subprocess.check_output([
  84. self._conf.values["youtube-dl"], "--max-downloads",
  85. self._conf.values["maxvideos"], "--get-id",
  86. self._url], stderr=stderr).strip()
  87. except subprocess.CalledProcessError as e:
  88. video_ids = e.output.strip()
  89. if len(video_ids) >= 1:
  90. for video_id in video_ids.split("\n"):
  91. video_exists = False
  92. if existing_videos:
  93. for existing_video in existing_videos:
  94. if video_id == existing_video.ytid:
  95. video_exists = True
  96. break
  97. if not video_exists:
  98. try:
  99. video_title = subprocess.check_output([
  100. self._conf.values["youtube-dl"], "--get-title",
  101. "https://www.youtube.com/watch?v=%s"
  102. % video_id], stderr=stderr).strip()
  103. video_description = subprocess.check_output([
  104. self._conf.values["youtube-dl"], "--get-description",
  105. "https://www.youtube.com/watch?v=%s"
  106. % video_id], stderr=stderr).strip()
  107. except subprocess.CalledProcessError:
  108. raise stov_exceptions.YoutubeDlCallFailed()
  109. else:
  110. videos_list.append(youtubeAPI.YtVideo(
  111. unicode(video_title, "utf-8"),
  112. unicode(video_description, "utf-8"),
  113. unicode(video_id)))
  114. return videos_list
  115. def ParseAPIData(self, existing_videos):
  116. """This method calls all necessary methods to retrieve the data
  117. and assembles them into a Channel object. The naming of this
  118. method is set according to the method in youtubeAPI to be
  119. compatible.
  120. """
  121. self._fetch_title()
  122. videos = self._fetch_videos(existing_videos)
  123. channel = youtubeAPI.YtChannel()
  124. channel.title = unicode(self._title)
  125. channel.videos = videos
  126. return channel