Skip to content

Commit

Permalink
Pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
Dagur committed Jul 9, 2014
1 parent 42fc635 commit 8e0fafb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 69 deletions.
7 changes: 4 additions & 3 deletions plugin.video.sarpur/default.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# encoding: UTF-8


import sys
import sarpur
import urlparse
import xbmcplugin
Expand Down Expand Up @@ -32,15 +32,16 @@
actions.tab_index(action_value)
elif action_key == 'view_podcast_index':
actions.podcast_index()
elif action_key =='view_podcast_show':
elif action_key == 'view_podcast_show':
actions.podcast_show(action_value, name)
elif action_key == 'play_podcast':
actions.play_podcast(action_value)
elif action_key == 'play_live':
actions.play_live_stream(action_value)
else:

logger.log("Action: {0}, Value: {1}, Name: {2}".format(action_key, action_value, name))
logger.log("Action: {0}, Value: {1}, Name: {2}".format(
action_key, action_value, name))

finally:
xbmcplugin.endOfDirectory(sarpur.ADDON_HANDLE)
54 changes: 30 additions & 24 deletions plugin.video.sarpur/sarpur/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,59 @@


import sarpur
import scraper
import sarpur.scraper as scraper
import util.player as player
from sarpur.cached import Categories
from util.gui import GUI


gui = GUI(sarpur.ADDON_HANDLE, sarpur.BASE_URL)
cats = Categories()
INTERFACE = GUI(sarpur.ADDON_HANDLE, sarpur.BASE_URL)
CATS = Categories()

def index():
""" The front page (i.e. the first one the user sees when opening the plugin) """
"""
The front page (i.e. the first one the user sees when opening the plugin)
"""

for tab in cats.tabs:
for tab in CATS.tabs:
title = tab[0].encode('utf-8')
url = tab[1].encode('utf-8')
gui.addDir(title, 'view_tab', url)
INTERFACE.addDir(title, 'view_tab', url)

for i, channel in enumerate(cats.showtree):
for i, channel in enumerate(CATS.showtree):
title = '{0} Þættir'.format(channel['name'].encode('utf-8'))
gui.addDir(title, 'view_channel_index', i)
INTERFACE.addDir(title, 'view_channel_index', i)

gui.addDir('Hlaðvarp', 'view_podcast_index', '')
gui.addItem('Bein úttsending: RÚV', 'play_live', 'ruv')
gui.addItem('Bein úttsending: RÁS 1', 'play_live', 'ras1')
gui.addItem('Bein úttsending: RÁS 2', 'play_live', 'ras2')
gui.addItem('Bein úttsending: RONDÓ', 'play_live', 'rondo')
INTERFACE.addDir('Hlaðvarp', 'view_podcast_index', '')
INTERFACE.addItem('Bein úttsending: RÚV', 'play_live', 'ruv')
INTERFACE.addItem('Bein úttsending: RÁS 1', 'play_live', 'ras1')
INTERFACE.addItem('Bein úttsending: RÁS 2', 'play_live', 'ras2')
INTERFACE.addItem('Bein úttsending: RONDÓ', 'play_live', 'rondo')

def channel_index(channel):
for i, category in enumerate(cats.showtree[channel].get('categories')):
gui.addDir(category['name'].encode('utf-8'),
for i, category in enumerate(CATS.showtree[channel].get('categories')):
INTERFACE.addDir(category['name'].encode('utf-8'),
'view_channel_category',
"{0};{1}".format(channel,i))
"{0};{1}".format(channel, i))

def channel_category(channel, category):
for show in cats.showtree[channel]['categories'][category]['shows']:
for show in CATS.showtree[channel]['categories'][category]['shows']:
name, url = show
if url[0] == '/':
url = 'http://dagskra.ruv.is%s' % url
gui.addDir(name.encode('utf-8'), 'view_channel_category_show', url.encode('utf-8'))
INTERFACE.addDir(name.encode('utf-8'),
'view_channel_category_show',
url.encode('utf-8'))

def channel_category_show(url, show_name):
episodes = scraper.get_episodes(url)
if not episodes:
gui.infobox("Engar upptökur", "Engin upptaka fannst")
INTERFACE.infobox("Engar upptökur", "Engin upptaka fannst")
else:
for episode in episodes:
episode_name, url = episode
name = "{0} - {1}".format(show_name, episode_name.encode('utf-8'))
gui.addItem(name, 'play', url)
INTERFACE.addItem(name, 'play', url)

