-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathwallpaper-scraper.py
More file actions
55 lines (47 loc) · 1.26 KB
/
wallpaper-scraper.py
File metadata and controls
55 lines (47 loc) · 1.26 KB
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
import urllib2
import json
import os
import os.path
import datetime
import time
from os.path import expanduser
def getName(urlname):
i = urlname.find("_EN")
i -= 1
s1 = ""
while (urlname[i] != '/') :
s1 += urlname[i]
i -= 1
s1 = s1[::-1] + '.jpg'
return s1
market = "en-US"
resolution = "1920x1080"
wallpaperDirectory = expanduser("~")+'/Pictures/Wallpapers/'
i = 1
while (i == 1) :
try :
urllib2.urlopen("http://google.com")
except urllib2.URLError:
time.sleep(5)
print("Trying to connect to internet")
else :
i = 0
response = urllib2.urlopen("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=" + market)
obj = json.load(response)
url = obj['images'][0]['urlbase']
wallpaperName = getName(url)
print("The name of wallpaper is %s" %(wallpaperName))
url = 'http://www.bing.com' + url + '_' + resolution + '.jpg'
print(url)
if not os.path.exists(wallpaperDirectory) :
os.makedirs(wallpaperDirectory)
path = wallpaperDirectory + wallpaperName
print("Downloading Bing Wallpaper to %s" %(path))
f = open(path,"w")
bingpic = urllib2.urlopen(url)
f.write(bingpic.read())
s1 = "/usr/bin/gsettings set org.gnome.desktop.background picture-uri file:///home/fbd/Pictures/Wallpapers/" + wallpaperName
try:
os.system(s1)
except OSError:
print("OS Error")