def send_message_to_everyone(update, CallbackContext) -> None: global bot args = CallbackContext.args chatid = update.message.chat_id if Utils.admin_check(chatid) == False: send_message(chatid, "You're not authorized to do this", bot) return chatid_list = [] message = "" start_time = datetime.datetime.now() logging.info(f"{chatid} started sending a message to everyone") results = Utils.retrieve_query_results("SELECT DISTINCT CHAT_ID FROM CHATURBATE") for row in results: chatid_list.append(row[0]) for word in args: message += f"{word} " message = message[:-1] for x in chatid_list: send_message(x, message, bot) logging.info( f"{chatid} finished sending a message to everyone, took {(datetime.datetime.now() - start_time).total_seconds()} seconds")
def list_command(update, CallbackContext) -> None: global bot chatid = update.message.chat_id username_dict = {} followed_users = "" results = Utils.retrieve_query_results(f"SELECT USERNAME, ONLINE FROM CHATURBATE WHERE CHAT_ID='{chatid}'") if results != []: # an exception didn't happen for row in results: username=row[0] status=row[1] username_dict[username]=status for username, status in sorted(username_dict.items()): followed_users += f"{username}: " if status == "T": followed_users += "<b>online</b>\n" else: followed_users += "offline\n" if followed_users == "": send_message(chatid, "You aren't following any user", bot) else: send_message( chatid, f"You are currently following these {len(username_dict)} users:\n" + followed_users, bot, html=True)
def active_models(update, CallbackContext) -> None: global bot chatid = update.message.chat_id if Utils.admin_check(chatid) == False: send_message(chatid, "You're not authorized to do this", bot) return models_count = Utils.retrieve_query_results("SELECT COUNT(DISTINCT USERNAME) FROM CHATURBATE")[0][0] send_message(chatid,f"The active models are {models_count}",bot)
def active_users(update, CallbackContext) -> None: global bot chatid = update.message.chat_id if Utils.admin_check(chatid) == False: send_message(chatid, "You're not authorized to do this", bot) return users_count = Utils.retrieve_query_results("SELECT COUNT(CHAT_ID) FROM PREFERENCES")[0][0] send_message(chatid,f"The active users are {users_count}",bot)
def user_has_preferences(chatid: str) -> bool: """ Checks if user exists in the preferences table :param chatid: The chatid of the user who will be tested :return: True if it exists, False if it doesn't exist """ results = Utils.retrieve_query_results( f"SELECT * FROM PREFERENCES WHERE CHAT_ID={chatid}") if not results: return False else: return True
def get_user_notifications_sound_preference(chatid: str) -> bool: """ Retrieve the notifications preference of the user :param chatid: The chatid of the user who will be tested :return: The boolean value of the preference """ if not user_has_preferences(chatid): add_user_to_preferences(chatid) results = Utils.retrieve_query_results( f"SELECT NOTIFICATIONS_SOUND FROM PREFERENCES WHERE CHAT_ID={chatid}") if results[0][0] == 0: return False else: return True
def get_user_link_preview_preference(chatid: str) -> bool: """ Retrieve the link_preview preference of the user :param chatid: The chatid of the user who will be tested :return: The boolean value of the preference """ if not user_has_preferences(chatid): add_user_to_preferences(chatid) results = Utils.retrieve_query_results( f"SELECT LINK_PREVIEW FROM PREFERENCES WHERE CHAT_ID={chatid}") if results[0][0] == 0: return False else: return True
def remove(update, CallbackContext) -> None: global bot args = CallbackContext.args chatid = update.message.chat_id username_message_list = [] usernames_in_database = [] if len(args) < 1: send_message( chatid, "You need to specify an username to follow, use the command like /remove <b>test</b>\n You can also remove multiple users at the same time by separating them using a comma, like /remove <b>username1</b>,<b>username2</b>", bot, html=True ) return if len(args) > 1: for username in args: if username != "": username_message_list.append(Utils.sanitize_username(username).replace(",", "")) # len(args)==0 -> only one username or all in one line elif "," in args[0].lower(): for splitted_username in args[0].lower().replace(" ", "").rstrip().split(","): if splitted_username != "": username_message_list.append(Utils.sanitize_username(splitted_username)) else: username_message_list.append(Utils.sanitize_username(args[0])) results = Utils.retrieve_query_results(f"SELECT * FROM CHATURBATE WHERE CHAT_ID='{chatid}'") for row in results: usernames_in_database.append(row[0]) if "all" in username_message_list: Utils.exec_query( f"DELETE FROM CHATURBATE WHERE CHAT_ID='{chatid}'") send_message(chatid, "All usernames have been removed", bot) logging.info(f"{chatid} removed all usernames") else: for username in username_message_list: if username in usernames_in_database: Utils.exec_query(f"DELETE FROM CHATURBATE WHERE USERNAME='******' AND CHAT_ID='{chatid}'") send_message(chatid, f"{username} has been removed", bot) logging.info(f"{chatid} removed {username}") else: send_message(chatid, f"You aren't following {username}", bot)
def update_status() -> None: username_list = [] chat_and_online_dict={} # create a dictionary with usernames and online using distinct results = Utils.retrieve_query_results("SELECT DISTINCT USERNAME FROM CHATURBATE") for row in results: username_list.append(row[0]) # obtain chatid for username in username_list: results = Utils.retrieve_query_results(f"SELECT DISTINCT CHAT_ID, ONLINE FROM CHATURBATE WHERE USERNAME='******'") chat_and_online_dict[username]=results # assign (chatid,online) to every model # Threaded function for queue processing. def crawl(q, model_instances_dict): while not q.empty(): work = q.get() # fetch new work from the Queue username = Utils.sanitize_username(work[1]) model_instance=Model(username,autoupdate=False) model_instance.update_model_status() try: model_instance.update_model_image() except Exception: model_instance.model_image = None # set to None just to be secure Todo: this may be extra model_instances_dict[username] = model_instance # signal to the queue that task has been processed q.task_done() return True q = Queue(maxsize=0) # Populating Queue with tasks model_instances_dict = {} # load up the queue with the username_list to fetch and the index for each job (as a tuple): for index, value in enumerate(username_list): # need the index and the username in each queue item. q.put((index, value)) # Starting worker threads on queue processing for i in range(http_threads): worker = threading.Thread(target=crawl, args=(q, model_instances_dict), daemon=True) worker.start() time.sleep(wait_time) # avoid server spamming by time-limiting the start of requests # now we wait until the queue has been processed q.join() for username in username_list: model_instance = model_instances_dict[username] keyboard_with_link_preview = [[InlineKeyboardButton("Watch the live", url=f'http://chaturbate.com/{username}'), InlineKeyboardButton("Update stream image",callback_data='view_stream_image_callback_' + username)]] keyboard_without_link_preview = [ [InlineKeyboardButton("Watch the live", url=f'http://chaturbate.com/{username}')]] markup_with_link_preview = InlineKeyboardMarkup(keyboard_with_link_preview) markup_without_link_preview = InlineKeyboardMarkup(keyboard_without_link_preview) try: if model_instance.status != "error": for chatid_tuple in chat_and_online_dict[username]: chat_id=chatid_tuple[0] db_status=chatid_tuple[1] if model_instance.online and db_status == "F": if model_instance.status in {"away", "private", "hidden", "password"}: # assuming the user knows the password Utils.exec_query(f"UPDATE CHATURBATE SET ONLINE='T' WHERE USERNAME='******' AND CHAT_ID='{chat_id}'") send_message(chat_id, f"{username} is now <b>online</b>!\n<i>No link preview can be provided</i>", bot, html=True, markup=markup_without_link_preview) else: Utils.exec_query( f"UPDATE CHATURBATE SET ONLINE='T' WHERE USERNAME='******' AND CHAT_ID='{chat_id}'") if Preferences.get_user_link_preview_preference(chat_id) and model_instance.model_image != None: send_image(chat_id, model_instance.model_image, bot, markup=markup_with_link_preview, caption=f"{username} is now <b>online</b>!", html=True) else: send_message(chat_id,f"{username} is now <b>online</b>!",bot,html=True,markup=markup_without_link_preview) elif model_instance.online==False and db_status == "T": Utils.exec_query( f"UPDATE CHATURBATE SET ONLINE='F' WHERE USERNAME='******' AND CHAT_ID='{chat_id}'") send_message(chat_id, f"{username} is now <b>offline</b>", bot, html=True) if model_instance.status=="deleted": Utils.exec_query(f"DELETE FROM CHATURBATE WHERE USERNAME='******' AND CHAT_ID='{chat_id}'") send_message(chat_id, f"{username} has been removed because room has been deleted", bot) logging.info(f"{username} has been removed from {chat_id} because room has been deleted") elif model_instance.status=="banned": Utils.exec_query(f"DELETE FROM CHATURBATE WHERE USERNAME='******' AND CHAT_ID='{chat_id}'") send_message(chat_id, f"{username} has been removed because room has been banned", bot) logging.info(f"{username} has been removed from {chat_id} because has been banned") elif model_instance.status=="canceled": Utils.exec_query(f"DELETE FROM CHATURBATE WHERE USERNAME='******' AND CHAT_ID='{chat_id}'") send_message(chat_id, f"{username} has been removed because room has been canceled", bot) logging.info(f"{username} has been removed from {chat_id} because has been canceled") elif model_instance.status=="geoblocked": Utils.exec_query(f"DELETE FROM CHATURBATE WHERE USERNAME='******' AND CHAT_ID='{chat_id}'") send_message(chat_id, f"{username} has been removed because of geoblocking", bot) logging.info(f"{username} has been removed from {chat_id} because of geoblocking") except Exception as e: Utils.handle_exception(e)
def add(update, CallbackContext) -> None: global bot args = CallbackContext.args chatid = update.message.chat_id username_message_list = [] if len(args) < 1: send_message( chatid, "You need to specify an username to follow, use the command like /add <b>username</b>\n You can also add multiple users at the same time by separating them using a comma, like /add <b>username1</b>,<b>username2</b>", bot, html=True ) return # not lowercase usernames bug the api calls if len(args) > 1: for username in args: if username != "": username_message_list.append(Utils.sanitize_username(username).replace(",", "")) # len(args)==0 -> only one username or all in one line elif "," in args[0].lower(): for splitted_username in args[0].lower().replace(" ", "").rstrip().split(","): if splitted_username != "": username_message_list.append(Utils.sanitize_username(splitted_username)) else: username_message_list.append(Utils.sanitize_username(args[0])) username_message_list = list(dict.fromkeys(username_message_list)) # remove duplicate usernames usernames_in_database = [] # obtain present usernames results = Utils.retrieve_query_results(f"SELECT USERNAME FROM CHATURBATE WHERE CHAT_ID='{chatid}'") for row in results: usernames_in_database.append(row[0]) # 0 is unlimited usernames if len(usernames_in_database) + len(username_message_list) > user_limit and ( Utils.admin_check(chatid) == False != user_limit != 0): send_message(chatid, "You are trying to add more usernames than your limit permits, which is " + str(user_limit), bot) logging.info(f'{chatid} tried to add more usernames than his limit permits') return for username in username_message_list: model_instance=Model(username) if model_instance.status not in ('deleted', 'banned', 'geoblocked', 'canceled', 'error'): if username not in usernames_in_database: Utils.exec_query(f"INSERT INTO CHATURBATE VALUES ('{username}', '{chatid}', 'F')") send_message(chatid, f"{username} has been added", bot) logging.info(f'{chatid} added {username}') else: send_message(chatid,f"{username} has already been added",bot) elif model_instance.status=='deleted': send_message(chatid, f"{username} has not been added because is deleted", bot) logging.info(f"{chatid} could not add {username} because is deleted") elif model_instance.status=='banned': send_message(chatid, f"{username} has not been added because is banned", bot) logging.info(f"{chatid} could not add {username} because is banned") elif model_instance.status=='geoblocked': send_message(chatid, f"{username} has not been added because is geoblocked", bot) logging.info(f"{chatid} could not add {username} because is geoblocked") elif model_instance.status=='canceled': send_message(chatid, f"{username} was not added because it doesn't exist", bot) logging.info(f'{chatid} tried to add {username}, which does not exist') elif model_instance.status=='error': send_message(chatid, f"{username} was not added because an error happened", bot) logging.info(f'{chatid} could not add {username} because an error happened')