diff --git a/anibot/utils/helper.py b/anibot/utils/helper.py index ee7f364..d97434a 100644 --- a/anibot/utils/helper.py +++ b/anibot/utils/helper.py @@ -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', } + url = "https://graphql.anilist.co" return requests.post(url, json={"query": query, "variables": vars_}, headers=headers).json() @@ -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" def make_it_rw(time_stamp): @@ -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 "") ) + return tmp[:-2] def timestamp_today(inc=0): @@ -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}", + ) + ], + ) + ) + if media == "SCHEDULED": get_day_name = lambda day_id: datetime.fromtimestamp(timestamp_today(day_id)).strftime("%A") buttons.append([ @@ -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) @@ -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}", + ), + ) + ) + return btn @@ -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) async def remove_useless_elements():