def cron_thread(): Log("Cron thread started!") time = Timer() while True: Log("Running cron!") time.start() cron_cursor = mydb.cursor() #create cursor specifically for cron jobs cache_user_ids(cron_cursor) cache_ranks(cron_cursor) calculate_cp(cron_cursor) max_star_count_ban(cron_cursor) cache_comment_bans(cron_cursor) cache_server_stats(cron_cursor) cron_cursor.close() #close it after all is done Log(f"Cron done! Took {round(time.end(),2)}s") pytime.sleep(UserConfig["CronThreadDelay"])
def cache_user_ids(cron_cursor): """Caches all UserIDs from table. Used to remove the query from AIDToUID.""" Log("Caching UserIDs.") cron_cursor.execute("SELECT extID, userID FROM users") UserIDs = cron_cursor.fetchall() for a in UserIDs: UserIDCache[int(a[0])] = a[1]
def ImportGDPySDatabase(cursor): """Imports the GDPyS MySQL database.""" if path.exists("GDPyS.sql"): Log("Importing GDPyS database!") ExecuteSQLFile(cursor, "GDPyS.sql") Success("Successfully imported database!") else: Fail("SQL file is missing! Make sure GDPyS.sql exists!") raise FileNotFoundError
def Connect(self): """Sets up the bot to be able to be used.""" if not self._CheckBot(): Log("Bot not found! Creating new account for it!") self._RegitsterBot() self.BotID = self._FetchID() self.Connected = True self._SetUserId()
def ExecuteSQLFile(cursor, sql_file): """Executes an SQL file. Credit: https://stackoverflow.com/a/19159041""" Log(f"Executing file {sql_file}") statement = "" for line in open(sql_file): if re.match(r'--', line): # ignore sql comment lines continue if not re.search(r';$', line): # keep appending lines that don't end in ';' statement = statement + line else: # when you get a line ending in ';' then exec statement and reset for next statement statement = statement + line #print "\n\n[DEBUG] Executing SQL statement:\n%s" % (statement) try: cursor.execute(statement) except Exception as e: Fail(f"Could now execute command! Err {e}") statement = ""
"end_time" : ban[1], "reason" : ban[2] } time.end() logger.info(f"Done with {len(comment_bans)} comment bans cached! {time.ms_return()}ms") def cache_server_stats(cron_cursor): """Caches server statistics.""" time = Timer() time.start() logger.info("Caching server statictics...") cron_cursor.execute("SELECT COUNT(*) FROM accounts") ServerStatsCache["registered"] = cron_cursor.fetchone()[0] week_ago = round(pytime.time()) - 604800 cron_cursor.execute("SELECT COUNT(*) FROM accounts WHERE registerDate > %s", (week_ago,)) ServerStatsCache["registered_in_last_week"] = cron_cursor.fetchone()[0] cron_cursor.execute("SELECT COUNT(*) FROM levels WHERE uploadDate > %s", (week_ago,)) ServerStatsCache["levels_in_last_week"] = cron_cursor.fetchone()[0] time.end() logger.info(f"Done! {time.ms_return()}ms") if __name__ == "__main__": Log("Would you like to start the cron job? (y/N)") a = input("") if a.lower() == "y": cron_thread()