-
Notifications
You must be signed in to change notification settings - Fork 112
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
Update helper.py (Sourcery refactored) #24
base: patch-1
Are you sure you want to change the base?
Conversation
if auth is False: | ||
if not auth: | ||
url = "https://graphql.anilist.co" | ||
return requests.post(url, json={"query": query, "variables": vars_}).json() | ||
else: | ||
headers = { | ||
'Authorization': 'Bearer ' + str((await AUTH_USERS.find_one({"id": int(user)}))['token']), | ||
'Authorization': 'Bearer ' | ||
+ str((await AUTH_USERS.find_one({"id": user}))['token']), | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/json', | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function return_json_senpai
refactored with the following changes:
- Simplify comparison to boolean (
simplify-boolean-comparison
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
th = "st" if x == "1" else "nd" if x == "2" else "rd" if x == "3" else "th" | ||
return th | ||
return "st" if x == "1" else "nd" if x == "2" else "rd" if x == "3" else "th" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function pos_no
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
((str(days) + " Days, ") if days else "") | ||
+ ((str(hours) + " Hours, ") if hours else "") | ||
+ ((str(minutes) + " Minutes, ") if minutes else "") | ||
+ ((str(seconds) + " Seconds, ") if seconds else "") | ||
+ ((str(milliseconds) + " ms, ") if milliseconds else "") | ||
(f"{str(days)} Days, " if days else "") | ||
+ (f"{str(hours)} Hours, " if hours else "") | ||
+ (f"{str(minutes)} Minutes, " if minutes else "") | ||
+ (f"{str(seconds)} Seconds, " if seconds else "") | ||
+ (f"{str(milliseconds)} ms, " if milliseconds else "") | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function make_it_rw
refactored with the following changes:
- Use f-string instead of string concatenation [×5] (
use-fstring-for-concatenation
)
buttons.append([ | ||
InlineKeyboardButton(text="Characters", callback_data=f"char_{result[2][0]}_ANI{qry}{pg}_{str(auth)}_1_{user}"), | ||
InlineKeyboardButton(text="Description", callback_data=f"desc_{result[2][0]}_ANI{qry}{pg}_{str(auth)}_{user}"), | ||
InlineKeyboardButton(text="List Series", callback_data=f"ls_{result[2][0]}_ANI{qry}{pg}_{str(auth)}_{user}"), | ||
]) | ||
buttons.append( | ||
[ | ||
InlineKeyboardButton( | ||
text="Characters", | ||
callback_data=f"char_{result[2][0]}_ANI{qry}{pg}_{auth}_1_{user}", | ||
), | ||
InlineKeyboardButton( | ||
text="Description", | ||
callback_data=f"desc_{result[2][0]}_ANI{qry}{pg}_{auth}_{user}", | ||
), | ||
InlineKeyboardButton( | ||
text="List Series", | ||
callback_data=f"ls_{result[2][0]}_ANI{qry}{pg}_{auth}_{user}", | ||
), | ||
] | ||
) | ||
|
||
if media == "CHARACTER": | ||
buttons.append([InlineKeyboardButton("Description", callback_data=f"desc_{result[2][0]}_CHAR{qry}{pg}_{str(auth)}_{user}")]) | ||
buttons.append([InlineKeyboardButton("List Series", callback_data=f"lsc_{result[2][0]}{qry}{pg}_{str(auth)}_{user}")]) | ||
buttons.extend( | ||
( | ||
[ | ||
InlineKeyboardButton( | ||
"Description", | ||
callback_data=f"desc_{result[2][0]}_CHAR{qry}{pg}_{auth}_{user}", | ||
) | ||
], | ||
[ | ||
InlineKeyboardButton( | ||
"List Series", | ||
callback_data=f"lsc_{result[2][0]}{qry}{pg}_{auth}_{user}", | ||
) | ||
], | ||
) | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_btns
refactored with the following changes:
- Remove unnecessary calls to
str()
from formatted values in f-strings [×13] (remove-str-from-fstring
) - Merge consecutive list appends into a single extend (
merge-list-appends-into-extend
) - Simplify comparison to boolean (
simplify-boolean-comparison
) - Merge else clause's nested if statement into elif [×2] (
merge-else-if-into-elif
) - Swap if/else branches (
swap-if-else-branches
) - Swap positions of nested conditionals (
swap-nested-ifs
) - Remove unnecessary casts to int, str, float or bool [×4] (
remove-unnecessary-cast
)
btn.append(InlineKeyboardButton(text="Add to Favs" if data[3] is not True else "Remove from Favs", callback_data=f"fav_{media}_{data[0]}{qry}{pg}_{user}")) | ||
btn.append(InlineKeyboardButton( | ||
text="Add to List" if data[1] is False else "Update in List", | ||
callback_data=f"lsadd_{media}_{data[0]}{qry}{pg}_{user}" if data[1] is False else f"lsupdt_{media}_{data[0]}_{data[2]}{qry}{pg}_{user}" | ||
)) | ||
btn.extend( | ||
( | ||
InlineKeyboardButton( | ||
text="Add to Favs" | ||
if data[3] is not True | ||
else "Remove from Favs", | ||
callback_data=f"fav_{media}_{data[0]}{qry}{pg}_{user}", | ||
), | ||
InlineKeyboardButton( | ||
text="Add to List" | ||
if data[1] is False | ||
else "Update in List", | ||
callback_data=f"lsadd_{media}_{data[0]}{qry}{pg}_{user}" | ||
if data[1] is False | ||
else f"lsupdt_{media}_{data[0]}_{data[2]}{qry}{pg}_{user}", | ||
), | ||
) | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_auth_btns
refactored with the following changes:
- Merge consecutive list appends into a single extend (
merge-list-appends-into-extend
)
if (await PIC_DB.find_one({'_id': link})) is None: | ||
await PIC_DB.insert_one({'_id': link}) | ||
if await PIC_DB.find_one({'_id': link}) is not None: | ||
continue | ||
await PIC_DB.insert_one({'_id': link}) | ||
try: | ||
me = await user.send_photo("me", f"{link}?a={ts}") | ||
msg = await user.send_photo("me", link) | ||
except ConnectionError: | ||
await asyncio.sleep(5) | ||
me = await user.send_photo("me", f"{link}?a={ts}") | ||
msg = await user.send_photo("me", link) | ||
await asyncio.sleep(7) | ||
dls1 = await user.download_media( | ||
msg.photo, | ||
file_name=DOWN_PATH + link.split("/").pop()+'(1).png', | ||
) | ||
dls2 = await user.download_media( | ||
me.photo, | ||
file_name=DOWN_PATH + link.split("/").pop()+'(2).png', | ||
) | ||
await asyncio.sleep(10) | ||
with open(dls1, 'rb') as p1: | ||
b1 = p1.read() | ||
with open(dls2, 'rb') as p2: | ||
b2 = p2.read() | ||
await user.delete_messages("me", [me.message_id, msg.message_id]) | ||
if b1!=b2: | ||
try: | ||
me = await user.send_photo("me", link+f"?a={ts}") | ||
msg = await user.send_photo("me", link) | ||
await user.send_message("webpagebot", link) | ||
except ConnectionError: | ||
await asyncio.sleep(5) | ||
me = await user.send_photo("me", link+f"?a={ts}") | ||
msg = await user.send_photo("me", link) | ||
await asyncio.sleep(7) | ||
dls1 = await user.download_media( | ||
msg.photo, | ||
file_name=DOWN_PATH + link.split("/").pop()+'(1).png', | ||
) | ||
dls2 = await user.download_media( | ||
me.photo, | ||
file_name=DOWN_PATH + link.split("/").pop()+'(2).png', | ||
) | ||
await asyncio.sleep(10) | ||
with open(dls1, 'rb') as p1: | ||
b1 = p1.read() | ||
with open(dls2, 'rb') as p2: | ||
b2 = p2.read() | ||
await user.delete_messages("me", [me.message_id, msg.message_id]) | ||
if b1!=b2: | ||
try: | ||
await user.send_message("webpagebot", link) | ||
except ConnectionError: | ||
await asyncio.sleep(5) | ||
await user.send_message("webpagebot", link) | ||
else: | ||
continue | ||
await user.send_message("webpagebot", link) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function update_pics_cache
refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 1.25%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
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! |
Pull Request #23 refactored by Sourcery.
If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
NOTE: As code is pushed to the original Pull Request, Sourcery will
re-run and update (force-push) this Pull Request with new refactorings as
necessary. If Sourcery finds no refactorings at any point, this Pull Request
will be closed automatically.
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
patch-1
branch, then run:Help us improve this pull request!