forked from primaeval/script.tvguide.fullscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PickImage.py
76 lines (65 loc) · 2.04 KB
/
PickImage.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
import os
import re
import xbmc
import xbmcgui
import xbmcaddon
import xbmcvfs
import requests
import json
import sys
from rpc import RPC
location = sys.argv[1]
setting = sys.argv[2]
def log(x):
xbmc.log(repr(x))
ADDON = xbmcaddon.Addon(id='script.tvguide.fullscreen')
d = xbmcgui.Dialog()
if location == "both":
places = ["Local","Pixabay"]
elif location == "remote":
places = ["Pixabay"]
else:
places = ["Local"]
where = d.select("Image Search",places)
if where == -1:
quit()
place = places[where]
if place == "Local":
image = d.browse(2, 'Image', 'files', '', True, False)
if image:
ADDON.setSetting(setting,image)
elif place == "Pixabay":
what = d.input("PixaBay Image Search","background")
if not what:
quit()
url = "https://pixabay.com/api/?key=3974133-0e761ef66bcfb72c6a8ac8f4e&q=%s&image_type=photo&pretty=true&orientation=horizontal&per_page=200" % what
r = requests.get(url)
j = json.loads(r.content)
if not 'hits' in j:
quit()
dirs, files = xbmcvfs.listdir('special://profile/addon_data/script.tvguide.fullscreen/pick/')
for f in files:
path = 'special://profile/addon_data/script.tvguide.fullscreen/pick/'+f
success = xbmcvfs.delete(path)
hits = j['hits']
images = {}
p = xbmcgui.DialogProgressBG()
p.create("Finding Images","...")
total = len(hits)
i = 0
for h in hits:
url = h["previewURL"].replace(': //','://')
basename= url.rsplit('/',1)[-1]
localfile = 'special://profile/addon_data/script.tvguide.fullscreen/pick/'+basename
xbmcvfs.copy(url,localfile)
image = h["webformatURL"].replace(': //','://')
if image:
images[localfile] = image
percent = 100.0 * i / total
i = i + 1
p.update(int(percent),"Finding Images",basename)
p.close()
what = d.browse(2, 'Image', 'files', '', True, False, 'special://profile/addon_data/script.tvguide.fullscreen/pick/')
image = images.get(what,'')
if image:
ADDON.setSetting(setting,image)