1
0

youtubeAPI.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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>', '').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. try:
  35. xmlTag = dom.getElementsByTagName('media:description')[i-1].toxml()
  36. video_description = xmlTag.replace('<media:description type="plain">',
  37. '').replace('</media:description>', '')
  38. except IndexError:
  39. video_description = ""
  40. video_id = dom.getElementsByTagName('yt:videoid')[i-1].toxml()
  41. video_id = video_id.replace('<yt:videoid>', '').replace('</yt:videoid>',
  42. '')
  43. channel.videos.append(YtVideo(video_title, video_description, video_id))
  44. i = i + 1
  45. return channel
  46. class YtChannel(object):
  47. def __init__(self):
  48. self.title = ""
  49. self.videos = []
  50. class YtVideo(object):
  51. def __init__(self, title, description, ytid):
  52. self.title = title
  53. self.description = description
  54. self.ytid = ytid