|
| 1 | +''' |
| 2 | + VimCasts KODI Addon |
| 3 | + ------------------- |
| 4 | +
|
| 5 | + Watch screencasts from http://vimcasts.org in KODI. |
| 6 | +
|
| 7 | + :copyright: (c) 2012 by Jonathan Beluch |
| 8 | + :license: GPLv3, see LICENSE.txt for more details. |
| 9 | +''' |
| 10 | +import re |
| 11 | +import sys |
| 12 | +try: |
| 13 | + import json |
| 14 | +except ImportError: |
| 15 | + import simplejson as json |
| 16 | +from xbmcswift2 import Plugin |
| 17 | + |
| 18 | +PY3 = sys.version_info.major >= 3 |
| 19 | + |
| 20 | +if PY3: |
| 21 | + from html.parser import HTMLParser |
| 22 | + from urllib.request import urlopen |
| 23 | +else: |
| 24 | + from HTMLParser import HTMLParser |
| 25 | + from urllib2 import urlopen |
| 26 | + |
| 27 | +PLUGIN_NAME = 'VimCasts' |
| 28 | +PLUGIN_ID = 'plugin.video.vimcasts' |
| 29 | +plugin = Plugin(PLUGIN_NAME, PLUGIN_ID, __file__) |
| 30 | + |
| 31 | + |
| 32 | +def get_json_feed(): |
| 33 | + '''Loads the JSON feed for vimcasts.org.''' |
| 34 | + json_url = 'http://vimcasts.org/episodes.json?referrer=xbmc' |
| 35 | + conn = urlopen(json_url) |
| 36 | + _json = json.load(conn) |
| 37 | + conn.close() |
| 38 | + return _json |
| 39 | + |
| 40 | + |
| 41 | +def strip_tags(inp): |
| 42 | + '''Naively strips instances of <tag> from the given inp''' |
| 43 | + return re.sub('(<.+?>)', '', inp) |
| 44 | + |
| 45 | + |
| 46 | +_parser = HTMLParser() |
| 47 | +def unescape_html(inp): |
| 48 | + '''Replaces named instances of html entities with the corresponding |
| 49 | + unescaped character. |
| 50 | +
|
| 51 | + >>> unescape_html('apples & oranges') |
| 52 | + apples & oranges |
| 53 | + ''' |
| 54 | + return _parser.unescape(inp) |
| 55 | + |
| 56 | + |
| 57 | +def clean(inp): |
| 58 | + '''Strips HTML tags and unescapes named HTML entities for the given input. |
| 59 | +
|
| 60 | + >>> clean('<strong>apples & oranges</strong>') |
| 61 | + apples & oranges |
| 62 | + ''' |
| 63 | + return unescape_html(strip_tags(inp)) |
| 64 | + |
| 65 | + |
| 66 | +@plugin.route('/') |
| 67 | +def index(): |
| 68 | + '''The main menu and only view for this plugin. Lists available episodes''' |
| 69 | + items = [{ |
| 70 | + 'label': '#%s %s' % (epi['episode_number'], epi['title']), |
| 71 | + 'path': epi['quicktime']['url'], |
| 72 | + 'thumbnail': epi['poster'], |
| 73 | + 'fanart': plugin.fanart, |
| 74 | + 'info': { |
| 75 | + 'plot': clean(epi['abstract']), |
| 76 | + 'duration': int(epi['quicktime']['seconds']) |
| 77 | + }, |
| 78 | + 'is_playable': True, |
| 79 | + } for epi in get_json_feed()['episodes'] if epi['quicktime']['url']] |
| 80 | + |
| 81 | + finish_kwargs = { |
| 82 | + 'sort_methods': [ |
| 83 | + ('UNSORTED', '%X'), |
| 84 | + ('TITLE', '%X'), |
| 85 | + 'DURATION', |
| 86 | + ], |
| 87 | + } |
| 88 | + |
| 89 | + return plugin.finish(items, **finish_kwargs) |
| 90 | + |
| 91 | + |
| 92 | +def run(): |
| 93 | + plugin.run() |
0 commit comments