def play_video(url, name):
(playpath, rtmp_url, swfplayer) = scraper.get_stream_info(url)
Expand All @@ -70,19 +74,21 @@ def tab_index(url):

episodes = scraper.get_tab_items(pageurl)
if not episodes:
gui.infobox("Engar upptökur", "Engin upptaka fannst")
INTERFACE.infobox("Engar upptökur", "Engin upptaka fannst")
else:
for episode in episodes:
episode_name, url = episode
gui.addItem(episode_name.encode('utf-8'), 'play', url.encode('utf-8'))
INTERFACE.addItem(episode_name.encode('utf-8'),
'play',
url.encode('utf-8'))

def podcast_index():
for show in scraper.get_podcast_shows():
name, url = show
gui.addDir(name.encode('utf-8'), 'view_podcast_show', url)
INTERFACE.addDir(name.encode('utf-8'), 'view_podcast_show', url)

def podcast_show(url, name):
for recording in scraper.get_podcast_episodes(url):
date, url = recording
title = "{0} - {1}".format(name, date.encode('utf-8'))
gui.addItem(title, 'play_podcast', url.encode('utf-8'))
INTERFACE.addItem(title, 'play_podcast', url.encode('utf-8'))
22 changes: 11 additions & 11 deletions plugin.video.sarpur/sarpur/cached.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import os
import json
import sarpur
import scraper
import sarpur.scraper as scraper
from datetime import datetime


DATA_PATH = os.path.join(sarpur.ADDON.getAddonInfo('path'), 'resources','data')
SHOWTREEFILE_LOCATION = os.path.join(DATA_PATH,'showtree.dat')
TABFILE_LOCATION = os.path.join(DATA_PATH,'tabs.dat')
DATA_PATH = os.path.join(sarpur.ADDON.getAddonInfo('path'), 'resources', 'data')
SHOWTREEFILE_LOCATION = os.path.join(DATA_PATH, 'showtree.dat')
TABFILE_LOCATION = os.path.join(DATA_PATH, 'tabs.dat')

class Categories():
class Categories(object):
def __init__(self):

try:
Expand All @@ -23,8 +23,8 @@ def __init__(self):
showtree = self.update_showtree()
tabs = self.update_tabs()
else:
tabs = json.load(file(TABFILE_LOCATION,'rb'))
showtree = json.load(file(SHOWTREEFILE_LOCATION,'rb'))
tabs = json.load(file(TABFILE_LOCATION, 'rb'))
showtree = json.load(file(SHOWTREEFILE_LOCATION, 'rb'))

except OSError:
if not os.path.exists(DATA_PATH):
Expand All @@ -48,8 +48,8 @@ def update_tabs(self):

tabs = scraper.get_tabs()

json.dump(tabs, file(TABFILE_LOCATION,'wb'))
return tabs
json.dump(tabs, file(TABFILE_LOCATION, 'wb'))
self.tabs = tabs


