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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 161 additions & 69 deletions anibot/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,17 @@ async def take_screen_shot(


async def return_json_senpai(query: str, vars_: dict, auth: bool = False, user: int = None):
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',
}

Comment on lines -209 to +219
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:

url = "https://graphql.anilist.co"
return requests.post(url, json={"query": query, "variables": vars_}, headers=headers).json()

Expand All @@ -235,8 +237,7 @@ def pos_no(no):
x = ep_.pop()
if ep_ != [] and ep_.pop()=='1':
return 'th'
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"
Comment on lines -238 to +240
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:



def make_it_rw(time_stamp):
Expand All @@ -246,12 +247,13 @@ def make_it_rw(time_stamp):
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
tmp = (
((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 "")
)

Comment on lines -249 to +256
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:

return tmp[:-2]

def timestamp_today(inc=0):
Expand All @@ -270,14 +272,41 @@ def get_btns(media, user: int, result: list, lsqry: str = None, lspage: int = No
qry = f"_{lsqry}" if lsqry is not None else ""
pg = f"_{lspage}" if lspage is not None else ""
if media == "ANIME" and sfw == "False":
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}",
)
],
)
)

Comment on lines -273 to +309
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:

if media == "SCHEDULED":
get_day_name = lambda day_id: datetime.fromtimestamp(timestamp_today(day_id)).strftime("%A")
buttons.append([
Expand All @@ -288,31 +317,81 @@ def get_btns(media, user: int, result: list, lsqry: str = None, lspage: int = No
buttons.append([InlineKeyboardButton("More Info", url=result[1][2])])
if media == "AIRING" and sfw == "False":
buttons.append([InlineKeyboardButton("More Info", url=result[1][0])])
if auth is True and media!="SCHEDULED" and sfw == "False":
if auth and media != "SCHEDULED" and sfw == "False":
auth_btns = get_auth_btns(media, user, result[2], lspage=lspage, lsqry=lsqry)
buttons.append(auth_btns)
if len(result)>3:
if result[3] == "None":
if result[4] != "None":
buttons.append([InlineKeyboardButton(text="Sequel", callback_data=f"btn_{result[4]}_{str(auth)}_{user}")])
else:
if result[4] != "None":
buttons.append([
InlineKeyboardButton(text="Prequel", callback_data=f"btn_{result[3]}_{str(auth)}_{user}"),
InlineKeyboardButton(text="Sequel", callback_data=f"btn_{result[4]}_{str(auth)}_{user}"),
])
else:
buttons.append([InlineKeyboardButton(text="Prequel", callback_data=f"btn_{result[3]}_{str(auth)}_{user}")])
if (lsqry is not None) and (len(result)!=1) and (result[1][1]!=1):
if lspage == 1:
buttons.append([InlineKeyboardButton(text="Next", callback_data=f"page_{media}{qry}_{int(lspage)+1}_{str(auth)}_{user}")])
elif lspage == result[1][1]:
buttons.append([InlineKeyboardButton(text="Prev", callback_data=f"page_{media}{qry}_{int(lspage)-1}_{str(auth)}_{user}")])
buttons.append(
[
InlineKeyboardButton(
text="Sequel",
callback_data=f"btn_{result[4]}_{auth}_{user}",
)
]
)

elif result[4] == "None":
buttons.append(
[
InlineKeyboardButton(
text="Prequel",
callback_data=f"btn_{result[3]}_{auth}_{user}",
)
]
)

else:
buttons.append([
InlineKeyboardButton(text="Prev", callback_data=f"page_{media}{qry}_{int(lspage)-1}_{str(auth)}_{user}"),
InlineKeyboardButton(text="Next", callback_data=f"page_{media}{qry}_{int(lspage)+1}_{str(auth)}_{user}"),
])
buttons.append(
[
InlineKeyboardButton(
text="Prequel",
callback_data=f"btn_{result[3]}_{auth}_{user}",
),
InlineKeyboardButton(
text="Sequel",
callback_data=f"btn_{result[4]}_{auth}_{user}",
),
]
)

if lspage == 1:
if (lsqry is not None) and (len(result)!=1) and (result[1][1]!=1):
buttons.append(
[
InlineKeyboardButton(
text="Next",
callback_data=f"page_{media}{qry}_{lspage + 1}_{auth}_{user}",
)
]
)

elif lspage == result[1][1]:
if (lsqry is not None) and (len(result)!=1) and (result[1][1]!=1):
buttons.append(
[
InlineKeyboardButton(
text="Prev",
callback_data=f"page_{media}{qry}_{lspage - 1}_{auth}_{user}",
)
]
)

elif (lsqry is not None) and (len(result)!=1) and (result[1][1]!=1):
buttons.append(
[
InlineKeyboardButton(
text="Prev",
callback_data=f"page_{media}{qry}_{lspage - 1}_{auth}_{user}",
),
InlineKeyboardButton(
text="Next",
callback_data=f"page_{media}{qry}_{lspage + 1}_{auth}_{user}",
),
]
)

return InlineKeyboardMarkup(buttons)


Expand All @@ -323,11 +402,25 @@ def get_auth_btns(media, user, data, lsqry: str = None, lspage: int = None):
if media=="CHARACTER":
btn.append(InlineKeyboardButton(text="Add to Favs" if data[1] is not True else "Remove from Favs", callback_data=f"fav_{media}_{data[0]}{qry}{pg}_{user}"))
else:
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}",
),
)
)

Comment on lines -326 to +423
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:

return btn


Expand Down Expand Up @@ -365,38 +458,37 @@ async def update_pics_cache():
await PIC_DB.drop()
await PIC_DB.insert_one({'_id': 'month', 'm': m})
for link in PIC_LS:
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)
Comment on lines -368 to +491
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:



async def remove_useless_elements():
Expand Down