· 6 years ago · Dec 29, 2019, 08:04 PM
1import ctypes
2import requests
3import random
4import time
5import httplib2
6import subprocess
7import os
8
9def main():
10
11 def set_wallpaper(path):
12 '''
13 Установка обоев на рабочий стол, аргумент функции - путь до картинки. Минус в том что после перезагрузки обои сбрасываются.
14 '''
15 ctypes.windll.user32.SystemParametersInfoW(20, 0, path , 0)
16
17
18 def get_image():
19 '''
20 Поиск картинки через API, выдает ссылку на картинку. В будущем сделать поиск картинок по параметрам и добавить функцию включения/отключения NSFW
21 '''
22
23 k = {
24 'sorting':'toplist',
25 'topRange':'1d',
26 'atleast':'1920x1080',
27 'ratios':'16x9,16x10,21x9,32x9,48x9'
28 }
29
30 response = requests.get("https://wallhaven.cc/api/v1/search?apikey=INSERT YOUR KEY HERE",params=k)
31 # Делать api запрос 1 раз, просто сохранять данные в переменную
32 A = response.json()
33
34 page = random.randint(1,A['meta']['last_page']-1)
35
36 k = {
37 'sorting':'toplist',
38 'topRange':'1d',
39 'atleast':'1920x1080',
40 'page':page,
41 'ratios':'16x9,16x10,21x9,32x9,48x9'
42 }
43
44 response = requests.get("https://wallhaven.cc/api/v1/search?apikey=INSERT YOUR KEY HERE",params=k)
45 # print(response.status_code)
46 A = response.json()
47
48 # print(A['meta'])
49
50
51 # for i in A['data']:
52 # print(i)
53 # print()
54
55 # print(page)
56
57 return A['data'][random.randint(1,24)]['path']
58
59
60
61 def save_image(link):
62 '''
63 Сохранение картинки по ссылке в указанную папку.
64 '''
65
66 h = httplib2.Http('.cache')
67 response, content = h.request(link)
68 name = time.strftime("%d.%m.%Y %H-%M-%S",time.localtime())
69 out = open(f"C:\\Users\\aaaan\\Desktop\\cashed images\\{name}.jpg", 'wb')
70 out.write(content)
71 out.close()
72
73 def new_mech():
74
75 def find_last_wlp():
76 return os.listdir('C:\\Users\\aaaan\\Desktop\\cashed images')[-1]
77
78 set_wallpaper(f"C:\\Users\\aaaan\\Desktop\\cashed images\\{find_last_wlp()}")
79
80
81 link = get_image()
82
83 save_image(link)
84
85
86
87 new_mech()
88
89main()
90
91# input()