-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefault.py
executable file
·317 lines (277 loc) · 11.8 KB
/
default.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import sys
import xbmc, xbmcgui, xbmcplugin
import urllib2
import urllib
import CommonFunctions
common = CommonFunctions
from t0mm0.common.addon import Addon
from metahandler import metahandlers
addon = Addon('plugin.video.1channel', sys.argv)
from bs4 import BeautifulSoup
import soupselect; soupselect.monkeypatch()
import re
import feedparser
import json
#sys.path.append("./xbmc_env/bin/python2.5")
#sys.path.append("./xbmc_env/lib/python2.5")
#sys.path.append("./xbmc_env/lib/python2.5/site-packages")
# plugin modes
MODE_FIRST = 10
MODE_SECOND = 20
MODE_SEARCH = 30
MODE_RESULT = 40
# parameter keys
PARAMETER_KEY_MODE = "mode"
# menu item names
FIRST_SUBMENU = "First Submenu"
SECOND_SUBMENU = "Second Submenu"
# plugin handle
handle = int(sys.argv[1])
#patterns
YOURUPLOAD = re.compile("content.*?(http:.*?\.mp4)")
SAPO = re.compile("url:\s+'(http:.*?\.mp4)'")
MP4UPLOAD = re.compile("(http:.*?:182/.*?\.mp4)")
UPLOADC = re.compile("(http:.*?:182/.*?\.mp4)")
ENGINE = re.compile("url:\s+'(http.*?mp4.*?)'")
VIDEOWEED = re.compile("advURL.*?(http.*?file.*?)\"")
VIDEONEST = re.compile("(http.*?mp4)")
UPLOAD2 = re.compile("video=(http.*?mp4.*?)\&")
RUTUBE = re.compile("file=(http.*?iflv)")
VIDEOEMBED = re.compile("div class=\"videoembed activeembed\".*?src=\"(http.*?)\"")
# utility functions
def parameters_string_to_dict(parameters):
''' Convert parameters encoded in a URL to a dict. '''
paramDict = {}
if parameters:
paramPairs = parameters[1:].split("&")
for paramsPair in paramPairs:
paramSplits = paramsPair.split('=')
if (len(paramSplits)) == 2:
paramDict[paramSplits[0]] = paramSplits[1]
return paramDict
def addDirectoryItem(name, isFolder=True, parameters={}, listitem = False):
''' Add a list item to the XBMC UI.'''
if (listitem):
li = listitem
else:
li = xbmcgui.ListItem(name)
url = sys.argv[0] + '?' + urllib.urlencode(parameters)
return xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=li, isFolder=isFolder)
def addLink(name,url):
ok=True
liz=xbmcgui.ListItem(name)
liz.setInfo( type="video", infoLabels = {
'title' : name
})
ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=liz)
return ok
def fetchUrl(url):
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
html=response.read()
response.close()
return html
'''
XBMC plays mp4
'''
def get_video_url(provider_name, url):
html = fetchUrl(url)
match = False
video_url = ''
if provider_name == 'rutube':
match=RUTUBE.search(html)
else:
try:
provider_page_url = VIDEOEMBED.search(html).group(1)
html = fetchUrl(provider_page_url)
if (provider_name == 'sapo'):
match=SAPO.search(html)
elif (provider_name == 'yourupload'):
match=YOURUPLOAD.search(html)
elif (provider_name == 'uploadc'):
match=UPLOADC.search(html)
elif (provider_name == 'upload2'):
match=UPLOAD2.search(html)
elif (provider_name == 'mp4upload'):
match=MP4UPLOAD.search(html)
#elif (provider_name == 'videoweed'):
# match=VIDEOWEED.search(html)
elif (provider_name == 'engine'):
match=ENGINE.search(html)
elif (provider_name == 'videonest'):
match=VIDEONEST.search(html)
else:
try:
print "AD-TV: unknown provider - "+provider_page_url+"\n"+html
except:
print "AD-TV: unknown provider - "+provider_page_url+"\n"+repr(html)
match=VIDEONEST.search(html)
except Exception as inst:
print "AD-TV: parsing problem - \n"+html, inst
if(match):
video_url = match.group(1)
if (provider_name == 'engine'):
video_url = urllib.unquote(video_url)
return video_url
def get_video_providers(url):
html = fetchUrl(url)
soup = BeautifulSoup(html)
generic_video_items = soup.findSelect('div.generic-video-item')
result = []
for item in generic_video_items:
ssoup = BeautifulSoup(str(item.contents))
video_page = ssoup.findSelect('div.thumb')
if (not video_page[0].a):
provider_url = url
temp = BeautifulSoup(str(video_page[0].contents))
span = temp.findAll('span')
provider_name = span[1].string
else:
provider_name = video_page[0].a.center.span.string
provider_url = video_page[0].a['href']
data = {'provider_name': provider_name,
'provider_url': provider_url}
result.append(data)
return result
# UI builder functions
def show_root_menu():
''' Show the plugin root menu. '''
'''addDirectoryItem(name=FIRST_SUBMENU, parameters={ PARAMETER_KEY_MODE: MODE_FIRST }, isFolder=True)
addDirectoryItem(name=SECOND_SUBMENU, parameters={ PARAMETER_KEY_MODE: MODE_SECOND }, isFolder=True)
'''
addDirectoryItem(' - Search - ', isFolder=True, parameters ={'mode': MODE_SEARCH})
#d = feedparser.parse('http://www.animedreaming.tv/rss.xml')
#for i in d.entries:
entries = get_latest_episodes()
for i in entries:
name = i["title"]
link = i["link"]
meta = create_meta('tvshow', re.sub('episode\s\d+','', name), False, '')
listitem = xbmcgui.ListItem(name, iconImage=meta['cover_url'], thumbnailImage=meta['cover_url'])
listitem.setInfo('video', meta)
fanart = ''
try: fanart = meta['backdrop_url']
except: pass
listitem.setProperty('fanart_image', fanart)
addDirectoryItem(name, isFolder=True, parameters ={'link': link, 'mode': MODE_FIRST}, listitem=listitem)
#video_url = get_video_url(link)
#if (video_url):
# addLink(name, video_url)
xbmcplugin.endOfDirectory(handle=handle, succeeded=True)
def get_latest_episodes():
json_episodes = fetchUrl("""http://query.yahooapis.com/v1/public/yql?q=use%0A'https%3A%2F%2Fraw.github.com%2Fneofreko%2Fcss2xpath%2Fmaster%2Fdata.html.cssselect.xml'%20as%20data.html.cssselect%3B%0Aselect%20*%20from%20data.html.cssselect%20where%20url%3D%22http%3A%2F%2Fwww.animedreaming.tv%22%20and%20css%3D'%23latest_holder%20.episode_div%20a'&format=json&callback=""")
json_titles = fetchUrl("""http://query.yahooapis.com/v1/public/yql?q=use%0A'https%3A%2F%2Fraw.github.com%2Fneofreko%2Fcss2xpath%2Fmaster%2Fdata.html.cssselect.xml'%20as%20data.html.cssselect%3B%0Aselect%20*%20from%20data.html.cssselect%20where%20url%3D%22http%3A%2F%2Fwww.animedreaming.tv%22%20and%20css%3D'%23latest_holder%20.latest_title%20a'&format=json&callback=""")
print json_episodes
print json_titles
episodes = json.loads(json_episodes)
titles = json.loads(json_titles)
result = []
i=0
titles_result = titles["query"]["results"]["results"]["a"]
episodes_result = episodes["query"]["results"]["results"]["a"]
print episodes_result
episode_regex = re.compile("episode-(\d+)")
for link in titles_result:
title = link["content"]
episode_url = "http://www.animedreaming.tv/%s" % episodes_result[i]["href"]
episode_number = episode_regex.search(episode_url)
if (episode_number):
title = "%s episode %s" % (title, episode_number.group(1))
i=i+1
obj = {'link': episode_url, 'title': title}
result.append(obj)
return result
def show_first_submenu():
''' Show first submenu. '''
params = parameters_string_to_dict(sys.argv[2])
link = params.get('link', "")
providers = get_video_providers(urllib.unquote(link))
for provider in providers:
print "processing %s from %s" % (provider['provider_name'], provider['provider_url'])
video_url = get_video_url(provider['provider_name'], provider['provider_url'])
print video_url
if (video_url):
addLink("Play from %s" % provider['provider_name'], video_url)
#addDirectoryItem(provider['provider_name'], isFolder=True, parameters ={'link': provider['provider_url'], 'mode': MODE_SECOND})
#addLink()
xbmcplugin.endOfDirectory(handle=handle, succeeded=True)
def show_second_submenu():
''' Show second submenu. '''
params = parameters_string_to_dict(sys.argv[2])
link = params.get('link', "")
video_url = get_video_url(urllib.unquote(link))
if (video_url):
addLink("Play from %s" % name, video_url)
xbmcplugin.endOfDirectory(handle=handle, succeeded=True)
def getSearch():
query = common.getUserInput('Search', '')
if not query:
return False
html = fetchUrl('http://www.animedreaming.tv/search.php?searchquery='+urllib.quote_plus(query))
for link in re.compile('<li.*?href="(http://www.animedreaming.tv/.*?)">(.*?)</a>').findall(html):
title = re.sub('<b>|</b>','*',link[1])
meta = create_meta('tvshow', title, False, '')
listitem = xbmcgui.ListItem(meta['title'], iconImage=meta['cover_url'], thumbnailImage=meta['cover_url'])
listitem.setInfo('video', meta)
fanart = ''
try: fanart = meta['backdrop_url']
except: pass
listitem.setProperty('fanart_image', fanart)
addDirectoryItem(title, isFolder=True, parameters ={'link': link[0], 'mode': MODE_RESULT}, listitem=listitem)
xbmcplugin.endOfDirectory(handle=handle, succeeded=True)
def parseResults():
params = parameters_string_to_dict(sys.argv[2])
link = params.get('link', "")
html = fetchUrl(urllib.unquote(link))
for link in re.compile('<li.*?href="(http://www.animedreaming.tv/.*?)".*?>(.*?) <').findall(html):
addDirectoryItem(link[1], isFolder=True, parameters ={'link': link[0], 'mode': MODE_FIRST})
xbmcplugin.endOfDirectory(handle=handle, succeeded=True)
def create_meta(video_type, title, year, thumb):
metaget=metahandlers.MetaData()
try: year = int(year)
except: year = 0
year = str(year)
meta = {'title':title, 'year':year, 'imdb_id':'', 'overlay':''}
meta['imdb_id'] = ''
#if META_ON:
try:
if video_type == 'tvshow':
meta = metaget.get_meta(video_type, title)
if not (meta['imdb_id'] or meta['tvdb_id']):
meta = metaget.get_meta(video_type, title, year=year)
alt_id = meta['tvdb_id']
###Temporary work around. t0mm0.common isn't happy with the episode key being a str
meta['episode'] = int(meta['episode'])
meta['rating'] = float(meta['rating'])
else: #movie
meta = metaget.get_meta(video_type, title, year=year)
alt_id = meta['tmdb_id']
'''
if video_type == 'tvshow' and not USE_POSTERS:
meta['cover_url'] = meta['banner_url']
if POSTERS_FALLBACK and meta['cover_url'] in ('/images/noposter.jpg',''):
meta['cover_url'] = thumb
img = meta['cover_url']
'''
# except: addon.log('Error assigning meta data for %s %s %s' %(video_type, title, year))
except: print 'Error assigning meta data for %s %s %s' %(video_type, title, year)
return meta
# parameter values
params = parameters_string_to_dict(sys.argv[2])
mode = int(params.get(PARAMETER_KEY_MODE, "0"))
print "##########################################################"
print("Mode: %s" % mode)
print "##########################################################"
# Depending on the mode, call the appropriate function to build the UI.
if not sys.argv[2]:
# new start
ok = show_root_menu()
elif mode == MODE_FIRST:
ok = show_first_submenu()
elif mode == MODE_SECOND:
ok = show_second_submenu()
elif mode == MODE_SEARCH:
ok = getSearch()
elif mode == MODE_RESULT:
ok = parseResults()