/
wall.py
291 lines (215 loc) · 7.07 KB
/
wall.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
#! /usr/bin/python
"""
Usage: python wall.py [OPTIONS] TAGS
OPTIONS:
-q query or --query query
TAGS is a space delimited list of tags
Wallpaper changer for Ubuntu that downloads wallpapers from wallgig through a Mashape api.
You can chouse a set of wallpapers
A Mashape authorization key is required.
Tested for Ubuntu 14.04 - Gnome 3
Requires:
- unirest, install from pip
- appindicator, install from pip
"""
import urllib, urllib2, urlparse
import os
import sys
import random
import unirest
import json
import gtk
import appindicator
from getopt import getopt, GetoptError
__author__ = "Simone Fisicaro <simonefisicaro@gmail.com>"
__version__ = "0.1"
__date__ = "15-05-2014"
#set wallpaper import - only works without gtk, appindicator
#from gi.repository import Gtk, Gio
#set your screen size
width = 1366
height = 768
ufile = "wallpaper_urls.txt"
wfile = "wallpaper.jpg"
tfile = "wallpaper.tmp"
mashape_key = "Insert-your-api-key"
SCHEMA = 'org.gnome.desktop.background'
KEY = 'picture-uri'
class Wallpy:
def __init__(self, q, t):
self.query = q
self.tags = t
self.ind = appindicator.Indicator("Wallp", "indicator-messages", appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(appindicator.STATUS_ACTIVE)
self.ind.set_icon("distributor-logo")
self.menu_setup()
self.ind.set_menu(self.menu)
self.filemanage = FileManager(ufile, wfile, tfile)
def main(self):
gtk.main()
return 0
def menu_setup(self):
self.menu = gtk.Menu()
self.name = gtk.MenuItem("Wallpy")
self.name.set_sensitive(False)
self.name.show()
self.menu.append(self.name)
self.new_wallp = gtk.MenuItem("A new wallp?")
self.new_wallp.connect("activate", self.on_new_wallp_clicked)
self.new_wallp.show()
self.menu.append(self.new_wallp)
self.status = gtk.MenuItem("")
self.status.set_sensitive(False)
self.status.show()
self.menu.append(self.status)
self.quit_item = gtk.MenuItem("Quit")
self.quit_item.connect("activate", self.quit)
self.quit_item.show()
self.menu.append(self.quit_item)
def quit(self, widget):
sys.exit(0)
def on_new_wallp_clicked(self, widget):
self.new_wallp.set_sensitive(False)
check = self.get_wallpgig()
if check == 1:
self.load_wallp()
else:
self.status.set_label(check)
self.new_wallp.set_sensitive(True)
def load_wallp(self):
temp = open(tfile, 'r')
wallp = open(wfile, 'w')
wallp.write(temp.read())
temp.close()
wallp.close()
os.remove(tfile)
#this solution does not work if I import appindicator and gtk
#gsettings = Gio.Settings.new(self.SCHEMA)
#background = os.path.dirname(os.path.abspath(__file__)) + '/' + wfile
#gsettings.set_string(self.KEY, "file://" + background)
background = "file://" + os.path.dirname(os.path.abspath(__file__)) + '/' + wfile
cmd = "gsettings set org.gnome.desktop.background picture-uri " + background
os.system(cmd)
#solution for gnome 2
#gconftool-2 --type=string --set /desktop/gnome/background/picture_filename" + background
self.status.set_label("Complete.")
#get wallpaper from wallgig
def get_wallpgig(self):
if not self.check_connection():
return "No internet connection!"
tags_from_file = self.filemanage.file_get_tags()
query_from_file = self.filemanage.file_get_query()
if (set(tags_from_file) == set(self.tags)) & (query_from_file == self.query):
self.status.set_label("Loading urls...")
urls = self.filemanage.file_get_urls()
self.status.set_label("Urls loaded.")
else:
self.status.set_label("Downloading urls...")
urls = self.download_urls()
self.status.set_label("Urls downloaded.")
random.shuffle(urls)
try:
url = urls[0]
urllib.urlretrieve(url, tfile)
urllib.urlcleanup()
check = 1
except IndexError:
check = "No photos for this tags!"
except urllib2.URLError:
check = "No internet connection!"
return check
def download_urls(self):
if not self.check_connection():
return []
t = ""
for tag in self.tags:
t += "&tags[]=" + tag
try:
response = unirest.get("https://wallgig-v1.p.mashape.com/wallpapers?q=" + self.query + "&width=" + str(width) + "&height=" + str(height) + "" + t,
headers={"X-Mashape-Authorization": mashape_key});
except urllib2.URLError:
return []
j = json.loads(response.raw_body)
wallpapers = j["wallpapers"]
urls = []
for w in wallpapers:
image = w['image']
original = image['original']
urls.append(original['url'])
self.filemanage.file_save_urls(self.query, self.tags, urls)
return urls
def check_connection(self):
try:
urllib2.urlopen("http://www.google.com").close()
except urllib2.URLError:
return False
else:
return True
class FileManager:
def __init__(self, u, w, t):
self.ufile = u
self.wfile = w
self.tfile = t
def file_save_urls(self, query, tags, urls):
file = open(ufile, 'w')
file.write(query + "\n")
for tag in tags:
file.write(tag + ' ')
file.write("\n")
for url in urls:
file.write(url + "\n")
file.close()
def file_get_query(self):
try:
file = open(ufile, 'r')
except IOError:
return ""
lines = file.readlines()
q = ""
if len(lines) >= 1:
q = lines[0].replace('\n', '')
file.close()
return q
def file_get_tags(self):
try:
file = open(ufile, 'r')
except IOError:
return []
lines = file.readlines()
tags = []
if len(lines) >= 2:
tags = lines[0].rstrip().split(' ')
file.close()
return tags
def file_get_urls(self):
file = open(ufile, 'r')
lines = file.readlines()
urls = []
if len(lines) >= 3:
tags = lines[0]
for line in lines[1:]:
urls.append(line.replace('\n', ''))
pass
file.close()
return urls
def main(*argv):
try:
(opts, args) = getopt(argv[1:], 'q', ['query'])
except GetoptError, e:
print e
print __doc__
return 1
query = ""
tags = []
for o, a in opts:
if o in ('-q' '--query'):
query = a
else:
print "Unknown argument: %s" % o
print __doc__
return 1
tags = [item for item in args]
indicator = Wallpy(query, tags)
indicator.main()
if __name__ == '__main__':
sys.exit(main(*sys.argv))