Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery refactored master branch #1

Merged
merged 1 commit into from
Sep 6, 2021
Merged

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Sep 6, 2021

Branch master refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the master branch, then run:

git fetch origin sourcery/master
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

Comment on lines -83 to +84
else:
if not self.__is_cancelled:
self.__onDownloadError('Internal error occurred')
elif not self.__is_cancelled:
self.__onDownloadError('Internal error occurred')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TelegramDownloadHelper.__download refactored with the following changes:

Comment on lines -64 to +70
status = MirrorStatus.STATUS_WAITING
return MirrorStatus.STATUS_WAITING
elif download.is_paused:
status = MirrorStatus.STATUS_CANCELLED
return MirrorStatus.STATUS_CANCELLED
elif download.has_failed:
status = MirrorStatus.STATUS_FAILED
return MirrorStatus.STATUS_FAILED
else:
status = MirrorStatus.STATUS_DOWNLOADING
return status
return MirrorStatus.STATUS_DOWNLOADING
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AriaDownloadStatus.status refactored with the following changes:

Comment on lines -170 to +178
if reason == 'userRateLimitExceeded' or reason == 'dailyLimitExceeded':
if USE_SERVICE_ACCOUNTS:
self.switchServiceAccount()
LOGGER.info(f"Got: {reason}, Trying Again.")
return self.upload_file(file_path, file_name, mime_type, parent_id)
else:
if reason not in [
'userRateLimitExceeded',
'dailyLimitExceeded',
]:
raise err
if USE_SERVICE_ACCOUNTS:
self.switchServiceAccount()
LOGGER.info(f"Got: {reason}, Trying Again.")
return self.upload_file(file_path, file_name, mime_type, parent_id)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GoogleDriveHelper.upload_file refactored with the following changes:

Comment on lines -264 to +281
res = self.__service.files().copy(supportsAllDrives=True,fileId=file_id,body=body).execute()
return res
return (
self.__service.files()
.copy(supportsAllDrives=True, fileId=file_id, body=body)
.execute()
)

except HttpError as err:
if err.resp.get('content-type', '').startswith('application/json'):
reason = json.loads(err.content).get('error').get('errors')[0].get('reason')
if reason == 'userRateLimitExceeded' or reason == 'dailyLimitExceeded':
if USE_SERVICE_ACCOUNTS:
self.switchServiceAccount()
LOGGER.info(f"Got: {reason}, Trying Again.")
return self.copyFile(file_id,dest_id)
else:
if reason not in ['userRateLimitExceeded', 'dailyLimitExceeded']:
raise err

if USE_SERVICE_ACCOUNTS:
self.switchServiceAccount()
LOGGER.info(f"Got: {reason}, Trying Again.")
return self.copyFile(file_id,dest_id)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GoogleDriveHelper.copyFile refactored with the following changes:

Comment on lines -475 to +527
if response["files"]:
content_count = 0
self.telegraph_content = []
self.path = []
msg += f'<h4>Results : {fileName}</h4><br>@LoaderXbot #ProjektX<br><br>'
if not response["files"]:
return "No Result Found :(", None

for file in response.get('files', []):
if file.get('mimeType') == "application/vnd.google-apps.folder": # Detect Whether Current Entity is a Folder or File.
msg += f"⁍<code>{file.get('name')}<br>(folder📁)</code><br>" \
f"<b><a href='https://drive.google.com/drive/folders/{file.get('id')}'>Drive Link</a></b>"
if INDEX_URL is not None:
url_path = requests.utils.quote(f'{file.get("name")}')
url = f'{INDEX_URL}/{url_path}/'
msg += f' <b>| <a href="{url}">Index Link</a></b>'
content_count = 0
self.telegraph_content = []
self.path = []
msg += f'<h4>Results : {fileName}</h4><br>@LoaderXbot #ProjektX<br><br>'

else:
msg += f"⁍<code>{file.get('name')}<br>({get_readable_file_size(int(file.get('size')))})📄</code><br>" \
f"<b><a href='https://drive.google.com/uc?id={file.get('id')}&export=download'>Drive Link</a></b>"
if INDEX_URL is not None:
url_path = requests.utils.quote(f'{file.get("name")}')
url = f'{INDEX_URL}/{url_path}'
msg += f' <b>| <a href="{url}">Index Link</a></b>'
msg += '<br><br>'
content_count += 1
if content_count == TELEGRAPHLIMIT :
self.telegraph_content.append(msg)
msg = ""
content_count = 0

