Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Björling committed Jul 28, 2017
1 parent 0629952 commit 9442f4f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
38 changes: 16 additions & 22 deletions addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
import xbmcgui
import xbmcplugin
import resources.lib.svtoa as svtoa
import resources.lib.utils as utils

# default parameters
# parse arguments from kodi
DEFAULT = {'page': ['root'],
'genre': [''],
'program': [''],
'letter': [''],
}

ABC = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') + ['Å', 'Ä', 'Ö']

# parse arguments from kodi
base_url = sys.argv[0]
addon_handle = int(sys.argv[1])
newargs = urlparse.parse_qs(sys.argv[2][1:])
args = DEFAULT.copy()
args.update(newargs)

# constants for page identification
# set content type for the addon
xbmcplugin.setContent(addon_handle, 'movies')

# constants
ABC = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') + ['Å', 'Ä', 'Ö']
PAGE_ROOT = 'root' # front page
PAGE_ABC = 'abc' # page which lists all starting letters
PAGE_PROGRAMS = 'programs' # program title page for a given starting letter
Expand All @@ -32,44 +33,36 @@
PAGE_SEARCH = 'search' # search page
PAGE_PROGRAM = 'program' # page which lists all videos/episodes for a given program

# set content type for the addon
xbmcplugin.setContent(addon_handle, 'movies')

def build_url(query):
""" Helper which builds a url query from a dict.
"""
return base_url + '?' + urllib.urlencode(query)

# The following describes the pages of the addon. Each time this script
# is executed, one of the PAGE_ constants is supplied in the arguments.
page = args['page'][0]
if page == PAGE_ROOT:
# item for the Program interface
url = build_url({'page': PAGE_ABC})
url = utils.build_url(base_url, {'page': PAGE_ABC})
li = xbmcgui.ListItem('Program A-Ö', iconImage='DefaultFile.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
# item for the Genres interface
url = build_url({'page': PAGE_GENRES})
url = utils.build_url(base_url, {'page': PAGE_GENRES})
li = xbmcgui.ListItem('Genrer', iconImage='DefaultFolder.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
# item for the Search interface
url = build_url({'page': PAGE_SEARCH})
url = utils.build_url(base_url, {'page': PAGE_SEARCH})
li = xbmcgui.ListItem('Sök', iconImage='DefaultSearch.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
xbmcplugin.endOfDirectory(addon_handle)

elif page == PAGE_ABC:
# special case for 0-9
url = build_url({'page': 'programs', 'letter': '0'})
url = utils.build_url(base_url, {'page': 'programs', 'letter': '0'})
li = xbmcgui.ListItem('0-9')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
# the rest of the alphabet
for letter in ABC:
url = build_url({'page': 'programs', 'letter': letter})
url = utils.build_url(base_url, {'page': 'programs', 'letter': letter})
li = xbmcgui.ListItem(letter)
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
Expand All @@ -81,7 +74,7 @@ def build_url(query):
for item in programs:
# comparison which takes care of ÅÄÖ
if item.name.upper().startswith(letter.upper()) or (letter == '0' and item.name[0].upper() not in ABC and item.name[:2].upper() not in ABC):
url = build_url({'page': 'program', 'program': item.url})
url = utils.build_url(base_url, {'page': 'program', 'program': item.url})
image = svtoa.getProgramImage(item.url)
li = xbmcgui.ListItem(item.name, iconImage=image)
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
Expand All @@ -91,7 +84,7 @@ def build_url(query):
elif page == PAGE_GENRES:
genres = svtoa.getGenres()
for item in genres:
url = build_url({'page': 'genre', 'genre': urllib.quote(item.name)})
url = utils.build_url(base_url, {'page': 'genre', 'genre': urllib.quote(item.name)})
li = xbmcgui.ListItem(item.name, iconImage=item.image)
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
Expand All @@ -101,7 +94,7 @@ def build_url(query):
genre = args['genre'][0]
programs = svtoa.getProgramsByGenre(genre)
for item in programs:
url = build_url({'page': 'program', 'program': item.url})
url = utils.build_url(base_url, {'page': 'program', 'program': item.url})
li = xbmcgui.ListItem(item.name, iconImage=item.image)
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
listitem=li, isFolder=True)
Expand All @@ -111,6 +104,7 @@ def build_url(query):
program = args['program'][0]
videos = svtoa.getVideosByProgram(program)
for item in videos:
### TODO: add additional episode data
li = xbmcgui.ListItem(item.name, iconImage=item.image)
xbmcplugin.addDirectoryItem(handle=addon_handle, url=item.url, listitem=li)
xbmcplugin.endOfDirectory(addon_handle)
Expand Down
8 changes: 8 additions & 0 deletions resources/lib/svtoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,13 @@ def getVideosByProgram(program):
if vid['playerType'] == 'ios':
item_.url = vid['url']
item_.image = entry['thumbnailMedium']
# add extra metadata to the info attribute
try:
season = entry['seasonNumber']
episode = entry['episodeNumber']
episodes = entry['totalEpisodes']
item_.info += 'Säsong %d, avsnitt %d/%d.' % (season, episode, episodes)
except:
pass
items.append(item_)
return items
8 changes: 8 additions & 0 deletions resources/lib/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-

import urllib

def build_url(base_url, query):
""" Helper which builds a url query from a dict.
"""
return base_url + '?' + urllib.urlencode(query)

0 comments on commit 9442f4f

Please sign in to comment.