forked from hemanta212/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallpaper_unsplash.py
104 lines (89 loc) · 3.22 KB
/
wallpaper_unsplash.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
import os, sys
import json
import ctypes
import random
from bs4 import BeautifulSoup as BS
import requests
url = "https://unsplash.com/search/photos/{0}"
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
def is_duplicate(link):
home_path = os.path.expanduser('~')
wallpaper_path = '.wallpaper_files/wallpaper.json'
full_path = os.path.join(home_path, wallpaper_path)
dir_path = os.path.dirname(full_path)
duplicate = True
if not os.path.exists(dir_path):
print("generating config folder...")
os.mkdir(dir_path)
if not os.path.exists(full_path):
print("Generating config file...")
with open(full_path, 'w') as wf:
links = {
'wallpaper_urls': []
}
json.dump(links, wf, indent=2)
else:
print("Checking config file for duplicates....")
with open(full_path, 'r') as wf:
try:
data = json.load(wf)
urls = data.get('wallpaper_urls')
if len(urls) >= 135:
urls = urls[20:]
if not link in urls:
print("No duplicates found....")
urls.append(link)
duplicate = False
except:
print("Unknown error regenerating config file..")
os.remove(full_path)
is_duplicate(link)
if not duplicate:
print("Writing new entry to config....")
with open(full_path, 'w') as wf:
json.dump(data, wf, indent=2)
return duplicate
def keywords(url):
key_list = [
"black", "shadow", "ghost", "night", "Neon",
"space", "darkness", "dark", "Dark Background","Horror", "stars",
"darkside", "universe", "moon", "galaxy"
]
random.shuffle(key_list)
choice = random.choice(key_list)
url = url.format(choice)
print("Url generated: ", url)
return url
def random_wallpaper():
try:
response = requests.get(keywords(url), headers=headers)
except:
print("No connection: Exiting..")
sys.exit(1)
soup = BS(response.content, 'lxml')
links = []
print("Scanning for pictures...")
for i in soup.find_all('img'):
primary_img = i.get('src')
if primary_img:
contents = primary_img.split(".")
small_images = primary_img.split("=")
if not 'adserver' in contents and not 'crop&w' in small_images:
links.append(primary_img)
print("Got ", len(links), "images, Choosing random one.....")
choice = random.choice(links)
return choice
duplicate = True
while duplicate:
wallpaper_url = random_wallpaper()
duplicate = is_duplicate(wallpaper_url)
print("Downloading the images:", wallpaper_url)
image_raw = requests.get(wallpaper_url, headers=headers)
home_path = os.path.expanduser('~')
wallpaper_path = '.wallpaper_files/wallpaper.png'
path = os.path.join(home_path, wallpaper_path)
print("writing image to file...")
with open(path, 'wb') as wf:
wf.write(image_raw.content)
print("changing the wallpaper")
ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 3)