-
Notifications
You must be signed in to change notification settings - Fork 0
/
bing_wallpaper.py
122 lines (102 loc) · 3.43 KB
/
bing_wallpaper.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
import requests, os, sys, subprocess, shutil
from ctypes import windll
from tkinter import Tk, messagebox
import re
from time import sleep
import schedule, threading
start_pc = 0
picture_name = ''
### set your registery to autorun after restart pc!
def become_persistent():
try:
prgram_path = os.environ['appdata'] + '\\bingo'
file_location = prgram_path + '\\bing_wallpaper.exe'
if not os.path.exists(file_location):
os.mkdir(prgram_path)
shutil.copyfile(sys.executable, file_location)
subprocess.call(
'reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v bing-wallpaper /t REG_SZ /d "'
+ file_location + '"',
shell=True)
except Exception:
root = Tk()
root.withdraw()
messagebox.showerror('error', 'please run with administrator permission!')
# sys.exit()
""" after setup pc this mechanism run every minute and when success to connect api and
get picture run every 15 miniute again and again
"""
def schedule_day():
schedule.every(15).minutes.do(background_changer)
global start_pc
while True:
if start_pc == 0:
timer = threading.Timer(1.0, background_changer)
timer.start()
sleep(59)
else:
schedule.run_pending()
sleep(1)
def get_url():
try:
global picture_name
api = requests.get('https://bing.biturl.top')
img_url = api.json()['url']
picture_name = re.search(r'th\?id=OHR\.(.*)?_ZH', img_url).group(1)
return img_url
except Exception:
pass
def download(url):
global start_pc
"""set timer to finish download process"""
try:
img = requests.get(url)
start_pc = 1
return img
except requests.ConnectionError:
start_pc = 0
pass
### change your wall in windows:)
def background_changer():
global path
path = os.path.join(os.path.expanduser('~'), 'Pictures', 'Bing_WallPaper')
try:
file_exist()
img_url = get_url()
new_file = path + '\\' + picture_name + '.jpg'
if not os.path.exists(new_file):
picture = download(img_url)
if picture.ok:
with open(new_file, 'wb') as file:
file.write(picture.content)
windll.user32.SystemParametersInfoW(20, 0, new_file, 1)
except Exception:
pass
# check file path
def file_exist():
if not os.path.exists(path=path):
os.mkdir(path=path)
# def check_execute():
# file_path = os.path.abspath(__file__).split('\\')[-1]
# x = file_path.split('.')
# reg = x[0] + '.exe'
# binary_data = subprocess.run('tasklist | findstr /R -i "' + reg + '"', shell=True, stdout=subprocess.PIPE)
# utf8 = binary_data.stdout.decode()
# # if utf8.find('.exe') != -1:
# # print('uffff')
# if '.exe' in utf8:
# print('hast')
# # index_num = utf8.find('.exe') + 4
# # result = utf8[0:index_num]
# print(utf8)
# # print(file_path)
# if reg.lower() == utf8.lower():
# return True
# return False
"""if program execute don't run it again Ok!"""
# if check_execute():
root = Tk()
root.withdraw()
messagebox.showinfo('Info', 'bing_wallpaper start running!')
# become_persistent()
schedule_day()