1
0

youtubeAPI.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #
  2. # This file is part of stov, written by Helmut Pozimski in 2012.
  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 xml.dom.minidom import parseString
  17. class Parser(object):
  18. def __init__(self, api_result):
  19. self.__api_data = api_result
  20. def parse(self):
  21. """Take the youtube API data and extract the important entries"""
  22. dom = parseString(self.__api_data)
  23. channel = YtChannel()
  24. i = 0
  25. for title in dom.getElementsByTagName('title'):
  26. xmlTag = title.toxml()
  27. if i == 0:
  28. channel.title = xmlTag.replace('<title>', '').replace('</title>', '')
  29. channel.title = channel.title.replace('&amp;', '&').replace('&quot;', '"')
  30. else:
  31. video_title = xmlTag.replace('<title>', '').replace('</title>', '')
  32. video_title = video_title.replace('&amp;', '&').replace('&quot;', '"')
  33. xmlTag = dom.getElementsByTagName('media:description')[i-1].toxml()
  34. video_description = xmlTag.replace('<media:description type="plain">',
  35. '').replace('</media:description>', '')
  36. video_id = dom.getElementsByTagName('yt:videoid')[i-1].toxml()
  37. video_id = video_id.replace('<yt:videoid>', '').replace('</yt:videoid>',
  38. '')
  39. channel.videos.append(YtVideo(video_title, video_description, video_id))
  40. i = i + 1
  41. return channel
  42. class YtChannel(object):
  43. def __init__(self):
  44. self.title = u""
  45. self.videos = []
  46. class YtVideo(object):
  47. def __init__(self, title, description, ytid):
  48. self.title = title
  49. self.description = description
  50. self.ytid = ytid