forked from Vonjessen/plugin.git.browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.py
299 lines (266 loc) · 11.7 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
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
# -*- coding: utf-8 -*-
'''*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*'''
from commoncore import kodi
@kodi.register('main')
def main():
show_about()
kodi.add_menu_item({'mode': 'search_menu', 'type': "username", 'title': "Search by GitHub Username"}, {'title': "Search by GitHub Username"}, icon='username.png')
kodi.add_menu_item({'mode': 'search_menu', 'type': "repository", 'title': "Search by GitHub Repository Title"}, {'title': "Search by GitHub Repository Title [COLOR red](Advanced)[/COLOR]"}, icon='repository.png')
kodi.add_menu_item({'mode': 'search_menu', 'type': "addonid",'title': "Search by Addon ID"}, {'title': "Search by Addon ID [COLOR red](Advanced)[/COLOR]"}, icon='addonid.png')
kodi.add_menu_item({'mode': 'feed_menu'}, {'title': "Search Feeds"}, icon='search_feeds.png', visible=feed_count()>0)
kodi.add_menu_item({'mode': 'update_addons'}, {'title': "Check for Updates [COLOR red](Advanced)[/COLOR]"}, icon='update.png', visible=kodi.get_setting('enable_updates') == 'true')
kodi.add_menu_item({'mode': 'about'}, {'title': "About GitHub Installer"}, icon='about.png')
kodi.add_menu_item({'mode': 'addon_settings'}, {'title': "Tools and Settings"}, icon='settings.png')
kodi.eod()
@kodi.register('search_menu')
def search_menu():
from libs.database import DB
menu = kodi.ContextMenu()
menu.add('Search Filter', {"mode": "search_filter"})
kodi.add_menu_item({'mode': 'void'}, {'title': "[COLOR darkorange]%s[/COLOR]" % kodi.arg('title')}, icon='null', menu=menu)
kodi.add_menu_item({'mode': 'search', 'type': kodi.arg('type')}, {'title': "*** New Search ***"}, icon='null', menu=menu)
#kodi.add_menu_item({'mode': 'search_filter'}, {'title': "Set Result Filter"}, icon='null', menu=menu)
results = DB.query_assoc("SELECT search_id, query FROM search_history WHERE search_type=? ORDER BY ts DESC LIMIT 10", [kodi.arg('type')], quiet=True)
if results is not None:
for result in results:
menu = kodi.ContextMenu()
menu.add('Search Filter', {"mode": "search_filter"})
menu.add('Delete from search history', {"mode": "history_delete", "id": result['search_id']})
kodi.add_menu_item({'mode': 'search', 'type': kodi.arg('type'), 'query': result['query']}, {'title': result['query']}, menu=menu, icon='null')
kodi.eod()
@kodi.register('search')
def search():
from commoncore.dispatcher import dispatcher
from libs.database import DB
from libs import github
q = kodi.arg('query') if kodi.arg('query') else kodi.dialog_input('Search GitHub')
if q in [None, False, '']: return False
DB.execute('INSERT INTO search_history(search_type, query) VALUES(?,?)', [kodi.arg('type'), q])
DB.commit()
@dispatcher.register('username')
def username():
rtype = 'api'
response = github.find_zips(q)
if response is None: return
for r in github.sort_results(response['items']):
url = github.content_url % (r['repository']['full_name'], r['path'])
menu = kodi.ContextMenu()
if r['is_repository']:
menu.add('Browse Repository Contents', {"mode": "browse_repository", "url": url, "file": r['name'], "full_name": "%s/%s" % (q, r['repository']['name'])})
if r['is_feed']:
r['display'] = "[COLOR yellow]%s[/COLOR]" % r['name']
kodi.add_menu_item({'mode': 'install_feed', "url": url}, {'title': r['name'], 'display': r['display']}, menu=menu, icon='null')
else:
kodi.add_menu_item({'mode': 'github_install', "url": url, "user": q, "file": r['name'], "full_name": "%s/%s" % (q, r['repository']['name'])}, {'title': r['name']}, menu=menu, icon='null')
@dispatcher.register('repository')
def repository():
rtype = 'api'
results = github.search(q, 'title')
if results is None: return
for i in results['items']:
user = i['owner']['login']
response = github.find_zips(user)
if response is None: continue
for r in github.sort_results(response['items']):
url = github.content_url % (r['repository']['full_name'], r['path'])
menu = kodi.ContextMenu()
if r['is_repository']:
menu.add('Browse Repository Contents', {"mode": "browse_repository", "url": url, "file": r['name'], "full_name": "%s/%s" % (q, r['repository']['name'])})
if r['is_feed']:
kodi.add_menu_item({'mode': 'install_feed', "url": url}, {'title': r['name']}, menu=menu, icon='null')
else:
kodi.add_menu_item({'mode': 'github_install', "url": url, "user": q, "file": r['name'], "full_name": "%s/%s" % (q, r['repository']['name'])}, {'title': r['name']}, menu=menu, icon='null')
@dispatcher.register('addonid')
def addonid():
from commoncore.core import highlight
from libs.github import re_version, content_url
from distutils.version import LooseVersion
rtype = 'api'
results = github.search(q, 'id')
if results is None: return
def version_sort(name):
v = re_version.search(name)
if v:
return LooseVersion(v.group(1))
else:
return LooseVersion('0.0.0')
results.sort(key=lambda x:version_sort(x['name']), reverse=True)
for i in results:
menu = kodi.ContextMenu()
r = i['repository']
full_name = r['full_name']
title = highlight("%s/%s" % (full_name, i['name']), q, 'yellow')
url = content_url % (full_name, i['path'])
menu.add("Search Username", {'mode': 'search', 'type': 'username', 'query': r['owner']['login']})
kodi.add_menu_item({'mode': 'github_install', "url": url, "file": i['name'], "full_name": full_name}, {'title': title}, menu=menu, icon='null')
dispatcher.run(kodi.arg('type'))
kodi.eod()
@kodi.register('search_filter')
def search_filter():
from commoncore.core import format_color
options = display =['None', 'Repository', 'Feed', 'Music Plugin', 'Video Plugin', 'Script']
filter = kodi.get_property('search.filter')
if filter in options:
index = options.index(filter)
display[index] = format_color(display[index], 'yellow')
else:
display[0] = format_color(display[0], 'yellow')
c = kodi.dialog_select("Filter Results by:", display)
if c is not False:
if c is 0:
kodi.set_property('search.filter', '')
else:
kodi.set_property('search.filter', options[c])
@kodi.register('feed_menu')
def feed_menu():
from libs.database import DB
kodi.add_menu_item({'mode': 'new_feed'}, {'title': "*** New Search Feed ***"}, icon='null')
feeds = DB.query_assoc("SELECT feed_id, name, url, enabled FROM feed_subscriptions")
for feed in feeds:
menu = kodi.ContextMenu()
name = feed['name'] if feed['name'] else feed['url']
if not feed['enabled']:
title = "[COLOR darkred]%s[/COLOR]" % name
else: title = name
menu.add('Delete Feed', {"mode": "delete_feed", "title": title, "id": feed['feed_id']})
kodi.add_menu_item({'mode': 'list_feed', 'url': feed['url']}, {'title': title}, menu=menu, icon='null')
kodi.eod()
@kodi.register('install_feed')
def install_feed():
if not kodi.dialog_confirm('Install Feed?', "Click YES to proceed."): return
from libs.database import DB
from libs import github
xml = github.install_feed(kodi.arg('url'))
try:
for f in xml.findAll('feeds'):
name = f.find('name').text
url = f.find('url').text
DB.execute("INSERT INTO feed_subscriptions(name, url) VALUES(?,?)", [name, url])
DB.commit()
count = DB.query("SELECT count(1) FROM feed_subscriptions")
kodi.set_setting('installed_feeds', str(count[0][0]))
kodi.notify("Install Complete",'Feed Installed')
except:
kodi.notify("Install failed",'Invalid Format.')
def feed_count():
from libs.database import DB
try:
count = DB.query("SELECT count(1) FROM feed_subscriptions")[0][0]
except:
count = 0
return count
@kodi.register('new_feed')
def new_feed():
from libs.database import DB
url = kodi.dialog_input('Feed URL')
if not url: return
DB.execute("INSERT INTO feed_subscriptions(url) VALUES(?)", [url])
DB.commit()
kodi.refresh()
@kodi.register('delete_feed')
def delete_feed():
if not kodi.dialog_confirm('Delete Feed?', kodi.arg('title'), "Click YES to proceed."): return
from libs.database import DB
DB.execute("DELETE FROM feed_subscriptions WHERE feed_id=?", [kodi.arg('id')])
DB.commit()
kodi.refresh()
@kodi.register('list_feed')
def feed_list():
from commoncore.baseapi import CACHABLE_API, EXPIRE_TIMES
class FeedAPI(CACHABLE_API):
base_url = ''
default_return_type = 'xml'
try:
xml = FeedAPI().request(kodi.arg('url'), cache_limit=EXPIRE_TIMES.EIGHTHOURS)
for r in xml.findAll('repository'):
name = r.find('name').text
username = r.find('username').text
desc = r.find('description').text
title = "%s: %s" % (name, desc)
kodi.add_menu_item({'mode': 'search', 'type': 'username', 'query': username}, {'title': title, 'plot': desc}, icon='null')
kodi.eod()
except:pass
@kodi.register('github_install')
def github_install():
import re
from libs import github_installer
from libs import github
c = kodi.dialog_confirm("Confirm Install", kodi.arg('file'), yes="Install", no="Cancel")
if not c: return
addon_id = re.sub("-[\d\.]+zip$", "", kodi.arg('file'))
github_installer.GitHub_Installer(addon_id, kodi.arg('url'), kodi.arg('full_name'), kodi.vfs.join("special://home", "addons"))
r = kodi.dialog_confirm(kodi.get_name(), 'Click Continue to install more addons or', 'Restart button to finalize addon installation', yes='Restart', no='Continue')
if r:
import sys
import xbmc
if sys.platform in ['linux', 'linux2', 'win32']:
xbmc.executebuiltin('RestartApp')
else:
xbmc.executebuiltin('ShutDown')
@kodi.register('about')
def about():
try:
import xbmc
KODI_LANGUAGE = xbmc.getLanguage()
except:
KODI_LANGUAGE = 'English'
path = kodi.vfs.join(kodi.get_path(), 'resources/language/%s/github_help.txt', KODI_LANGUAGE)
if not kodi.vfs.exists(path):
path = kodi.vfs.join(kodi.get_path(), 'resources/language/English/github_help.txt')
text = kodi.vfs.read_file(path)
kodi.dialog_textbox('GitHub Browser Instructions', text)
def show_about():
if kodi.get_setting('disable_about') == 'true': return
interval = int(kodi.get_setting('last_about'))
if interval == 0:
interval = 5
try:
import xbmc
KODI_LANGUAGE = xbmc.getLanguage()
except:
KODI_LANGUAGE = 'English'
path = kodi.vfs.join(kodi.get_path(), 'resources/language/%s/github_help.txt', KODI_LANGUAGE)
if not kodi.vfs.exists(path):
path = kodi.vfs.join(kodi.get_path(), 'resources/language/English/github_help.txt')
text = kodi.vfs.read_file(path)
kodi.dialog_textbox('GitHub Browser Instructions', text)
else:
interval -= 1
kodi.set_setting('last_about', interval)
@kodi.register('browse_repository')
def browse_repository():
from libs import github
xml = github.browse_repository(kodi.arg('url'))
heading = "%s/%s" % (kodi.arg('full_name'), kodi.arg('file'))
options = []
if xml:
for addon in xml.findAll('addon'):
options.append("%s (%s)" % (addon['name'], addon['version']))
kodi.dialog_select(heading, sorted(options))
@kodi.register('history_delete')
def history_delete():
if not kodi.arg('id'): return
from libs.database import DB
DB.execute("DELETE FROM search_history WHERE search_id=?", [kodi.arg('id')])
DB.commit()
kodi.refresh()
@kodi.register('update_addons')
def update_addons():
from libs import github_installer
quiet = True if kodi.arg('quiet') == 'quiet' else False
if not quiet:
c = kodi.dialog_confirm("Confirm Update", "Check for updates", yes="Update", no="Cancel")
if not c: return
github_installer.update_addons(quiet)
if __name__ == '__main__': kodi.run()