youtubeAPI.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #
  2. # This file is part of stov, written by Helmut Pozimski 2012-2013.
  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>', '').replace('</title>', '')
  30. channel.title = channel.title.replace('&amp;', '&').replace('&quot;', '"')
  31. else:
  32. video_title = xmlTag.replace('<title>', '').replace('</title>', '')
  33. video_title = video_title.replace('&amp;', '&').replace('&quot;', '"')
  34. xmlTag = dom.getElementsByTagName('media:description')[i-1].toxml()
  35. video_description = xmlTag.replace('<media:description type="plain">',
  36. '').replace('</media:description>', '')
  37. video_id = dom.getElementsByTagName('yt:videoid')[i-1].toxml()
  38. video_id = video_id.replace('<yt:videoid>', '').replace('</yt:videoid>',
  39. '')
  40. channel.videos.append(YtVideo(video_title, video_description, video_id))
  41. i = i + 1
  42. return channel
  43. class YtChannel(object):
  44. def __init__(self):
  45. self.title = ""
  46. self.videos = []
  47. class YtVideo(object):
  48. def __init__(self, title, description, ytid):
  49. self.title = title
  50. self.description = description
  51. self.ytid = ytid