1
0

youtubeAPI.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2014.
  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 sys
  18. import ssl
  19. if sys.version_info >= (3,):
  20. import urllib.request as urllib2
  21. else:
  22. import urllib2
  23. from xml.dom.minidom import parseString
  24. from lib_stov import stov_exceptions
  25. class Parser(object):
  26. def __init__(self, api_result):
  27. self.__api_data = api_result
  28. def parse(self):
  29. """Take the youtube API data and extract the important entries"""
  30. dom = parseString(self.__api_data)
  31. channel = YtChannel()
  32. i = 0
  33. for title in dom.getElementsByTagName('title'):
  34. xmlTag = title.toxml()
  35. if i == 0:
  36. channel.title = xmlTag.replace('<title>', '')
  37. channel.title = channel.title.replace('</title>', '')
  38. channel.title = channel.title.replace('&amp;', '&')
  39. channel.title = channel.title.replace('&quot;', '"')
  40. else:
  41. video_title = xmlTag.replace('<title>', '')
  42. video_title = video_title.replace('</title>', '')
  43. video_title = video_title.replace('&amp;', '&')
  44. video_title = video_title.replace('&quot;', '"')
  45. try:
  46. xmlTag = dom.getElementsByTagName('media:\
  47. description')[i-1].toxml()
  48. video_description = xmlTag.replace('<media:description '
  49. 'type="plain">',
  50. '')
  51. video_description = video_description.replace('</media:\
  52. description>', '')
  53. except IndexError:
  54. video_description = ""
  55. video_id = dom.getElementsByTagName('yt:videoid')[i-1].toxml()
  56. video_id = video_id.replace('<yt:videoid>', '').replace(
  57. '</yt:videoid>', '')
  58. channel.videos.append(YtVideo(video_title, video_description,
  59. video_id))
  60. i = i + 1
  61. return channel
  62. class YtChannel(object):
  63. def __init__(self):
  64. self.title = ""
  65. self.videos = []
  66. class YtVideo(object):
  67. def __init__(self, title, description, ytid):
  68. self.title = title
  69. self.description = description
  70. self.ytid = ytid
  71. class Connector(object):
  72. """Handles all connections to the youtube API and invokes the parsing
  73. functions.
  74. """
  75. def __init__(self, type, name, search, conf):
  76. """Populates the object will all necessary variables."""
  77. self._type = type
  78. self._name = name
  79. self._search = search
  80. self._conf = conf
  81. self._APIURL = self._ConstructAPIURL()
  82. def ParseAPIData(self, video_list):
  83. """Retrieves the youtube API data and parses them so they can be used
  84. by the object
  85. """
  86. try:
  87. ConnectAPI = urllib2.urlopen(self._APIURL, timeout=30)
  88. APIResponse = ConnectAPI.read()
  89. ConnectAPI.close()
  90. except urllib2.HTTPError:
  91. raise stov_exceptions.NoDataFromYoutubeAPIException()
  92. except ssl.SSLError:
  93. raise stov_exceptions.YoutubeAPITimeoutException(self._name)
  94. else:
  95. parser = Parser(APIResponse)
  96. parsed_response = parser.parse()
  97. return parsed_response
  98. def _ConstructAPIURL(self):
  99. """Constructs the API URL which is used to retrieve API data"""
  100. if self._type == "channel":
  101. APIURL = "https://gdata.youtube.com/feeds/api/users/" \
  102. + urllib2.quote(self._name)\
  103. + "/uploads/?v=2&max-results=%s"\
  104. % self._conf.values["maxvideos"]
  105. if self._search != "":
  106. APIURL = APIURL + "&q=" + "%22" \
  107. + urllib2.quote(self._search) + "%22"
  108. elif self._type == "search":
  109. APIURL = "http://gdata.youtube.com/feeds/api/videos?q=" \
  110. + urllib2.quote(self._search) + "&v=2" \
  111. + "&max-results=%s" % self._conf.values["maxvideos"]
  112. elif self._type == "playlist":
  113. APIURL = "https://gdata.youtube.com/feeds/api/playlists/" \
  114. + "%20" + urllib2.quote(self._name) + "%20?v=2" \
  115. + "&max-results=%s" \
  116. % self._conf.values["maxvideos"]
  117. return APIURL