def update_showtree(self):
Expand Down Expand Up @@ -80,5 +80,5 @@ def update_showtree(self):
"""

showtree = scraper.get_showtree()
json.dump(showtree, file(SHOWTREEFILE_LOCATION,'wb'))
return showtree
json.dump(showtree, file(SHOWTREEFILE_LOCATION, 'wb'))
self.showtree = showtree
39 changes: 20 additions & 19 deletions plugin.video.sarpur/sarpur/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

import requests, re
from html5lib import treebuilders
from xml.etree import cElementTree
from xml.etree import ElementTree


def get_document(url):
r = requests.get(url)
source = r.content
req = requests.get(url)
source = req.content

tb = treebuilders.getTreeBuilder("etree", cElementTree)
builder = treebuilders.getTreeBuilder("etree", ElementTree)
doc = html5lib.parse(source,
treebuilder=tb,
treebuilder=builder,
namespaceHTMLElements=False)

return doc
Expand All @@ -23,16 +23,16 @@ def get_episodes(url):
doc = get_document(url)

#Generic look
for ep in doc.xpath("//a[contains(@title, 'Spila')]"):
episodes.append((ep.text, ep.get('href')))
for episode in doc.xpath("//a[contains(@title, 'Spila')]"):
episodes.append((episode.text, episode.get('href')))

if episodes:
return episodes

#"Special" page
for ep in doc.xpath("//div[contains(@class,'mm-mynd')]"):
episode_date = ep.getparent().find('span').text
url = u'http://www.ruv.is{0}'.format(ep.find('a').attrib.get('href'))
for episode in doc.xpath("//div[contains(@class,'mm-mynd')]"):
episode_date = episode.getparent().find('span').text
url = u'http://www.ruv.is{0}'.format(episode.find('a').attrib.get('href'))
episodes.append((episode_date, url))

return episodes
Expand All @@ -42,8 +42,8 @@ def get_tabs():
xpathstring = "//div[@class='menu-block-ctools-menu-sarpsmynd-1 menu-name-menu-sarpsmynd parent-mlid-_active:0 menu-level-2']/ul/li/a"
tabs = []

for a in doc.xpath(xpathstring):
tabs.append((a.text, a.get('href')))
for hyperlink in doc.xpath(xpathstring):
tabs.append((hyperlink.text, hyperlink.get('href')))

return tabs

Expand All @@ -57,7 +57,8 @@ def get_showtree():

for group in channel.find("div").iterchildren():
if group.tag == 'h2':
showtree[i]["categories"].append({"name":group.text, "shows":[]})
showtree[i]["categories"].append(
{"name":group.text, "shows":[]})
elif group.tag == 'div':
for show in group.findall("div"):
hyperlink = show.find("a")
Expand All @@ -82,12 +83,12 @@ def get_stream_info(page_url):
else: #RÁS 1 & 2
# The ip address of the stream server is returned in another page
cache_url = doc.xpath("//script[contains(@src, 'load.cache.is')]")[0].get('src')
cache_content = res = requests.get(cache_url)
res = requests.get(cache_url)
cache_ip = re.search('"([^"]+)"', res.content).group(1)

# Now that we have the ip address we can insert it into the URL
source_js = doc.xpath("//script[contains(., 'tengipunktur')]")[0].text
source_url = re.search("'file': '(http://' \+ tengipunktur \+ '[^']+)", source_js).group(1)
source_url = re.search(r"'file': '(http://' \+ tengipunktur \+ '[^']+)", source_js).group(1)

rtmp_url = source_url.replace("' + tengipunktur + '", cache_ip)

Expand Down Expand Up @@ -133,13 +134,13 @@ def get_podcast_episodes(url):

for item in doc.findall("//guid"):
url = item.text
for el in item.itersiblings():
if el.tag == 'pubdate':
date = el.text
for element in item.itersiblings():
if element.tag == 'pubdate':
date = element.text

#date = item.xpath('pubdate')[0].text
#url = item.xpath('guid')[0].text
episodes.append((date,url))
episodes.append((date, url))

return episodes

Expand Down
36 changes: 24 additions & 12 deletions plugin.video.sarpur/util/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@ def _addDir(self, name, action_key, action_value, iconimage, is_folder):

url = "{base_url}?action_key={key}&action_value={value}&name={name}".format(**formatparams)

listitem = xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage='')
listitem.setInfo(type="Video", infoLabels={ "Title": name } )
listitem = xbmcgui.ListItem(name,
iconImage=iconimage,
thumbnailImage='')
listitem.setInfo(type="Video", infoLabels={"Title": name})

xbmcplugin.addDirectoryItem(
handle = self.addon_handle,
url = url,
listitem = listitem,
isFolder = is_folder)

def addDir(self, name, action_key, action_value, iconimage='DefaultFolder.png'):
self._addDir(name, action_key, action_value, iconimage, is_folder=True)

def addItem(self, name, action_key, action_value, iconimage='DefaultMovies.png'):
self._addDir(name, action_key, action_value, iconimage, is_folder=False)
handle=self.addon_handle,
url=url,
listitem=listitem,
isFolder=is_folder)

def addDir(self, name, action_key, action_value,
iconimage='DefaultFolder.png'):
self._addDir(name,
action_key,
action_value,
iconimage,
is_folder=True)

def addItem(self, name, action_key, action_value,
iconimage='DefaultMovies.png'):
self._addDir(name,
action_key,
action_value,
iconimage,
is_folder=False)

def infobox(self, title, message):
xbmcgui.Dialog().ok(title, message)
Expand Down

0 comments on commit 8e0fafb

Please sign in to comment.