if msg != '':
for file in response.get('files', []):
if file.get('mimeType') == "application/vnd.google-apps.folder": # Detect Whether Current Entity is a Folder or File.
msg += f"⁍<code>{file.get('name')}<br>(folder📁)</code><br>" \
f"<b><a href='https://drive.google.com/drive/folders/{file.get('id')}'>Drive Link</a></b>"
if INDEX_URL is not None:
url_path = requests.utils.quote(f'{file.get("name")}')
url = f'{INDEX_URL}/{url_path}/'
msg += f' <b>| <a href="{url}">Index Link</a></b>'

else:
msg += f"⁍<code>{file.get('name')}<br>({get_readable_file_size(int(file.get('size')))})📄</code><br>" \
f"<b><a href='https://drive.google.com/uc?id={file.get('id')}&export=download'>Drive Link</a></b>"
if INDEX_URL is not None:
url_path = requests.utils.quote(f'{file.get("name")}')
url = f'{INDEX_URL}/{url_path}'
msg += f' <b>| <a href="{url}">Index Link</a></b>'
msg += '<br><br>'
content_count += 1
if content_count == TELEGRAPHLIMIT :
self.telegraph_content.append(msg)
msg = ""
content_count = 0

for content in self.telegraph_content :
self.path.append(telegra_ph.create_page(title = 'LoaderX',
html_content=content )['path'])
if msg != '':
self.telegraph_content.append(msg)

self.num_of_path = len(self.path)
if self.num_of_path > 1:
self.edit_telegraph()
for content in self.telegraph_content :
self.path.append(telegra_ph.create_page(title = 'LoaderX',
html_content=content )['path'])

msg = f"<b>Search Results For {fileName} 👇</b>"
buttons = button_builder.ButtonMaker()
buttons.buildbutton("HERE", f"https://telegra.ph/{self.path[0]}")
self.num_of_path = len(self.path)
if self.num_of_path > 1:
self.edit_telegraph()

return msg, InlineKeyboardMarkup(buttons.build_menu(1))
msg = f"<b>Search Results For {fileName} 👇</b>"
buttons = button_builder.ButtonMaker()
buttons.buildbutton("HERE", f"https://telegra.ph/{self.path[0]}")

else :
return "No Result Found :(", None
return msg, InlineKeyboardMarkup(buttons.build_menu(1))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GoogleDriveHelper.drive_list refactored with the following changes:

Comment on lines -59 to +61
if dlDetails.status() == MirrorStatus.STATUS_DOWNLOADING \
or dlDetails.status() == MirrorStatus.STATUS_WAITING:
if dlDetails.status() in [
MirrorStatus.STATUS_DOWNLOADING,
MirrorStatus.STATUS_WAITING,
]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function cancel_all refactored with the following changes:

LOGGER.info(msg_args[1])
LOGGER.info(link)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function deletefile refactored with the following changes:

@@ -116,7 +116,6 @@ def onDownloadError(self, error):
LOGGER.info(str(download_dict))
except Exception as e:
LOGGER.error(str(e))
pass
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MirrorListener.onDownloadError refactored with the following changes:

Comment on lines -202 to +211
if len(link) == 0:
if file is not None:
if file.mime_type != "application/x-bittorrent":
listener = MirrorListener(bot, update, isTar, tag)
tg_downloader = TelegramDownloadHelper(listener)
tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/')
sendStatusMessage(update, bot)
if len(Interval) == 0:
Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages))
return
else:
link = file.get_file().file_path
if len(link) == 0 and file is not None:
if file.mime_type != "application/x-bittorrent":
listener = MirrorListener(bot, update, isTar, tag)
tg_downloader = TelegramDownloadHelper(listener)
tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/')
sendStatusMessage(update, bot)
if len(Interval) == 0:
Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages))
return
else:
link = file.get_file().file_path
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _mirror refactored with the following changes:

if reply_to is not None:
tag = reply_to.from_user.username
else:
tag = None

tag = reply_to.from_user.username if reply_to is not None else None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _watch refactored with the following changes:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Sep 6, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 1.34%.

