def start(self): if token == 'token': red('Please update your token in ./bot/constants.py') sys.exit(0) self.catch() self.bot.loop.create_task(self.cron()) self.bot.run(token)
async def display_cron( id_discord_server: int, db: DatabaseManager) -> Tuple[Optional[str], Optional[str]]: lang = await db.get_server_language(id_discord_server) users = await db.select_users(id_discord_server) for user in users: last = user['last_challenge_solve'] solved_user = await get_solved_challenges(user['rootme_username'], lang) if not solved_user or solved_user[-1]['name'] == last: continue blue(solved_user[-1]['name'] + " | " + last + "\n") next_chall = next_challenge_solved(solved_user, last) if next_chall is None: red(f'Error with {user} --> last chall: {solved_user[-1]["name"]}\n' ) continue name = f'New challenge solved by {user["rootme_username"]}' c = find_challenge(db, lang, next_chall['name']) green(f'{user} --> {c["name"]}') tosend = f' • {c["name"]} ({c["value"]} points)' tosend += f'\n • Difficulty: {c["difficulty"]}' tosend += f'\n • Date: {next_chall["date"]}' tosend += f'\n • New score: {next_chall["score_at_date"]}' await db.update_user_last_challenge(id_discord_server, user['rootme_username'], c['name']) return name, tosend return None, None
async def get_solved_challenges( id_user: int) -> Optional[response_profile_complete]: solved_challenges_data = await Parser.extract_rootme_profile_complete( id_user) if solved_challenges_data is None: red(f'Error trying to fetch solved challenges.') return None return solved_challenges_data['validations']
async def extract_json(url: str) -> response_content_type: data = await request_to(url) if data is None: red(url) else: green(url) if 'body' in data.keys(): data = data['body'] return data
async def get_cookies(): async with aiohttp.ClientSession() as session: data = dict(login=ROOTME_ACCOUNT_LOGIN, password=ROOTME_ACCOUNT_PASSWORD) async with session.post(f'{URL}/login', data=data, timeout=timeout) as response: if response.status == 200: content = await response.json(content_type=None) return dict(spip_session=content[0]['info']['spip_session']) red('Wrong credentials.') sys.exit(0)
async def display_by_blocks_duration(context: Context, tosend_list: List[str], color: int, duration_msg: str = '') \ -> None: for block in tosend_list: red(block) tosend = block['msg'] if block['user'] is None: embed_name = f"Challenges solved {duration_msg}" tosend = tosend_list[0]['msg'] await interrupt(context.message.channel, tosend, embed_color=color, embed_name=embed_name) return if tosend: embed_name = f"Challenges solved by {block['user']} {duration_msg}" await interrupt(context.message.channel, tosend, embed_color=color, embed_name=embed_name)
async def get_cookies(): async with aiohttp.ClientSession(cookie_jar=cookie_jar) as session: await asyncio.sleep(1) data = dict(login=ROOTME_ACCOUNT_LOGIN, password=ROOTME_ACCOUNT_PASSWORD) try: async with session.post(f'{URL}/login', data=data, timeout=timeout) as response: print(response) if response.status == 200: content = await response.json(content_type=None) return "Logged in" elif response.status == 429: # Too Many requests return await get_cookies() red('Wrong credentials.') sys.exit(0) except asyncio.TimeoutError: return await get_cookies()
""" """ await disp.reset_database(db, context) def start(self): if token == 'token': red('Please update your token in ./bot/constants.py') sys.exit(0) self.catch() self.bot.loop.create_task(self.cron()) self.bot.run(token) def init_rootme_challenges(): rootme_challenges = {} for lang in LANGS: loop = asyncio.get_event_loop() # event loop future = asyncio.ensure_future(get_categories(lang)) # tasks to do challenges = loop.run_until_complete(future) # loop until done rootme_challenges[lang] = challenges return rootme_challenges if __name__ == "__main__": rootme_challenges = init_rootme_challenges() if rootme_challenges is None: red('Cannot fetch RootMe challenges from the API.') sys.exit(0) db = DatabaseManager(FILENAME, rootme_challenges) bot = RootMeBot(db) bot.start()
async def extract_json(url: str) -> response_profile: data = await request_to(url) if data is None: red(url) return data
async def get_solved_challenges(user: str, lang: str): solved_challenges_data = await Parser.extract_rootme_stats(user, lang) if solved_challenges_data is None: red(f'user {user} name might have changed in rootme profile link') return None return solved_challenges_data['solved_challenges']