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

Update helper.py (Sourcery refactored) #24

Open
wants to merge 1 commit into
base: patch-1
Choose a base branch
from

Conversation

sourcery-ai[bot]
Copy link

@sourcery-ai sourcery-ai bot commented Oct 11, 2022

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:

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

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from lostb053 October 11, 2022 18:10
Comment on lines -209 to +219
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',
}

Copy link
Author

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:

Comment on lines -238 to +240
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"
Copy link
Author

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:

Comment on lines -249 to +256
((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 "")
)

Copy link
Author

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:

Comment on lines -273 to +309
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}",
)
],
)
)

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_btns refactored with the following changes:

Comment on lines -326 to +423
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}",
),
)
)

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_auth_btns refactored with the following changes:

Comment on lines -368 to +491
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)
Copy link
Author

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:

@sourcery-ai
Copy link
Author

sourcery-ai bot commented Oct 11, 2022

Sourcery Code Quality Report

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

Quality metrics Before After Change
Complexity 15.85 🙂 15.48 🙂 -0.37 👍
Method Length 133.55 😞 133.00 😞 -0.55 👍
Working memory 7.83 🙂 7.27 🙂 -0.56 👍
Quality 46.91% 😞 48.16% 😞 1.25% 👍
Other metrics Before After Change
Lines 375 467 92
Changed files Quality Before Quality After Quality Change
anibot/utils/helper.py 46.91% 😞 48.16% 😞 1.25% 👍

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

File Function Complexity Length Working Memory Quality Recommendation
anibot/utils/helper.py get_btns 30 😞 655 ⛔ 15 😞 18.09% ⛔ Refactor to reduce nesting. Try splitting into smaller methods. Extract out complex expressions
anibot/utils/helper.py control_user 30 😞 239 ⛔ 23.38% ⛔ Refactor to reduce nesting. Try splitting into smaller methods
anibot/utils/helper.py media_to_image 16 🙂 346 ⛔ 12 😞 33.47% 😞 Try splitting into smaller methods. Extract out complex expressions
anibot/utils/helper.py check_user 23 😞 142 😞 41.18% 😞 Refactor to reduce nesting. Try splitting into smaller methods
anibot/utils/helper.py get_auth_btns 12 🙂 176 😞 13 😞 43.68% 😞 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!

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.

0 participants