Quality metrics Before After Change
Complexity 12.80 🙂 11.48 🙂 -1.32 👍
Method Length 61.18 🙂 60.52 ⭐ -0.66 👍
Working memory 10.01 😞 9.85 🙂 -0.16 👍
Quality 59.37% 🙂 60.71% 🙂 1.34% 👍
Other metrics Before After Change
Lines 2328 2290 -38
Changed files Quality Before Quality After Quality Change
gen_sa_accounts.py 33.60% 😞 34.85% 😞 1.25% 👍
generate_drive_token.py 60.29% 🙂 63.29% 🙂 3.00% 👍
bot/init.py 44.30% 😞 48.40% 😞 4.10% 👍
bot/helper/ext_utils/bot_utils.py 81.72% ⭐ 82.31% ⭐ 0.59% 👍
bot/helper/ext_utils/db_handler.py 82.84% ⭐ 86.21% ⭐ 3.37% 👍
bot/helper/ext_utils/fs_utils.py 65.73% 🙂 65.74% 🙂 0.01% 👍
bot/helper/mirror_utils/download_utils/aria2_download.py 79.66% ⭐ 80.58% ⭐ 0.92% 👍
bot/helper/mirror_utils/download_utils/direct_link_generator.py 76.15% ⭐ 76.19% ⭐ 0.04% 👍
bot/helper/mirror_utils/download_utils/telegram_downloader.py 82.91% ⭐ 83.03% ⭐ 0.12% 👍
bot/helper/mirror_utils/status_utils/aria_download_status.py 92.58% ⭐ 93.11% ⭐ 0.53% 👍
bot/helper/mirror_utils/upload_utils/gdriveTools.py 50.90% 🙂 52.19% 🙂 1.29% 👍
bot/helper/telegram_helper/message_utils.py 74.30% 🙂 74.59% 🙂 0.29% 👍
bot/modules/authorize.py 57.65% 🙂 60.43% 🙂 2.78% 👍
bot/modules/cancel_mirror.py 47.70% 😞 50.86% 🙂 3.16% 👍
bot/modules/delete.py 75.26% ⭐ 75.51% ⭐ 0.25% 👍
bot/modules/mirror.py 50.79% 🙂 51.45% 🙂 0.66% 👍
bot/modules/watch.py 73.18% 🙂 74.86% 🙂 1.68% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
gen_sa_accounts.py _create_accounts._create_remaining_accounts._list_sas._delete_sas.serviceaccountfactory 52 ⛔ 420 ⛔ 26 ⛔ 5.55% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
bot/modules/mirror.py _mirror 25 😞 247 ⛔ 18 ⛔ 23.41% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
bot/helper/mirror_utils/upload_utils/gdriveTools.py GoogleDriveHelper.upload_file 21 😞 278 ⛔ 19 ⛔ 23.79% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
bot/helper/mirror_utils/upload_utils/gdriveTools.py GoogleDriveHelper.drive_list 16 🙂 295 ⛔ 15 😞 30.63% 😞 Try splitting into smaller methods. Extract out complex expressions
bot/modules/mirror.py MirrorListener.onDownloadComplete 11 🙂 267 ⛔ 15 😞 35.98% 😞 Try splitting into smaller methods. Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Help us improve this quality report!

for i in range(count):
for _ in range(count):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _create_accounts refactored with the following changes:

for i in range(count):
for _ in range(count):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _create_accounts._create_remaining_accounts._create_projects refactored with the following changes:

Comment on lines -148 to -152
index = 0
for j in current_key_dump:
for index, j in enumerate(current_key_dump):
with open(f'{path}/{index}.json', 'w+') as f:
f.write(j[1])
index += 1
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _create_accounts._create_remaining_accounts._list_sas._create_sa_keys refactored with the following changes:

Comment on lines -201 to +199
while projs == None:
while projs is None:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _create_accounts._create_remaining_accounts._list_sas._delete_sas.serviceaccountfactory refactored with the following changes:

Comment on lines -312 to +317
i = 0
print('Select a credentials file below.')
inp_options = [str(i) for i in list(range(1, len(options) + 1))] + options
while i < len(options):
for i in range(len(options)):
print(' %d) %s' % (i + 1, options[i]))
i += 1
inp = None
while True:
inp = input('> ')
if inp in inp_options:
break
if inp in options:
args.credentials = inp
else:
args.credentials = options[int(inp) - 1]
args.credentials = inp if inp in options else options[int(inp) - 1]
print('Use --credentials %s next time to use this credentials file.' % args.credentials)
if args.quick_setup:
opt = '*'
if args.new_only:
opt = '~'
opt = '~' if args.new_only else '*'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 312-331 refactored with the following changes:

Comment on lines 66 to 74
if self.err :
if self.err:
return "There's some error check log for details"
else:
sql = 'UPDATE users SET sudo = FALSE where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
SUDO_USERS.remove(chat_id)
return 'Successfully removed from Sudo' No newline at end of file
sql = 'UPDATE users SET sudo = FALSE where uid = {};'.format(chat_id)
self.cur.execute(sql)
self.conn.commit()
self.disconnect()
SUDO_USERS.remove(chat_id)
return 'Successfully removed from Sudo'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function DbManger.db_rmsudo refactored with the following changes:

