-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.py
executable file
·205 lines (180 loc) · 7.52 KB
/
addon.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
# -*- coding: utf-8 -*-
import sys
from urllib.parse import parse_qsl
import json
from resources.lib.kodihelper import KodiHelper
base_url = sys.argv[0]
handle = int(sys.argv[1])
helper = KodiHelper(base_url, handle)
def list_pages():
helper.add_item('Kaikki ohjelmat', params={'action': 'list_programs'})
helper.add_item('Kategoriat', params={'action': 'list_categories'})
helper.add_item('Live TV', params={'action': 'livetv'})
helper.add_item('Oma kanava', params={'action': 'favorites'})
helper.add_item('Haku', params={'action': 'search'}) # search
helper.eod()
def list_programs(category=None):
programs = helper.k.get_programs()
for program in programs:
if category:
if program['parentWonName'].encode('utf-8') == category:
title = program['title'].encode('utf-8')
params = {
'action': 'list_videos_or_subcats',
'program_id': program['id'],
'subcats': json.dumps(program['subs'])
}
helper.add_item(title, params)
else:
title = program['title'].encode('utf-8')
params = {
'action': 'list_videos_or_subcats',
'program_id': program['id'],
'subcats': json.dumps(program['subs'])
}
helper.add_item(title, params)
helper.eod()
def list_categories():
helper.add_item('Dokumentit ja asiaohjelmat', params={'action': 'list_programs', 'category': 'Dokumentit ja asiaohjelmat'})
helper.add_item('Elokuvat', params={'action': 'list_programs', 'category': 'Elokuvat'})
helper.add_item('Kotimaiset sarjat', params={'action': 'list_programs', 'category': 'Kotimaiset sarjat'})
helper.add_item('Lastenohjelmat', params={'action': 'list_programs', 'category': 'Lastenohjelmat'})
helper.add_item('Muut', params={'action': 'list_programs', 'category': 'Muut'})
helper.add_item('Ulkomaiset sarjat', params={'action': 'list_programs', 'category': 'Ulkomaiset sarjat'})
helper.add_item('Urheilu', params={'action': 'list_programs', 'category': 'Urheilu'})
helper.add_item('Uutiset ja sää', params={'action': 'list_programs', 'category': 'Uutiset ja sää'})
helper.eod()
def list_favorites():
favorites = helper.k.get_favorites()
for favorite in favorites['favorites']:
#Get program data
program_data = helper.k.get_program_data_by_id(favorite['programCategoryId'])
params = {
'action': 'list_videos_or_subcats',
'program_id': favorite['programCategoryId'],
'subcats': json.dumps(program_data['subs'])
}
helper.add_item(program_data['title'], params)
helper.eod()
def list_videos_or_subcats(program_id, subcats):
if json.loads(subcats):
list_subcats(program_id, subcats)
else:
list_videos(program_id=program_id)
def list_subcats(program_id, subcats):
for subcat in json.loads(subcats):
#List only subcategories where is more than 0 videos
if subcat['count'] > 0:
title = subcat['title']
params = {
'action': 'list_videos',
'program_id': program_id,
'subcat_id': subcat['id']
}
helper.add_item(title, params)
helper.eod()
def list_videos(program_id=None, subcat_id=None, search_query=None):
if program_id or subcat_id:
videos_data = helper.k.get_videos(program_id, subcat_id)
if videos_data['numberOfHits'] > 1:
videos = videos_data['asset']
else:
videos = []
videos.append(videos_data['asset'])
else:
videos = helper.k.get_search_data(search_query)['asset']
for i in videos:
params = {
'action': 'play',
'video_id': i['@id']
}
episode_info = {
'mediatype': 'episode',
'title': i.get('subtitle'),
'tvshowtitle': i.get('title'),
'plot': i.get('description'),
'duration': i.get('duration'),
'aired': i.get('liveBroadcastTime')
}
fanart_image = helper.k.get_program_fanart(program_id) if not search_query else None
thumb_image = i['imageVersions']['image']['url'] if i['imageVersions'] else None
episode_art = {
'fanart': fanart_image,
'thumb': thumb_image
}
helper.add_item(i.get('subtitle'), params=params, info=episode_info, art=episode_art, content='episodes', playable=True)
helper.eod()
def list_channels():
channels = helper.k.get_videos(33100, subcat_id=None)
for i in channels['asset']:
params = {
'action': 'play',
'video_id': i['@id']
}
episode_info = {
'mediatype': 'episode',
'title': i.get('title'),
'plot': i.get('description'),
'duration': i.get('duration')
}
episode_art = {
'thumb': i['imageVersions']['image']['url']
}
helper.add_item(i.get('title'), params=params, info=episode_info, art=episode_art, content='episodes', playable=True)
helper.eod()
def search():
search_query = helper.get_user_input('Hakusana')
if search_query:
list_videos(search_query=search_query)
else:
helper.log('No search query provided.')
return False
def router(paramstring):
"""
Router function that calls other functions
depending on the provided paramstring
:param paramstring: URL encoded plugin paramstring
:type paramstring: str
"""
# Parse a URL-encoded paramstring to the dictionary of
# {<parameter>: <value>} elements
params = dict(parse_qsl(paramstring))
# Check the parameters passed to the plugin
if 'setting' in params:
if params['setting'] == 'reset_credentials':
helper.reset_credentials()
elif 'action' in params:
if params['action'] == 'list_programs':
if 'category' in list(params.keys()):
list_programs(category=params['category'])
else:
list_programs()
elif params['action'] == 'list_categories':
list_categories()
elif params['action'] == 'favorites':
list_favorites()
elif params['action'] == 'livetv':
list_channels()
elif params['action'] == 'list_videos_or_subcats':
list_videos_or_subcats(program_id=params['program_id'], subcats=params['subcats'])
elif params['action'] == 'list_videos':
list_videos(program_id=params['program_id'], subcat_id=params['subcat_id'])
elif params['action'] == 'play':
# Play a video from a provided URL.
helper.play_item(params['video_id'])
elif params['action'] == 'search':
search()
else:
if helper.check_for_prerequisites():
try:
helper.login_process() # Have to trigger login process every time to get new cookie
# If the plugin is called from Kodi UI without any parameters,
# display the list of video categories
list_pages()
except helper.k.KatsomoError as error:
if error.value == 'AUTHENTICATION_FAILED':
helper.dialog('ok', 'Error', error.value)
if __name__ == '__main__':
# Call the router function and pass the plugin call parameters to it.
# We use string slicing to trim the leading '?' from the plugin call paramstring
router(sys.argv[2][1:])