Advertisement
Guest User

Untitled

a guest
Nov 12th, 2015
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Kodi plugin: plays latest news from Russian 1tv.
  5.  
  6. """
  7.  
  8. import sys
  9. import urllib2
  10. from xml.etree import ElementTree
  11. from HTMLParser import HTMLParser
  12. import xbmc
  13. import xbmcgui
  14. import xbmcplugin
  15.  
  16. __author__ = "Dmitry Sandalov"
  17. __copyright__ = "Copyright 2014, Dmitry Sandalov"
  18. __credits__ = []
  19. __license__ = "GNU GPL v2.0"
  20. __version__ = "1.0.4"
  21. __maintainer__ = "Dmitry Sandalov"
  22. __email__ = "dmitry@sandalov.org"
  23. __status__ = "Development"
  24.  
  25.  
  26. class MyHTMLParser(HTMLParser):
  27.  
  28.     def __init__(self):
  29.         HTMLParser.__init__(self)
  30.         self.links = []
  31.  
  32.     def handle_starttag(self, tag, attrs):
  33.         if tag == "iframe":
  34.             for name, value in attrs:
  35.                 if name == "src" in name:
  36.                     self.links.append(value)
  37.  
  38.     def get_latest(self):
  39.         return self.links[0].replace('/iframed/embednewslist.html?id=', '')
  40.  
  41.  
  42. def message():
  43.     dialog = xbmcgui.Dialog()
  44.     return dialog.yesno(heading="No connection",
  45.                         line1="Check your connection and try again",
  46.                         nolabel="Exit", yeslabel="Retry")
  47.  
  48.  
  49. def get_last_edition():
  50.     news_archive = 'http://www.1tv.ru/newsvideoarchive/'
  51.     html = urllib2.urlopen(news_archive).read()
  52.     parser = MyHTMLParser()
  53.     parser.feed(html)
  54.     last = parser.get_latest()
  55.     return urllib2.urlopen('http://www.1tv.ru/swfxml/newsvyp/'+last)
  56.  
  57. addon_handle = int(sys.argv[1])
  58. xbmcplugin.setContent(addon_handle, 'movies')
  59.  
  60. edition = ''
  61. while True:
  62.     try:
  63.         edition = get_last_edition()
  64.     except urllib2.URLError:
  65.         retry = message()
  66.         if retry:
  67.             continue
  68.         else:
  69.             sys.exit()
  70.     break
  71.  
  72. tree = ElementTree.parse(edition)
  73. root = tree.getroot()
  74. namespace = "{http://search.yahoo.com/mrss/}"
  75.  
  76. items = []
  77. playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
  78. xbmc.PlayList.clear(playlist)
  79. for item in root.getiterator('item'):
  80.     url = item.find(namespace + "content").attrib['url']
  81.     title = item.find('title').text
  82.     img = item.find(namespace + "thumbnail").attrib['url']
  83.     li = xbmcgui.ListItem(label=title, iconImage=img, thumbnailImage=img)
  84.     items.append((url, li, False,))
  85.     playlist.add(url=url, listitem=li, index=len(items))
  86.  
  87. xbmcplugin.addDirectoryItems(addon_handle, items, totalItems=len(items))
  88. xbmc.Player().play(playlist)
  89.  
  90. xbmcplugin.endOfDirectory(addon_handle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement