1
0

youtubeAPI.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. from xml.dom.minidom import parseString
  18. class Parser(object):
  19. def __init__(self, api_result):
  20. self.__api_data = api_result
  21. def parse(self):
  22. """Take the youtube API data and extract the important entries"""
  23. dom = parseString(self.__api_data)
  24. channel = YtChannel()
  25. i = 0
  26. for title in dom.getElementsByTagName('title'):
  27. xmlTag = title.toxml()
  28. if i == 0:
  29. channel.title = xmlTag.replace('<title>', '')
  30. channel.title = channel.title.replace('</title>', '')
  31. channel.title = channel.title.replace('&amp;', '&')
  32. channel.title = channel.title.replace('&quot;', '"')
  33. else:
  34. video_title = xmlTag.replace('<title>', '')
  35. video_title = video_title.replace('</title>', '')
  36. video_title = video_title.replace('&amp;', '&')
  37. video_title = video_title.replace('&quot;', '"')
  38. try:
  39. xmlTag = dom.getElementsByTagName('media:\
  40. description')[i-1].toxml()
  41. video_description = xmlTag.replace('<media:description '
  42. 'type="plain">',
  43. '')
  44. video_description = video_description.replace('</media:\
  45. description>', '')
  46. except IndexError:
  47. video_description = ""
  48. video_id = dom.getElementsByTagName('yt:videoid')[i-1].toxml()
  49. video_id = video_id.replace('<yt:videoid>', '').replace(
  50. '</yt:videoid>', '')
  51. channel.videos.append(YtVideo(video_title, video_description,
  52. video_id))
  53. i = i + 1
  54. return channel
  55. class YtChannel(object):
  56. def __init__(self):
  57. self.title = ""
  58. self.videos = []
  59. class YtVideo(object):
  60. def __init__(self, title, description, ytid):
  61. self.title = title
  62. self.description = description
  63. self.ytid = ytid