def check_filename(self, update, context): """ This method represents the 2° step of the download conversation. It asks the user to insert a valid filename for the video (the filename have to be from 5 to 254 characters of length). This step can be skipped if the config file the "automaticFilename" flag is set True or if the "noDownloadWizard" flag is set at True. """ # Create unique notifier for this user (every update -> different notifier) notifier = Notifier(update, self.BOT) # Get last message sent by the user filename = update.message.text if 4 < len(filename) < 255: # Check with regex if it's a valid filename update.message.reply_text("Shitty name but is okay..") # Save filename self.DOWNLOAD_REQUEST.filename = filename # Pass to another step update.message.reply_text(self.DOWNLOAD_CONFIRMATION) return self.START_DOWNLOAD else: notifier.notify_error( "This name is not valid! The name have a minimum 4 characters and a maximum of 254 characters" ) # Ask again return self.SET_FILE_NAME
def download(self, update, context): """ This method handles the '/download' command. It start a conversation (3 steps) with the user to determine the URL of the video resource to download and the filename (if set in the config file) """ print("[Bot] Received download command from", self.get_user_id(update)) # Create unique notifier for this user (every update -> different notifier) notifier = Notifier(update, self.BOT) if not self.get_user_id(update) in self.DOWNLOAD_PROCESSES: if self.CONFIG["noDownloadWizard"]: # For fast downloading change automatic filename to true self.CONFIG["automaticFilename"] = True notifier.notify_information("If you wanna abort the download process just type: '/cancel'") else: # Start the download wizard notifier.notify_information("Oh hello! I'm here to guide you inside the downloading wizard!.\ PS: you can exit this wizard any time you want, you have just to type '/cancel'") # Go to the first step notifier.notify_custom("1️", "Send me a video url") return self.SET_DOWNLOAD_URL else: notifier.notify_error("You are downloading already a resource." "Multiple download are currently disabled. Ask the developer for extra information.")
def cancel_download_wizard(self, update, context): """ This method is called when the conversation abort command is detected. It quits the conversation between the user and the bot and resets the DownloadRequest object. """ notifier = Notifier(update, self.BOT) # Exit the download wizard and reset the 'DOWNLOAD_REQUEST' attribute notifier.notify_success("Exited downloading wizard!") print("[Download cancel] User {} aborted download wizard".format(self.get_user_id(update))) self.DOWNLOAD_REQUEST = DownloadRequest(None, None) return ConversationHandler.END
def thumbnail_set_video_name(self, update, context): """ This method represents the 1° step of the thumbnail conversation. It asks the user to insert a valid name for the video (the filename have to be from 5 to 254 characters of length). """ notifier = Notifier(update, self.BOT) # Get last message sent by the user msg = update.message.text.strip() print("[Thumbnail] User {} select a message: {}".format( self.get_user_id(update), msg )) if 4 < len(msg) < 255: self.THUMBNAILS[self.get_user_id(update)].set_title(msg) notifier.notify_success("'{}' is a valid video name".format(msg)) notifier.notify_information( "Please, list all the models present in the video (names separated with '{}' ).\ (Example: Sasha Grey;Mia Khalifa;...".format(self.CONFIG["thumbnailArgumentDivider"]) ) return self.SET_MODEL else: # Invalid name detected notifier.notify_error("Invalid name, please try again") return self.SET_VIDEO_NAME
def cancel_thumbnail_wizard(self, update, context): """ This method is called when the conversation abort command is detected. It quits the conversation between the user and the bot and resets the Thumbnail object object. """ notify = Notifier(update, self.BOT) if self.CONFIG["openloadThumbnail"]: url = self.THUMBNAILS[self.get_user_id(update)].URL self.THUMBNAILS[self.get_user_id(update)] = Thumbnail(url) else: path = self.THUMBNAILS[self.get_user_id(update)].IMAGE_LOCAL_PATH self.THUMBNAILS[self.get_user_id(update)] = Thumbnail(path, local=True) notify.notify_success("Exited thumbnail wizard, I've cleared all saved data!" "If you wanna build again a message you can use '/thumbnail' without any problem") print("[Thumbnail Wizard] User {} aborted download wizard".format(self.get_user_id(update))) return ConversationHandler.END
def thumbnail(self, update, context): """ This method handles the '/thumbnail' command. It start a conversation (4 steps) with the user to determine the name of the video, the models of the video, the video's categories and the video URL. """ notifier = Notifier(update, self.BOT) print("[Thumbnail] Check if user with id {} has downloaded any videos..".format(self.get_user_id(update))) thumbnail = self.THUMBNAILS.get(self.get_user_id(update)) if (thumbnail is None) or (self.get_user_id(update) not in self.THUMBNAILS): print("You have first to upload the video to OpenLoad to access this function..") notifier.notify_warning("You have to download and upload a video on OpenLoad to use this function properly.") return ConversationHandler.END else: notifier.notify_success( "We detected that you have already uploaded a video on OpenLoad so you can start build your caption" ) notifier.notify_warning( "This is a wizard that helps you to generate a nice caption for your thumbnail. " "You can type '/cancel' in any moment to abort the process!" ) if self.CONFIG["onlineThumbnail"]: print("[Thumbnail] User {} has a valid thumbnail URL saved: {}".format( self.get_user_id(update), thumbnail.URL) ) else: print("[Thumbnail] User {} has a valid thumbnail data saved in '{}'".format( self.get_user_id(update), len(thumbnail.IMAGE_LOCAL_PATH)) ) notifier.notify_information("Select a video name. PS: It can't be only spaces!") return self.SET_VIDEO_NAME
def thumbnail_set_categories(self, update, context): """ This method represents the 3° step of the thumbnail conversation. It asks the user to insert a list of categories separated by the character selected in the config file (thumbnailArgumentDivider setting). """ notifier = Notifier(update, self.BOT) # Get last message sent by the user msg = update.message.text.strip() # Get categories and set them into the Thumbnail object categories = msg.split(self.CONFIG["thumbnailArgumentDivider"]) self.THUMBNAILS[self.get_user_id(update)].set_categories(categories) notifier.notify_success('I detected {} categories!'.format(len(categories))) print("[Thumbnail] User {} selected {} categories: {}".format( self.get_user_id(update), len(categories), categories )) # Proceed to the next step notifier.notify_information( 'Now tell me the url of the video!'.format( self.CONFIG["thumbnailArgumentDivider"] ) ) return self.SET_URL
def check_download_url(self, update, context): """ This method represents the 1° step of the download conversation. It asks the user to insert the video URL. It will check if the URL is formatted well (validator-like) and if the site is reachable (HTTP GET Request with Reponse Code 200) """ # Create unique notifier for this user (every update -> different notifier) notifier = Notifier(update, self.BOT) # Get last message sent by the user url = update.message.text # Check url if UrlChecker.full_check(url): # Save url self.DOWNLOAD_REQUEST.url = url if self.CONFIG["noDownloadWizard"]: print("[NoWizard] Skip to downloading") # Download file self._download_file(self.DOWNLOAD_REQUEST, update) # End conversation return ConversationHandler.END else: notifier.notify_success("The url is well-formatted and the website is reachable.") # Check if automaticFilename is enabled or not in the config if not self.CONFIG["automaticFilename"]: # Ask for the filename notifier.notify_custom("2️⃣", "Send me a filename") return self.SET_FILE_NAME else: # Go to the download confirmation update.message.reply_text(self.DOWNLOAD_CONFIRMATION) return self.START_DOWNLOAD else: # Ask again notifier.notify_error("The url is not valid or the website is not reachable.") return self.SET_DOWNLOAD_URL
def _download_file(self, request: DownloadRequest, session): """ This method uses the DownloadManager class to download the user request. :param request: DownloadRequest object that describes the user download request (url and filename" :param session: Current user session. (Telegram.ext.Update object) """ from classes.downloadmanager import DownloadManager manager = DownloadManager( request, notifier=Notifier(session, self.BOT, self.CONFIG["videoTimeout"]), uploader=self.UPLOADER, online_thumbnail=self.CONFIG["onlineThumbnail"], ) manager.download_file( self.CONFIG["saveFolder"], overwrite_check=self.CONFIG["overwriteCheck"], automatic_filename=self.CONFIG["automaticFilename"], new_download_method=self.CONFIG["newDownloadMethod"], convert_to_mp4=self.CONFIG["videoToMP4"] )
def stop(self, update, context): """ This method handles the '/stop' command. It stops the download process! """ print("[Bot] Received cancel command from", self.get_user_id(update)) notifier = Notifier(update, self.BOT) if self.get_user_id(update) in self.DOWNLOAD_PROCESSES: print("[Download cancel] Found download thread") # Stop download process self.DOWNLOAD_PROCESSES[self.get_user_id(update)].terminate() print("[Download cancel] Download thread killed") # Remove index from dictionary del self.DOWNLOAD_PROCESSES[self.get_user_id(update)] # Wait some seconds to let the process kill properly... sleep_time = 2 import time print("[Download cancel] Removing all the data in {} folder in {} seconds..".format( self.CONFIG["saveFolder"], sleep_time) ) time.sleep(sleep_time) # Delete all video parts (so only files) in download folder for the_file in os.listdir(self.CONFIG["saveFolder"]): file_path = os.path.join(self.CONFIG["saveFolder"], the_file) try: if os.path.isfile(file_path): print("[Download cancel] Found {}, i'm deleting it..".format(file_path)) os.unlink(file_path) except Exception as e: print(e) notifier.notify_success("I stopped the download process successfully!") else: notifier.notify_warning( "You are not downloading any content now. You can use this command only to stop a download." )
def _build_thumbnail_message(notifier: Notifier, thumbnail: Thumbnail, online=False): """ This method is used to generate the caption for the thumbnail using all the data saved in the passed Thumbnail object. It will also send the full message (thumbnail + caption) to the user. :param notifier Notifier object used to send messages to the user in a fancy way. :param thumbnail Thumbnail object with all the required data (thumbnail url, models, categories, ...) """ from classes.downloadmanager import DownloadManager print("[Thumbnail] Generating message with these values:") print(thumbnail.to_dict()) if online: notifier.send_photo_bytes( DownloadManager.download_image_stream(thumbnail.URL), caption=notifier.generate_caption(thumbnail) ) else: notifier.send_photo_bytes( open(thumbnail.IMAGE_LOCAL_PATH, 'rb'), caption=notifier.generate_caption(thumbnail) )
def thumbnail_set_url(self, update, context): """ This method represents the 4° and last step of the thumbnail conversation. It asks the user to insert a valid (well-formatted) and reachable via HTTP GET request URL. The URL will be validated by the UrlChecker class. """ notifier = Notifier(update, self.BOT) checker = UrlChecker() # Get last message sent by the user url = update.message.text.strip() if checker.check_format(url): # Url valid (well-formatted & reachable) notifier.notify_success("The URL is well-formatted.") if checker.check_exists(url): notifier.notify_success("The link is also reachable via HTTP GET requests") else: notifier.notify_warning("The link is not reachable or its response is not valid") self.THUMBNAILS[self.get_user_id(update)].set_video_url(url) print("[Thumbnail] User {} selected a url for the caption: {}".format( self.get_user_id(update), url )) # Send image with caption notifier.notify_information("Generating image with caption...") self._build_thumbnail_message( notifier, self.THUMBNAILS[self.get_user_id(update)], online=self.CONFIG["onlineThumbnail"] ) notifier.notify_success( "Message generated successfully. " "If you don't like it you can type '/thumbnail' again." ) # End conversation return ConversationHandler.END else: # Error detected by checker, retry again with another URL notifier.notify_error( "The url is not well-formatted or is not reachable via HTTP GET requests... Check the URL and try again" ) # Restart to 'set URL' step return self.SET_URL