Remove prints from most of the class members

poetry-migration v0.6
Kirill Snezhko 2022-04-19 16:38:21 +03:00
rodzic df7af8e46e
commit 3c566c8e1e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 0D997A398363CC47
1 zmienionych plików z 52 dodań i 44 usunięć

Wyświetl plik

@ -1,19 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
# pylint: disable=invalid-name # pylint: disable=invalid-name
# Copyright (c) 2020 Kirill Snezhko # Copyright (c) 2020 Kirill Snezhko
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal # of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights # in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is # copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions: # furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all # The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software. # copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -21,9 +17,7 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
"""Main module""" """Main module"""
import argparse import argparse
import getpass import getpass
import json import json
@ -31,12 +25,12 @@ import random
import shutil import shutil
import urllib import urllib
import uuid import uuid
from typing import Iterator, Tuple
import requests import requests
from rich import box
import urls
import errors import errors
import urls
class HuamiAmazfit: class HuamiAmazfit:
"""Base class for logging in and receiving auth keys and GPS packs""" """Base class for logging in and receiving auth keys and GPS packs"""
@ -63,7 +57,6 @@ class HuamiAmazfit:
def get_access_token(self) -> str: def get_access_token(self) -> str:
"""Get access token for log in""" """Get access token for log in"""
print(f"Getting access token with {self.method} login method...")
if self.method == 'xiaomi': if self.method == 'xiaomi':
login_url = urls.URLS["login_xiaomi"] login_url = urls.URLS["login_xiaomi"]
@ -113,13 +106,10 @@ class HuamiAmazfit:
self.country_code = redirect_url_parameters['country_code'] self.country_code = redirect_url_parameters['country_code']
self.access_token = redirect_url_parameters['access'] self.access_token = redirect_url_parameters['access']
print("Token: {}".format(self.access_token))
return self.access_token return self.access_token
def login(self, external_token=None) -> None: def login(self, external_token=None) -> None:
"""Perform login and get app and login tokens""" """Perform login and get app and login tokens"""
print("Logging in...")
if external_token: if external_token:
self.access_token = external_token self.access_token = external_token
@ -157,12 +147,10 @@ class HuamiAmazfit:
if 'user_id' not in token_info: if 'user_id' not in token_info:
raise ValueError("No 'user_id' parameter in login data.") raise ValueError("No 'user_id' parameter in login data.")
self.user_id = token_info['user_id'] self.user_id = token_info['user_id']
print("Logged in! User id: {}".format(self.user_id)) return self.user_id
def get_wearables(self) -> dict: def get_wearables(self) -> dict:
"""Request a list of linked devices""" """Request a list of linked devices"""
print("Getting linked wearables...")
devices_url = urls.URLS['devices'].format(user_id=urllib.parse.quote(self.user_id)) devices_url = urls.URLS['devices'].format(user_id=urllib.parse.quote(self.user_id))
headers = urls.PAYLOADS['devices'] headers = urls.PAYLOADS['devices']
@ -205,7 +193,7 @@ class HuamiAmazfit:
return _wearables return _wearables
@staticmethod @staticmethod
def get_firmware(_wearable: dict) -> None: def get_firmware(_wearable: dict) -> Iterator[Tuple[str, str]]:
"""Check and download updates for the furmware and fonts""" """Check and download updates for the furmware and fonts"""
fw_url = urls.URLS["fw_updates"] fw_url = urls.URLS["fw_updates"]
params = urls.PAYLOADS["fw_updates"] params = urls.PAYLOADS["fw_updates"]
@ -231,15 +219,8 @@ class HuamiAmazfit:
links.append(fw_response['fontUrl']) links.append(fw_response['fontUrl'])
hashes.append(fw_response['fontMd5']) hashes.append(fw_response['fontMd5'])
if not links: for link, hash_sum in zip(links, hashes):
print("No updates found!") yield link, hash_sum
else:
for link, hash_sum in zip(links, hashes):
file_name = link.split('/')[-1]
print(f"Downloading {file_name} with MD5-hash {hash_sum}...")
with requests.get(link, stream=True) as r:
with open(file_name, 'wb') as f:
shutil.copyfileobj(r.raw, f)
def get_gps_data(self) -> None: def get_gps_data(self) -> None:
"""Download GPS packs: almanac and AGPS""" """Download GPS packs: almanac and AGPS"""
@ -269,12 +250,8 @@ class HuamiAmazfit:
data['login_token'] = self.login_token data['login_token'] = self.login_token
response = requests.post(logout_url, data=data) response = requests.post(logout_url, data=data)
logout_result = response.json() logout_result = response.json()['result']
return logout_result
if logout_result['result'] == 'ok':
print("\nLogged out.")
else:
print("\nError logging out.")
if __name__ == "__main__": if __name__ == "__main__":
@ -340,9 +317,16 @@ if __name__ == "__main__":
device = HuamiAmazfit(method=args.method, device = HuamiAmazfit(method=args.method,
email=args.email, email=args.email,
password=args.password) password=args.password)
device.get_access_token()
device.login()
print(f"Getting access token with '{args.method}' login method...")
access_token = device.get_access_token()
print(f"Token: {access_token}")
print("Logging in...")
user_id = device.login(external_token=access_token)
print(f"Logged in! User id: {user_id}")
print("Getting linked wearables...")
wearables = [] wearables = []
if args.bt_keys or args.all: if args.bt_keys or args.all:
wearables = device.get_wearables() wearables = device.get_wearables()
@ -354,8 +338,12 @@ if __name__ == "__main__":
print(f"\u2551 Key: {wearable['auth_key']}") print(f"\u2551 Key: {wearable['auth_key']}")
print(footer) print(footer)
if args.gps or args.all:
device.get_gps_data()
if args.firmware: if args.firmware:
print("Downloading the firmware is untested and can brick your device. " footer = "\u2559" + "\u2500" * 12
print("\nDownloading the firmware is untested and can brick your device. "
"I am not responsible for any problems that might arise.") "I am not responsible for any problems that might arise.")
answer = input("Do you want to proceed? [yes/no] ") answer = input("Do you want to proceed? [yes/no] ")
if answer.lower() in ['yes', 'y', 'ye']: if answer.lower() in ['yes', 'y', 'ye']:
@ -363,19 +351,39 @@ if __name__ == "__main__":
if wearable_id == "-1": if wearable_id == "-1":
print("Be extremely careful with downloaded files!") print("Be extremely careful with downloaded files!")
for idx, wearable in enumerate(wearables): for idx, wearable in enumerate(wearables):
print(f"\nChecking for device {idx}...") print(f"\n\u2553\u2500\u2500\u2500Device {idx}")
device.get_firmware(wearable) for link, hash_sum in device.get_firmware(wearable):
if link and hash_sum:
file_name = link.split('/')[-1]
print(f"\u2551 File: {file_name}")
print(f"\u2551 Hash: {hash_sum}")
with requests.get(link, stream=True) as r:
with open(file_name, 'wb') as f:
shutil.copyfileobj(r.raw, f)
else:
print(f"\u2551 No updates found")
print(footer)
elif int(wearable_id) in range(0, len(wearables)): elif int(wearable_id) in range(0, len(wearables)):
device.get_firmware(wearables[int(wearable_id)]) print(f"\n\u2553\u2500\u2500\u2500Device {wearable_id}")
else: for link, hash_sum in device.get_firmware(wearables[int(wearable_id)]):
print("Wrong input!") if link and hash_sum:
file_name = link.split('/')[-1]
print(f"\u2551 File: {file_name}")
if args.gps or args.all: print(f"\u2551 Hash: {hash_sum}")
device.get_gps_data() with requests.get(link, stream=True) as r:
with open(file_name, 'wb') as f:
shutil.copyfileobj(r.raw, f)
else:
print(f"\u2551 No updates found")
print(footer)
if args.no_logout: if args.no_logout:
print("\nNo logout!") print("\nNo logout!")
print(f"app_token={device.app_token}\nlogin_token={device.login_token}") print(f"app_token={device.app_token}\nlogin_token={device.login_token}")
else: else:
device.logout() logout_result = device.logout()
if logout_result == 'ok':
print("\nLogged out.")
else:
print("\nError logging out.")