11import asyncio
22import logging
3-
4- import requests
53import sys
4+ from pathlib import Path
5+
6+ import aiohttp
67
78from vkmax .client import MaxClient
89from vkmax .functions .messages import edit_message
910
10- from pathlib import Path
11-
12- date_format = '%d.%m.%Y %H:%M:%S'
13- logging_format = '[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s'
14-
15- handlers = [
16- logging .StreamHandler (sys .stdout )
17- ]
1811
12+ # setup logging with custom format and level `info`
1913logging .basicConfig (
20- format = logging_format ,
21- datefmt = date_format ,
22- handlers = handlers ,
14+ format = '[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s' ,
15+ datefmt = '%d.%m.%Y %H:%M:%S' ,
16+ handlers = [ logging . StreamHandler ( sys . stdout )] ,
2317 level = logging .INFO
2418)
2519
20+ # global aiohttp session
21+ http = None
22+
23+
2624async def get_weather (city : str ) -> str :
27- response = requests .get (f"https://ru.wttr.in/{ city } ?Q&T&format=3" )
28- return response .text
25+ global http
26+ if not http :
27+ http = aiohttp .ClientSession ()
28+ response = await http .get (f"https://ru.wttr.in/{ city } ?Q&T&format=3" )
29+ return await response .text ()
2930
3031
3132async def packet_callback (client : MaxClient , packet : dict ):
@@ -51,26 +52,29 @@ async def packet_callback(client: MaxClient, packet: dict):
5152
5253async def main ():
5354 client = MaxClient ()
54-
5555 await client .connect ()
5656
57- login_token_file = Path ('login_token .txt' )
57+ session_file = Path ('max_session .txt' )
5858
59- if login_token_file .exists ():
60- login_token_from_file = login_token_file .read_text (encoding = 'utf-8' ).strip ()
61- try :
62- await client .login_by_token (login_token_from_file )
63- except :
64- print ("Couldn't login by token. Falling back to SMS login" )
65-
66- else :
67- phone_number = input ('Your phone number: ' )
68- sms_login_token = await client .send_code (phone_number )
59+ if not session_file .exists ():
60+ phone_number = input ('Enter your phone number: ' )
61+ sms_token = await client .send_code (phone_number )
6962 sms_code = int (input ('Enter SMS code: ' ))
70- account_data = await client .sign_in (sms_login_token , sms_code )
63+ account_data = await client .sign_in (sms_token , sms_code )
7164
65+ device_id = client .device_id
7266 login_token = account_data ['payload' ]['tokenAttrs' ]['LOGIN' ]['token' ]
73- login_token_file .write_text (login_token , encoding = 'utf-8' )
67+
68+ # save device uuid and auth token delimited by newline
69+ session_file .write_text (f'{ device_id } \n { login_token } ' )
70+
71+ else :
72+ contents = session_file .read_text ()
73+ device_id , login_token = contents .split ('\n ' , maxsplit = 1 )
74+ try :
75+ await client .login_by_token (login_token , device_id )
76+ except :
77+ print ("Couldn't login by token" )
7478
7579 await client .set_callback (packet_callback )
7680
0 commit comments