mime_type = mime_type if mime_type else "text/plain"
mime_type = mime_type or "text/plain"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_mime_type refactored with the following changes:

else:
if dl: threading.Thread(target=dl.getListener().onDownloadComplete).start()
elif dl: threading.Thread(target=dl.getListener().onDownloadComplete).start()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AriaDownloadHelper.__onDownloadComplete refactored with the following changes:

Comment on lines -75 to +78
reply = "`No Yandex.Disk links found`\n"
return reply
return "`No Yandex.Disk links found`\n"
api = 'https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key={}'
try:
dl_url = requests.get(api.format(link)).json()['href']
return dl_url
return requests.get(api.format(link)).json()['href']
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function yandex_disk refactored with the following changes:

Comment on lines -100 to +98
dl_url = data['download']
return dl_url
return data['download']
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function cm_ru refactored with the following changes:

Comment on lines -112 to +109
dl_url = info.get('href')
return dl_url
return info.get('href')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function mediafire refactored with the following changes:

Comment on lines -143 to +139
dl_url = download.headers["location"]
return dl_url
return download.headers["location"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function github refactored with the following changes:

Comment on lines -83 to +84
else:
if not self.__is_cancelled:
self.__onDownloadError('Internal error occurred')
elif not self.__is_cancelled:
self.__onDownloadError('Internal error occurred')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TelegramDownloadHelper.__download refactored with the following changes:

Comment on lines -64 to +70
status = MirrorStatus.STATUS_WAITING
return MirrorStatus.STATUS_WAITING
elif download.is_paused:
status = MirrorStatus.STATUS_CANCELLED
return MirrorStatus.STATUS_CANCELLED
elif download.has_failed:
status = MirrorStatus.STATUS_FAILED
return MirrorStatus.STATUS_FAILED
else:
status = MirrorStatus.STATUS_DOWNLOADING
return status
return MirrorStatus.STATUS_DOWNLOADING
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function AriaDownloadStatus.status refactored with the following changes:

Comment on lines -170 to +178
if reason == 'userRateLimitExceeded' or reason == 'dailyLimitExceeded':
if USE_SERVICE_ACCOUNTS:
self.switchServiceAccount()
LOGGER.info(f"Got: {reason}, Trying Again.")
return self.upload_file(file_path, file_name, mime_type, parent_id)
else:
if reason not in [
'userRateLimitExceeded',
'dailyLimitExceeded',
]:
raise err
if USE_SERVICE_ACCOUNTS:
self.switchServiceAccount()
LOGGER.info(f"Got: {reason}, Trying Again.")
return self.upload_file(file_path, file_name, mime_type, parent_id)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function GoogleDriveHelper.upload_file refactored with the following changes:

Comment on lines -59 to +61
if dlDetails.status() == MirrorStatus.STATUS_DOWNLOADING \
or dlDetails.status() == MirrorStatus.STATUS_WAITING:
if dlDetails.status() in [
MirrorStatus.STATUS_DOWNLOADING,
MirrorStatus.STATUS_WAITING,
]:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function cancel_all refactored with the following changes:

LOGGER.info(msg_args[1])
LOGGER.info(link)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function deletefile refactored with the following changes:

@@ -116,7 +116,6 @@ def onDownloadError(self, error):
LOGGER.info(str(download_dict))
except Exception as e:
LOGGER.error(str(e))
pass
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MirrorListener.onDownloadError refactored with the following changes:

Comment on lines -202 to +211
if len(link) == 0:
if file is not None:
if file.mime_type != "application/x-bittorrent":
listener = MirrorListener(bot, update, isTar, tag)
tg_downloader = TelegramDownloadHelper(listener)
tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/')
sendStatusMessage(update, bot)
if len(Interval) == 0:
Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages))
return
else:
link = file.get_file().file_path
if len(link) == 0 and file is not None:
if file.mime_type != "application/x-bittorrent":
listener = MirrorListener(bot, update, isTar, tag)
tg_downloader = TelegramDownloadHelper(listener)
tg_downloader.add_download(reply_to, f'{DOWNLOAD_DIR}{listener.uid}/')
sendStatusMessage(update, bot)
if len(Interval) == 0:
Interval.append(setInterval(DOWNLOAD_STATUS_UPDATE_INTERVAL, update_all_messages))
return
else:
link = file.get_file().file_path
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _mirror refactored with the following changes:

if reply_to is not None:
tag = reply_to.from_user.username
else:
tag = None

tag = reply_to.from_user.username if reply_to is not None else None
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _watch refactored with the following changes:

@YaMraaJ-debug YaMraaJ-debug merged commit ef7595b into master Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant