forked from markhoney/plugin.video.nz.ondemand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
153 lines (133 loc) · 5.19 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Generic functions
import sys, re
from xml.dom import minidom
from xml.parsers.expat import ExpatError
def message(message, title = "Warning"): #Show an on-screen message (useful for debugging)
import xbmcgui
dialog = xbmcgui.Dialog()
if message:
if message <> "":
dialog.ok(title, message)
else:
dialog.ok("Message", "Empty message text")
else:
dialog.ok("Message", "No message text")
def gethtmlpage(url, useragent = "ie9"): #Grab an HTML page
import urllib2
print "Requesting URL: %s" % (url)
req = urllib2.Request(url)
newheader = 'Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)'
if useragent == "ps3":
newheader = 'Mozilla/5.0 (PLAYSTATION 3; 3.55)'
req.add_header('User-agent', newheader)
try:
response = urllib2.urlopen(req)
doc = response.read()
response.close()
return doc
except urllib2.HTTPError, err:
print "urllib2.HTTPError requesting URL: %s" % (err.code)
pass
def getxmldocument(s):
try:
document = minidom.parseString(s)
if document:
return document.documentElement
except ExpatError: # Thrown if the content contains just the <xml> tag and no actual content. Some of the TVNZ .xml files are like this :(
pass
def unescape(s): #Convert escaped HTML characters back to native unicode, e.g. > to > and " to "
from htmlentitydefs import name2codepoint
return re.sub('&(%s);' % '|'.join(name2codepoint), lambda m: unichr(name2codepoint[m.group(1)]), s)
def checkdict(info, items): #Check that all of the list "items" are in the dictionary "info"
for item in items:
if info.get(item, "##unlikelyphrase##") == "##unlikelyphrase##":
sys.stderr.write("Dictionary missing item: %s" % (item))
return 0
return 1
# Metadata
def defaultinfo(folder = 0): #Set the default info for folders (1) and videos (0). Most options have been hashed out as they don't show up in the list and are grabbed from the media by the player
info = dict()
if folder:
info["Icon"] = "DefaultFolder.png"
else:
info["Icon"] = "DefaultVideo.png"
#info["VideoCodec"] = "flv"
#info["VideoCodec"] = "avc1"
#info["VideoCodec"] = "h264"
#info["VideoResolution"] = "480" #actually 360 (640x360)
#info["VideoAspect"] = "1.78"
#info["AudioCodec"] = "aac"
#info["AudioChannels"] = "2"
#info["AudioLanguage"] = "eng"
info["Thumb"] = ""
return info
def xbmcdate(inputdate): #Convert a date in "%d/%m/%y" format to an XBMC friendly format
import time, xbmc
return time.strftime(xbmc.getRegion( "datelong" ).replace( "DDDD,", "" ).replace( "MMMM", "%B" ).replace( "D", "%d" ).replace( "YYYY", "%Y" ).strip(), time.strptime(inputdate,"%d/%m/%y"))
def imageinfo(image): #Search an image for its HREF
if image:
info = dict()
info["Thumb"] = image['src']
#alttitle = image['title']
return info
def itemtitle(Title, PlotOutline): #Build a nice title from the program title and sub-title (given as PlotOutline)
if PlotOutline:
Title = "%s - %s" % (Title, PlotOutline)
return Title
# URL manipulation
def constructStackURL(playlist): #Build a URL stack from multiple URLs for the XBMC player
uri = ""
for url in playlist:
url.replace(',',',,')
if len(uri)>0:
uri = uri + " , " + url
else:
uri = "stack://" + url
return(uri)
# XBMC Manipulation
def addlistitems(id, infoarray, fanart = "fanart.jpg", folder = 0, path = ""):
total = len(infoarray)
#total = len(infoarray.viewkeys())
i = 0
#for listitem in infoarray:
for listkey, listitem in infoarray.items():
#listitem["Count"] = i
i += 1
addlistitem(id, listitem, fanart, folder, total, path)
def addlistitem(id, info, fanart = "fanart.jpg", folder = 0, total = 0, path = ""): #Add a list item (media file or folder) to the XBMC page
import xbmcgui, xbmcplugin, xbmcaddon, os
liz = xbmcgui.ListItem(info["Title"], iconImage = info["Icon"], thumbnailImage = info["Thumb"])
addon = xbmcaddon.Addon(id = sys.argv[0][9:-1])
liz.setProperty('fanart_image', os.path.join(addon.getAddonInfo('path'), fanart))
liz.setInfo(type = "Video", infoLabels = info)
if not folder:
liz.setProperty("IsPlayable", "true")
if path == "":
if xbmcplugin.addDirectoryItem(handle = id, url = info["FileName"], listitem = liz, isFolder = folder, totalItems = total):
return 1
else:
return 0
else:
liz.setPath(path)
try:
xbmcplugin.setResolvedUrl(handle = id, succeeded = True, listitem = liz)
except:
message("Boo, couldn't play.")
def addsorting(id, methods, mediacontent = ""):
import xbmcplugin
for method in methods:
if method == "unsorted":
xbmcplugin.addSortMethod(handle = id, sortMethod = xbmcplugin.SORT_METHOD_UNSORTED)
elif method == "count":
xbmcplugin.addSortMethod(handle = id, sortMethod = xbmcplugin.SORT_METHOD_PROGRAM_COUNT)
elif method == "date":
xbmcplugin.addSortMethod(handle = id, sortMethod = xbmcplugin.SORT_METHOD_DATE)
elif method == "label":
xbmcplugin.addSortMethod(handle = id, sortMethod = xbmcplugin.SORT_METHOD_LABEL)
elif method == "runtime":
xbmcplugin.addSortMethod(handle = id, sortMethod = xbmcplugin.SORT_METHOD_VIDEO_RUNTIME)
elif method == "episode":
xbmcplugin.addSortMethod(handle = id, sortMethod = xbmcplugin.SORT_METHOD_EPISODE)
if mediacontent <> "":
xbmcplugin.setContent(handle = id, content = mediacontent)
xbmcplugin.endOfDirectory(id)