[6c2c92]: plugin.video.vine / utils.py  Maximize  Restore  History

Download this file

149 lines (112 with data), 4.1 kB

  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
import re
import urllib2
from time import mktime,strptime
import xbmc
from xbmc import log
# Only used if we fail to parse the URL from the website
__SwfPlayerDefault__ = 'http://www.channel4.com/static/programmes/asset/flash/swf/4odplayer-11.34.1.swf'
__Aug12__ = mktime(strptime("12 Aug 2012", "%d %b %Y"))
# Return true if date is later than 12 Aug 2012
def isRecentDate(dateString):
try:
dateCompare = mktime(strptime(dateString, "%d %b %Y"))
if dateCompare > __Aug12__:
return True
except ( Exception ) as e:
if dateString is None:
dateString = 'None'
xbmc.log ( 'Exception: ' + str(e) + ', dateString: ' + str(dateString), xbmc.LOGERROR )
return False
def log(msg, level = xbmc.LOGNOTICE, method = None):
try:
if method is None:
method = inspect.stack()[1][3]
if isinstance(msg, unicode):
xbmc.log((u"%s : '%s'" % (method, msg)).encode('utf8'), level)
else:
xbmc.log(to_unicode((u"%s : '%s'" % (method, msg))).encode('utf8'), level)
except ( Exception ) as e:
xbmc.log(u"FALLBACK %s : '%s'" % (method, repr(msg)), level)
def findString(method, pattern, string, flags = (re.DOTALL | re.IGNORECASE)):
match = ( pattern, string, flags )
#match = None ###
if match is not None:
return (match.group(1))
return None
# Limit logging of string to 1000 chars
limit = 1000
# Can't find pattern in string
messageLog = "\nPattern - \n%s\n\nString - \n%s\n\n" % (pattern, string[0:limit])
return (None, ErrorHandler(method, ErrorCodes.CANT_FIND_PATTERN_IN_STRING, messageLog))
def GetSwfPlayer( html ):
log (u"html size:" + str(len(html)), xbmc.LOGDEBUG)
try:
swfRoot = re.search(u'var swfRoot = \'(.*?)\'', html, re.DOTALL | re.IGNORECASE).group(1)
fourodPlayerFile = re.search(u'var fourodPlayerFile = \'(.*?)\'', html, re.DOTALL | re.IGNORECASE).group(1)
#TODO Find out how to get the "asset/flash/swf/" part dynamically
swfPlayer = u"http://www.channel4.com" + swfRoot + u"asset/flash/swf/" + fourodPlayerFile
# Resolve redirect, if any
req = urllib2.Request(swfPlayer)
res = urllib2.urlopen(req)
swfPlayer = res.geturl()
except (Exception) as e:
log (u"Exception resolving swfPlayer URL: " + str(e), xbmc.LOGWARNING)
log (u"Unable to determine swfPlayer URL. Using default: " + __SwfPlayerDefault__, xbmc.LOGWARNING)
swfPlayer = __SwfPlayerDefault__
return swfPlayer
def remove_leading_slash(data):
p = re.compile(r'/')
return p.sub('', data)
def remove_square_brackets(data):
p = re.compile(r' \[.*?\]')
return p.sub('', data)
def remove_brackets(data):
p = re.compile(r' \(.*?\)')
return p.sub('', data)
def remove_html_tags(data):
p = re.compile(r'<.*?>')
return p.sub('', data)
def remove_extra_spaces(data):
p = re.compile(r'\s+')
return p.sub(' ', data)
def replace_non_alphanum(data):
p = re.compile(r'[^0-9A-Za-z]+')
return p.sub('', data)
def valueIfDefined(value, name = None):
try:
if name is None:
return value
else:
return name + ": " + value
except NameError:
if name is None:
return ""
else:
return name + ": Not defined"
def isVariableDefined(variable):
try:
variable
return True
except NameError:
return False
def extractJSON(text):
start = text.index('(') + 1
end = text.rindex(')')
return text[start:end]
def drepr(x, sort = True, indent = 0):
if isinstance(x, dict):
r = '{\n'
for (key, value) in (sorted(x.items()) if sort else x.iteritems()):
r += (' ' * (indent + 4)) + repr(key) + ': '
r += drepr(value, sort, indent + 4) + ',\n'
r = r.rstrip(',\n') + '\n'
r += (' ' * indent) + '}'
elif hasattr(x, '__iter__'):
r = '[\n'
for value in (sorted(x) if sort else x):
r += (' ' * (indent + 4)) + drepr(value, sort, indent + 4) + ',\n'
r = r.rstrip(',\n') + '\n'
r += (' ' * indent) + ']'
else:
r = repr(x)
return r