Skip to content

Commit

Permalink
Fix broken program listings
Browse files Browse the repository at this point in the history
* Popular, latest, live

Fixes #151, #155
  • Loading branch information
linqcan committed Apr 4, 2016
1 parent c8d8a8b commit aed1f7d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 71 deletions.
6 changes: 3 additions & 3 deletions default.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
MODE_PROGRAM = "pr"
MODE_CLIPS = "clips"
MODE_LIVE_PROGRAMS = "live"
MODE_LATEST = "senaste"
MODE_LATEST = "latest"
MODE_LATEST_NEWS = 'news'
MODE_POPULAR = "populara"
MODE_LAST_CHANCE = "sista-chansen"
MODE_POPULAR = "popular"
MODE_LAST_CHANCE = "last_chance"
MODE_VIDEO = "video"
MODE_CATEGORIES = "categories"
MODE_CATEGORY = "ti"
Expand Down
107 changes: 39 additions & 68 deletions resources/lib/svt.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
import urllib
import requests

import helper
import CommonFunctions as common

BASE_URL = "http://svtplay.se"
API_URL = "http://svtplay.se/api/"

URL_A_TO_O = "/program"
URL_TO_SEARCH = "/sok?q="
Expand Down Expand Up @@ -314,74 +316,6 @@ def getClips(url):
"""
return getProgramItems(SECTION_LATEST_CLIPS, url)

def getItems(section, page):
"""
TODO Update py-doc!
"""
if not section:
print "Missing parameter section!"
return None

container = getPage("/"+section+"?embed=true"+"&sida="+str(page))

article_class = "[^\"']*play_videolist-element[^\"']*"
articles = parseDOM(container, "article", attrs = { "class" : article_class })
titles = parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-title")
plots = parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-description")
airtimes = parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-broadcasted")
if section == SECTION_LATEST_CLIPS:
airtimes = parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-published")
durations = parseDOM(container, "article", attrs = { "class" : article_class }, ret = "data-length")

if not articles:
helper.errorMsg("No articles found for section '"+section+"' !")
return None

# Check if "Nästa sida" exists, assume it is
next_page_exists = True
next_page_elem = parseDOM( container,
"div",
attrs={ "class" : "[^\"']*play_gridpage__pagination[^\"']*"})
if not next_page_elem:
next_page_exists = False

new_articles = []
for index, article in enumerate(articles):
info = {}
new_article = {}
plot = plots[index]
aired = airtimes[index]
duration = durations[index]
title = titles[index]
new_article["url"] = parseDOM(article, "a",
attrs = { "class": "[^\"']*play_videolist-element__link[^\"']*" },
ret = "href")[0]
thumbnail = parseDOM(article,
"img",
attrs = { "class": "[^\"']*play_videolist-element__thumbnail-image[^\"']*" },
ret = "data-imagename")[0]
new_article["thumbnail"] = helper.prepareThumb(thumbnail, baseUrl=None)
if section == SECTION_LIVE_PROGRAMS:
notlive = parseDOM(article,
"span",
attrs = {"class": "[^\"']*play_graphics-live[^\"']*is-inactive[^\"']*"})
if notlive:
new_article["live"] = False
else:
new_article["live"] = True
title = common.replaceHTMLCodes(title)
plot = common.replaceHTMLCodes(plot)
new_article["title"] = title
info["title"] = title
info["plot"] = plot
info["aired"] = helper.convertDate(aired)
info["duration"] = helper.convertDuration(duration)
info["fanart"] = helper.prepareFanart(thumbnail, baseUrl=BASE_URL)
new_article["info"] = info
new_articles.append(new_article)

return (new_articles, next_page_exists)

def getProgramItems(section_name, url=None):
"""
Returns a list of program items for a show.
Expand Down Expand Up @@ -448,6 +382,43 @@ def getProgramItems(section_name, url=None):

return new_articles

def getItems(section_name, page):
if not page:
page = 1
url = API_URL+section_name+"_page"+";sida="+str(page)
r = requests.get(url)
if r.status_code != 200:
common.log("Did not get any response for: "+url)

returned_items = []
contents = r.json()
for video in contents["videos"]:
item = {}
item["title"] = video["programTitle"]
item["url"] = video["contentUrl"]
item["thumbnail"] = helper.prepareThumb(video["thumbnailLarge"], baseUrl=BASE_URL)
info = {}
info["title"] = item["title"]
try:
info["plot"] = video["description"]
except KeyError:
# Some videos do not have description (Rapport etc)
info["plot"] = ""
info["aired"] = video["broadcastDate"]
try:
info["duration"] = video["materialLength"]
except KeyError:
# Some programs are missing duration, default to 0
info["duration"] = 0
try:
info["fanart"] = helper.prepareFanart(video["posterXL"], baseUrl=BASE_URL)
except KeyError:
# Some programs do not have posters
info["fanart"] = ""
item["info"] = info
returned_items.append(item)

return (returned_items, contents["hasNextPage"])

def getPage(url):
"""
Expand Down

8 comments on commit aed1f7d

@linqcan
Copy link
Collaborator Author

@linqcan linqcan commented on aed1f7d Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #155 too.

@tomtefan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaks the plugin for me:
21:20:38 T:2539600704 ERROR: EXCEPTION Thrown (PythonToCppException) : -->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <type 'exceptions.RuntimeError'>
Error Contents: osrandom engine already registered
Traceback (most recent call last):
File ".kodi/addons/plugin.video.svtplay/default.py", line 14, in
import resources.lib.svt as svt
File ".kodi/addons/plugin.video.svtplay/resources/lib/svt.py", line 3, in
import requests
File "/usr/lib/python2.7/dist-packages/requests/init.py", line 53, in
from .packages.urllib3.contrib import pyopenssl
File "/usr/lib/python2.7/dist-packages/urllib3/contrib/pyopenssl.py", line 53, in
import OpenSSL.SSL
File "/usr/lib/python2.7/dist-packages/OpenSSL/init.py", line 8, in
from OpenSSL import rand, crypto, SSL
File "/usr/lib/python2.7/dist-packages/OpenSSL/rand.py", line 11, in
from OpenSSL._util import (
File "/usr/lib/python2.7/dist-packages/OpenSSL/_util.py", line 7, in
binding = Binding()
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/bindings/openssl/binding.py", line 63, in init
self._ensure_ffi_initialized()
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/bindings/openssl/binding.py", line 95, in _ensure_ffi_initialized
cls._register_osrandom_engine()
File "/usr/lib/python2.7/dist-packages/cryptography/hazmat/bindings/openssl/binding.py", line 70, in _register_osrandom_engine
raise RuntimeError("osrandom engine already registered")
RuntimeError: osrandom engine already registered
-->End of Python script error report<--
21:20:38 T:2961815488 ERROR: GetDirectory - Error getting plugin://plugin.video.svtplay/

@linqcan
Copy link
Collaborator Author

@linqcan linqcan commented on aed1f7d Apr 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a local install issue. I have added the Kodi version of requests now. Would really appreciate if you could try the latest version 😄

@tomtefan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No dice, seems to be an issue with cryptography
pyca/cryptography#2287

@tomtefan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it to work now, both with the patch and without.

Thanks

@linqcan
Copy link
Collaborator Author

@linqcan linqcan commented on aed1f7d Apr 5, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomtefan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stack trace looked the same as before.

I uninstalled the python-cryptography package and re-installed it.

@linqcan
Copy link
Collaborator Author

@linqcan linqcan commented on aed1f7d Apr 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, lets see how this pan out then. Did a PR for version 4.0.25.
xbmc/repo-plugins#425

Please sign in to comment.