Skip to content

Commit

Permalink
'Refactored by Sourcery'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourcery AI committed Oct 12, 2023
1 parent 0bf7684 commit d2f1f4d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 26 deletions.
30 changes: 8 additions & 22 deletions converters/tdata_to_telethon.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def __init__(self, data):
self.stream = io.BytesIO(data)

def read(self, n=None):
if n < 0:
n = 0
n = max(n, 0)
data = self.stream.read(n)
if n != 0 and len(data) == 0:
return None
Expand All @@ -53,38 +52,25 @@ def read_buffer(self):

def read_uint32(self):
data = self.read(4)
if data is None:
return None
return int.from_bytes(data, 'big')
return None if data is None else int.from_bytes(data, 'big')

def read_uint64(self):
data = self.read(8)
if data is None:
return None
return int.from_bytes(data, 'big')
return None if data is None else int.from_bytes(data, 'big')

def read_int32(self):
data = self.read(4)
if data is None:
return None
return int.from_bytes(data, 'big', signed=True)
return None if data is None else int.from_bytes(data, 'big', signed=True)


def create_local_key(passcode, salt):
if passcode:
iterations = 100_000
else:
iterations = 1
iterations = 100_000 if passcode else 1
_hash = hashlib.sha512(salt + passcode + salt).digest()
return hashlib.pbkdf2_hmac('sha512', _hash, salt, iterations, 256)


def prepare_aes_oldmtp(auth_key, msg_key, send):
if send:
x = 0
else:
x = 8

x = 0 if send else 8
sha1 = hashlib.sha1()
sha1.update(msg_key)
sha1.update(auth_key[x:][:32])
Expand Down Expand Up @@ -193,7 +179,7 @@ def build_session(dc, ip, port, key):
ip_bytes = ipaddress.ip_address(ip).packed
data = struct.pack('>B4sH256s', dc, ip_bytes, port, key)
encoded_data = urlsafe_b64encode(data).decode('ascii')
return '1' + encoded_data
return f'1{encoded_data}'


async def convert_tdata(path: Union[str, Path], work_dir: Path):
Expand Down Expand Up @@ -224,7 +210,7 @@ async def convert_tdata(path: Union[str, Path], work_dir: Path):


def save_config(work_dir: Path, phone: str, config: dict):
config_path = work_dir.joinpath(phone + '.json')
config_path = work_dir.joinpath(f'{phone}.json')
with open(config_path, 'w') as config_file:
json.dump(config, config_file)

Expand Down
3 changes: 1 addition & 2 deletions converters/telethon_to_pyrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,4 @@ async def get_converted_sting_session(session_data: StringSession, user_data: Us

encode_pack = base64.urlsafe_b64encode(bytes_pack)
decode_pack = encode_pack.decode()
sting_session = decode_pack.rstrip("=")
return sting_session
return decode_pack.rstrip("=")
4 changes: 2 additions & 2 deletions reactionbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
for logger_name in loggers:
logger = logging.getLogger(logger_name)
logger.setLevel(logging.INFO)
log_filepath = LOGS_DIR.joinpath(logger_name + '.log')
log_filepath = LOGS_DIR.joinpath(f'{logger_name}.log')
handler = logging.FileHandler(log_filepath)
handler.setFormatter(formatter)
logger.addHandler(handler)
Expand Down Expand Up @@ -198,7 +198,7 @@ async def try_convert(session_path: Path, config: Dict) -> bool:
config_file_path = session_path.with_suffix(suffix)
if config_file_path.exists():
await convertor.move_file_to_unnecessary(config_file_path)
error.warning('Preservation of the session failed ' + session_path.stem)
error.warning(f'Preservation of the session failed {session_path.stem}')
return False
except Exception:
error.warning(traceback.format_exc())
Expand Down

0 comments on commit d2f1f4d

Please sign in to comment.