-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.py
346 lines (268 loc) · 11.3 KB
/
main.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import ctypes, os, time, datetime, requests, json, win32gui, sys
from PIL import Image, ImageDraw, ImageFont
class POINT(ctypes.Structure):
_fields_ = [('x', ctypes.c_ulong), ('y', ctypes.c_ulong)]
def queryMousePosition():
pt = POINT()
ctypes.windll.user32.GetCursorPos(ctypes.byref(pt))
return { 'x': pt.x, 'y': pt.y}
class Cache:
def __init__(self):
self.filename = 'resources/cache/settings.json'
self.checkExists()
self.content = False
self.cache = False
self.read()
def checkExists(self):
if not os.path.exists(self.filename):
self.createCashe()
self.save()
def toString(self):
return json.dumps( self.cache, indent = 4, ensure_ascii = False )
def writeToFile(self, content):
f = open( self.filename, 'w', encoding = 'utf8' )
f.write( content )
f.close()
def save(self):
self.writeToFile( self.toString() )
def readFile(self):
f = open(self.filename, 'r', encoding = 'utf8')
self.content = f.read()
f.close()
def read(self, i = 0):
if i > 1:
print('Error read cache file.')
sys.exit()
self.readFile()
try:
self.cache = json.loads( self.content )
except:
self.createCashe()
self.save()
return self.read(i + 1)
def createCashe(self):
self.cache = {
'version': 'v1.0'
}
def get(self, index, default):
if index in self.cache:
return self.cache[index]
self.put( index, default )
return default
def put(self, index, body):
self.cache[index] = body
self.save()
cache = Cache()
class Course:
def getJsonData():
url = 'https://www.cbr-xml-daily.ru/daily_json.js'
return requests.get(url).json()
def getCourse(args):
try:
data = Course.getJsonData()
tbl = []
for i in args:
tbl.append( (i.upper(), round(data['Valute'][ i.upper() ]['Value'], 2)))
return tbl
except:
return False
class Weather:
def getCitys():
return cache.get('weather', [
['Самара', 'https://www.gismeteo.ru/weather-samara-4618/'],
['Тольятти', 'https://www.gismeteo.ru/weather-tolyatti-4429/'],
['Москва', 'https://www.gismeteo.ru/weather-moscow-4368/']
])
city = getCitys()[ cache.get('weatherIndex', 0) ]
def setCity(index):
Weather.city = Weather.getCitys()[index]
def getJsonData():
try:
data = requests.get(Weather.city[1], 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'}).text
i = str.find(data, 'M.state.weather.cw = {')
if i == -1:
return False
end = str.find(data[i:], '\n')
if end == -1:
return False
return json.loads( data[i + 21: i + end] )
except:
return False
def getData():
try:
info = Weather.getJsonData()
return (Weather.city[0], info['temperatureAir'][0], info['description'][0])
except:
return False
class Date:
def getDay():
return ([
'Понедельник',
'Вторник',
'Среда',
'Четверг',
'Пятница',
'Суббота',
'Воскресенье'
])[datetime.datetime.today().isoweekday() - 1]
def get(*formats):
date = datetime.datetime.now()
if len(formats) == 1:
return date.strftime(formats[0])
back = []
for i in formats:
back.append( date.strftime(i) )
return tuple(back)
class Color:
def hex(s):
n = int(s.lstrip('#'), 16)
return (n >> 16, (n >> 8) & 0xff, n & 0xff)
def getThemeList():
return [
{ 'bg': Color.hex('#131313'), 'fg': Color.hex('#980002'), 'text': Color.hex('#ffbf00') },
{ 'bg': Color.hex('#ebdcb2'), 'fg': Color.hex('#af4425'), 'text': Color.hex('#552e1c') },
{ 'bg': Color.hex('#1e0000'), 'fg': Color.hex('#bc6d4f'), 'text': Color.hex('#9d331f') },
{ 'bg': Color.hex('#ddc5a2'), 'fg': Color.hex('#523634'), 'text': Color.hex('#b6452c') },
{ 'bg': Color.hex('#003b46'), 'fg': Color.hex('#c3dfe6'), 'text': Color.hex('#66a5ad') }
]
def getTheme( mode ):
return Color.getThemeList()[mode]
class Cord:
def __init__(self, x, y, w, h):
self.x = x
self.y = y
self.w = w
self.h = h
self.x2 = x + w
self.y2 = y + h
def isInside(self, x, y):
return self.x <= x <= self.x2 and self.y <= y <= self.y2
class Main():
def __init__(self):
user32 = ctypes.windll.user32
self.width = user32.GetSystemMetrics(0)
self.height = user32.GetSystemMetrics(1)
self.mBuffer = user32.GetKeyState(1)
self.mPos = False
self.mDown = False
self.path = os.getcwd()
self.indexTheme = cache.get('themeIndex', 0)
self.theme = Color.getTheme(self.indexTheme)
self.font = ImageFont.load_default()
self.cashFonts = {}
self.bg = False
self.bgLastColor = False
self.openCitys = False
self.buttons = []
def addButton(self, cord, function, update):
self.buttons.append({'cord': cord, 'function': function, 'update': update})
def genEmpty(self):
if not self.bg or self.theme['bg'] != self.bgLastColor:
color = self.theme['bg']
img = Image.new('RGB', (self.width, self.height), color)
self.orig = img
img = self.orig.copy()
self.object = img
self.draw = ImageDraw.Draw(img)
def getTextSize(self, text):
return self.draw.textsize(text, self.font)
def setFont(self, name, size):
id = f'{name}x{size}'
if not id in self.cashFonts:
self.cashFonts[id] = ImageFont.truetype(f'resources/fonts/{name}', size)
self.font = self.cashFonts[id]
def setText(self, x, y, text, color = (255, 255, 255),):
self.draw.text((x, y), text, font = self.font, fill = color)
w, h = self.getTextSize( text )
return Cord(x, y, w, h)
def setMindText(self, x, y, text, color = (255, 255, 255)):
w, h = self.getTextSize( text )
return self.setText( int(x - w / 2), y, text, color )
def setWallpaper(self, filename):
ctypes.windll.user32.SystemParametersInfoW(0x0014 , 0, self.path + f'\\{filename}', 2)
def onUpdate(self, i = 0):
self.pos = queryMousePosition()
ctypes.windll.user32.GetKeyState.restype = ctypes.c_ushort
if ctypes.windll.user32.GetKeyState(1) != self.mBuffer:
self.mBuffer = bool(abs(int(self.mBuffer) - 1))
self.mDown = True
back = False
if self.mDown:
self.mDown = False
focus = win32gui.GetWindowText( win32gui.GetForegroundWindow() )
if focus in {'Program Manager', ''} :
x, y = self.pos['x'], self.pos['y']
for butt in self.buttons:
if butt['cord'].isInside(x, y):
butt['function']()
if butt['update']:
back = True
return back
def update(self):
def cityBtn():
self.openCitys = True
def setCity(index):
Weather.setCity( index )
self.openCitys = False
cache.put('weatherIndex', index)
self.weather = Weather.getData()
def newTheme():
self.indexTheme += 1
if self.indexTheme == len(Color.getThemeList()):
self.indexTheme = 0
self.theme = Color.getTheme(self.indexTheme)
cache.put('themeIndex', self.indexTheme)
self.buttons = []
c2 = False
self.genEmpty()
self.minutes, H_M, d_m_Y = Date.get('%M', '%H:%M', '%d.%m.%Y' )
w = self.width / 2
theme = self.theme
self.setFont( 'font.otf', 100 )
c = self.setMindText( w , self.height / 3 - 100, Date.getDay(), theme['fg'])
if self.courses:
self.setFont( 'font.otf', 50 )
c2 = self.setMindText( w , c.y2 + 15, ' '.join([ f'{i[0]}: {str(i[1])}' for i in self.courses ]), theme['fg'])
self.draw.line(( min(c.x, c2.x) - 10, c2.y2 + 15, max(c.x2, c2.x2) + 10, c2.y2 + 15), fill = theme['fg'], width=5)
else:
self.draw.line(( c.x - 10, c.y2 + 15, c.x2 + 10, c.y2 + 15), fill = theme['fg'], width=5)
self.setFont( 'font.otf', 100 )
c = self.setMindText( w , (c2.y2 if c2 else c.y2 ) + 30, H_M, theme['fg'])
self.setFont( 'font.otf', 45 )
c = self.setMindText( w , c.y2 + 20, d_m_Y, theme['fg'])
self.setFont( 'font.ttf', 20 )
w1, h1 = self.getTextSize('Сменить тему')
s = self.setText( self.width - 5 - w1, self.height - 48 - h1, 'Сменить тему', theme['fg'])
self.addButton(s, lambda: newTheme(), True)
if not self.openCitys:
self.setFont( 'font.otf', 40 )
if self.weather:
s = self.setText(5, 5, str(self.weather[0]), theme['text'])
self.addButton(s, cityBtn, True)
self.setText(5, s.y2 + 5, f'{str(self.weather[1])}° {str(self.weather[2])}', theme['text'])
else:
self.setFont( 'font.ttf', 25 )
s = self.setText(5, 5, 'Выберите населённый пункт', theme['fg'])
tbl = Weather.getCitys()
for i in range(len(tbl)):
s = self.setText(10, s.y2 + 5, tbl[i][0], theme['text'])
self.addButton(s, lambda index = i: setCity(index), True)
try:
self.object.save('resources/tmp/temp.png')
self.setWallpaper('resources/tmp/temp.png')
except:
if i > 2:
return
self.update(i + 1)
def start(self):
while True:
self.courses = Course.getCourse(cache.get('courses', ['USD', 'EUR']))
self.weather = Weather.getData()
self.update( )
while self.minutes == Date.get('%M'):
if self.onUpdate():
self.update( )
time.sleep(0.1)
if __name__ == "__main__":
main = Main()
main.start()