forked from anxdpanic/plugin.video.twitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.py
252 lines (198 loc) · 8.14 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
# -*- coding: utf-8 -*-
from converter import JsonListItemConverter
from functools import wraps
from twitch import TwitchTV, TwitchVideoResolver, Keys, TwitchException
from xbmcswift2 import Plugin # @UnresolvedImport
import urllib2, json, sys
ITEMS_PER_PAGE = 20
LINE_LENGTH = 60
PLUGIN = Plugin()
CONVERTER = JsonListItemConverter(PLUGIN, LINE_LENGTH)
TWITCHTV = TwitchTV(PLUGIN.log)
def managedTwitchExceptions(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except TwitchException as error:
handleTwitchException(error)
return wrapper
def handleTwitchException(exception):
codeTranslations = {TwitchException.NO_STREAM_URL : 30023,
TwitchException.STREAM_OFFLINE : 30021,
TwitchException.HTTP_ERROR : 30020,
TwitchException.JSON_ERROR : 30027}
code = exception.code
title = 30010
msg = codeTranslations[code]
PLUGIN.notify(PLUGIN.get_string(title), PLUGIN.get_string(msg))
@PLUGIN.route('/')
def createMainListing():
items = [
{'label': PLUGIN.get_string(30005),
'path': PLUGIN.url_for(endpoint='createListOfFeaturedStreams')
},
{'label': PLUGIN.get_string(30001),
'path': PLUGIN.url_for(endpoint='createListOfGames', index='0')
},
{'label': PLUGIN.get_string(30008),
'path': PLUGIN.url_for(endpoint='createListOfChannels', index='0')
},
{'label': PLUGIN.get_string(30002),
'path': PLUGIN.url_for(endpoint='createFollowingList')
},
{'label': PLUGIN.get_string(30006),
'path': PLUGIN.url_for(endpoint='createListOfTeams', index='0')
},
{'label': PLUGIN.get_string(30003),
'path': PLUGIN.url_for(endpoint='search')
},
{'label': PLUGIN.get_string(30004),
'path': PLUGIN.url_for(endpoint='showSettings')
}
]
return items
@PLUGIN.route('/createListOfFeaturedStreams/')
@managedTwitchExceptions
def createListOfFeaturedStreams():
featuredStreams = TWITCHTV.getFeaturedStream()
return [CONVERTER.convertStreamToListItem(featuredStream[Keys.STREAM])
for featuredStream in featuredStreams]
@PLUGIN.route('/createListOfGames/<index>/')
@managedTwitchExceptions
def createListOfGames(index):
index, offset, limit = calculatePaginationValues(index)
games = TWITCHTV.getGames(offset, limit)
items = [CONVERTER.convertGameToListItem(element[Keys.GAME]) for element in games]
items.append(linkToNextPage('createListOfGames', index))
return items
@PLUGIN.route('/createListOfChannels/<index>/')
@managedTwitchExceptions
def createListOfChannels(index):
index, offset, limit = calculatePaginationValues(index)
items = [CONVERTER.convertStreamToListItem(stream) for stream
in TWITCHTV.getChannels(offset, limit)]
items.append(linkToNextPage('createListOfChannels', index))
return items
@PLUGIN.route('/createListForGame/<gameName>/<index>/')
@managedTwitchExceptions
def createListForGame(gameName, index):
index, offset, limit = calculatePaginationValues(index)
items = [CONVERTER.convertStreamToListItem(stream) for stream
in TWITCHTV.getGameStreams(gameName, offset, limit)]
items.append(linkToNextPage('createListForGame', index, gameName=gameName))
return items
@PLUGIN.route('/createFollowingList/')
@managedTwitchExceptions
def createFollowingList():
username = getUserName()
streams = TWITCHTV.getFollowingStreams(username)
liveStreams = [CONVERTER.convertStreamToListItem(stream) for stream in streams['live']]
liveStreams.insert(0,{'path': PLUGIN.url_for(endpoint='createFollowingList'), 'icon': u'', 'is_playable': False, 'label': PLUGIN.get_string(30012)})
liveStreams.append({'path': PLUGIN.url_for(endpoint='createFollowingList'), 'icon': u'', 'is_playable': False, 'label': PLUGIN.get_string(30013)})
liveStreams.extend([CONVERTER.convertFollowersToListItem(follower) for follower in streams['others']])
return liveStreams
@PLUGIN.route('/channelVideos/<name>/')
@managedTwitchExceptions
def channelVideos(name):
items = [
{'label': 'Past Broadcasts',
'path': PLUGIN.url_for(endpoint='channelVideosList', name=name, index=0, past='true')
},
{'label': 'Video Highlights',
'path': PLUGIN.url_for(endpoint='channelVideosList', name=name, index=0, past='false')
}
]
return items
@PLUGIN.route('/channelVideosList/<name>/<index>/<past>/')
@managedTwitchExceptions
def channelVideosList(name,index,past):
index = int(index)
offset = index * 8
videos = TWITCHTV.getFollowerVideos(name,offset,past)
items = [CONVERTER.convertVideoListToListItem(video) for video in videos[Keys.VIDEOS]]
if videos[Keys.TOTAL] > (offset + 8):
items.append(linkToNextPage('channelVideosList', index, name=name, past=past))
return items
@PLUGIN.route('/playVideo/<id>/')
@managedTwitchExceptions
def playVideo(id):
#Get Required Quality From Settings
videoQuality = getVideoQuality()
playlist = TWITCHTV.getVideoChunksPlaylist(id,videoQuality)
# Doesn't fullscreen video, might be because of xbmcswift
#xbmc.Player().play(playlist)
try:
# Gotta wrap this in a try/except, xbmcswift causes an error when passing a xbmc.PlayList()
# but still plays the playlist properly
xbmc.Player().play(playlist)
PLUGIN.set_resolved_url(playlist)
except:
pass
@PLUGIN.route('/search/')
@managedTwitchExceptions
def search():
query = PLUGIN.keyboard('', PLUGIN.get_string(30007))
if query:
target = PLUGIN.url_for(endpoint='searchresults', query=query, index='0')
else:
target = PLUGIN.url_for(endpoint='createMainListing')
PLUGIN.redirect(target)
@PLUGIN.route('/searchresults/<query>/<index>/')
@managedTwitchExceptions
def searchresults(query, index='0'):
index, offset, limit = calculatePaginationValues(index)
streams = TWITCHTV.searchStreams(query, offset, limit)
items = [CONVERTER.convertStreamToListItem(stream) for stream in streams]
items.append(linkToNextPage('searchresults', index, query=query))
return items
@PLUGIN.route('/showSettings/')
def showSettings():
#there is probably a better way to do this
PLUGIN.open_settings()
@PLUGIN.route('/playLive/<name>/')
@managedTwitchExceptions
def playLive(name):
#Get Required Quality From Settings
videoQuality = getVideoQuality()
plpath = xbmc.translatePath('special://temp') + 'hlsplaylist.m3u8'
resolver = TwitchVideoResolver(PLUGIN.log)
resolver.saveHLSToPlaylist(name,videoQuality,plpath)
#Play Custom Playlist
xbmc.Player().play(plpath)
PLUGIN.set_resolved_url(plpath)
@PLUGIN.route('/createListOfTeams/<index>/')
@managedTwitchExceptions
def createListOfTeams(index):
index = int(index)
teams = TWITCHTV.getTeams(index)
items = [CONVERTER.convertTeamToListItem(item)for item in teams]
if len(teams) == 25:
items.append(linkToNextPage('createListOfTeams', index))
return items
@PLUGIN.route('/createListOfTeamStreams/<team>/')
@managedTwitchExceptions
def createListOfTeamStreams(team):
return [CONVERTER.convertTeamChannelToListItem(channel[Keys.CHANNEL])
for channel in TWITCHTV.getTeamStreams(team)]
def calculatePaginationValues(index):
index = int(index)
limit = ITEMS_PER_PAGE
offset = index * limit
return index, offset, limit
def getUserName():
username = PLUGIN.get_setting('username', unicode).lower()
if not username:
PLUGIN.open_settings()
username = PLUGIN.get_setting('username', unicode).lower()
return username
def getVideoQuality():
chosenQuality = PLUGIN.get_setting('video', unicode)
qualities = {'0': 0, '1': 1, '2': 2, '3': 3, '4' : 4}
return qualities.get(chosenQuality, sys.maxint)
def linkToNextPage(target, currentIndex, **kwargs):
return {'label': PLUGIN.get_string(30011),
'path': PLUGIN.url_for(target, index=str(currentIndex + 1), **kwargs)
}
if __name__ == '__main__':
PLUGIN.run()