class WebTracking(object): def __init__(self): self.cookies = Cookies() def inspect_response(self, response): if not log.ThugOpts.web_tracking: return self.cookies.inspect(response)
def __init__(self, user): self.bot = Bot().getBot() self.url = "https://www.tiktok.com/upload?lang=en" self.cookies = Cookies() self.userRequest = {"dir": "", "cap": "", "vidTxt": ""} self.video = None self.IO = IO("hashtags.txt") self.videoFormats = ["mov", "flv", "avi"] self.userPreference = user
def directUpload(self, filename, private=False, test=False): if self.bot is None: self.bot = Browser().getBot() self.webbot = Bot(self.bot) self.bot.get(self.url) utils.randomTimeQuery() self.cookies = Cookies(self.bot) self.bot.refresh() try: file_input_element = self.webbot.getVideoUploadInput() except Exception as e: print(f"Error: {e}") print( "Major error, cannot find the file upload button, please update getVideoUploadInput() in Bot.py" ) file_input_element = None exit() abs_path = os.path.join(os.getcwd(), filename) try: file_input_element.send_keys(abs_path) except StaleElementReferenceException as e: try: self.bot.implicitly_wait(5) file_input_element = self.webbot.getVideoUploadInput() file_input_element.send_keys(abs_path) except Exception as e: print( "Major error, cannot find the file upload button, please update getVideoUploadInput() in Bot.py" ) exit() # We need to wait until it is uploaded and then clear input. self.addCaptions() utils.randomTimeQuery() if private: self.webbot.selectPrivateRadio() # private video selection utils.randomTimeQuery() else: """ self.webbot.selectPublicRadio() # public video selection utils.randomTimeQuery() """ pass if not test: self.webbot.uploadButtonClick() # upload button input("Press any button to exit")
def uploadVideo(self, video_dir, videoText, startTime=0, endTime=0, private=True, test=False, scheduled=False, schdate="", schtime=""): video_dir = self.downloadIfYoutubeURL(video_dir) if not video_dir: return if self.bot is None: self.bot = Browser().getBot() self.webbot = Bot(self.bot) self.userRequest["dir"] = video_dir self.checkFileExtensionValid() self.userRequest["cap"] = self.IO.getHashTagsFromFile() # Initiate bot if isn't already. self.bot.get(self.url) self.userRequest["vidTxt"] = videoText # Cookies loaded here. self.cookies = Cookies(self.bot) self.bot.refresh() # User now has logged on and can upload videos time.sleep(3) self.inputVideo(startTime, endTime) self.addCaptions() utils.randomTimeQuery() if private: self.webbot.selectPrivateRadio() # private video selection else: self.webbot.selectPublicRadio() # public video selection utils.randomTimeQuery() if not test: self.webbot.uploadButtonClick() # upload button input("Press any button to exit")
class Upload: def __init__(self, user): self.bot = Bot().getBot() self.url = "https://www.tiktok.com/upload?lang=en" self.cookies = Cookies() self.userRequest = {"dir": "", "cap": "", "vidTxt": ""} self.video = None self.IO = IO("hashtags.txt") self.videoFormats = ["mov", "flv", "avi"] self.userPreference = user def uploadVideo(self, video_dir, videoText, startTime=0, endTime=0, private=True, test=True): video_dir = self.downloadIfYoutubeURL(video_dir) self.userRequest["dir"] = os.path.join(video_dir) self.checkFileExtensionValid() self.userRequest["cap"] = self.IO.getHashTagsFromFile() self.bot.get(self.url) self.userRequest["vidTxt"] = videoText if self.cookies.loadCookies(self.bot): self.bot.refresh() else: # User needs to sign in first... input( "Please sign in and do captcha to save captcha.\nPress any button to continue..." ) self.cookies.writeCookie(self.bot.get_cookies()) # User now has logged on and can upload videos time.sleep(3) self.inputVideo(startTime, endTime) self.addCaptions() utils.randomTimeQuery() if private: self.bot.execute_script( 'document.getElementsByClassName("radio-group")[0].children[2].click()' ) # private video selection else: self.bot.execute_script( 'document.getElementsByClassName("radio-group")[0].children[0].click()' ) # public video selection utils.randomTimeQuery() if not test: self.bot.execute_script( 'document.getElementsByClassName("btn-post")[0].click()' ) # upload button input("Exit") def addCaptions(self): # Add div elements to dom. self.bot.implicitly_wait(3) self.bot.execute_script( f'var element = document.getElementsByClassName("public-DraftStyleDefault-block")[0].children[0].getAttribute("data-offset-key");' ) caption_elem = self.bot.find_elements_by_class_name( "public-DraftStyleDefault-block")[0] for hashtag in self.IO.getHashTagsFromFile(): caption_elem.send_keys(hashtag) def inputVideo(self, startTime=0, endTime=0): WebDriverWait(self.bot, 10).until( EC.presence_of_element_located( (By.CLASS_NAME, "upload-btn-input"))) file_input_element = self.bot.find_elements_by_class_name( "upload-btn-input")[0] # Check if file has correct .mp4 extension, else throw error. self.video = Video(self.userRequest["dir"], self.userRequest["vidTxt"]) self.video.createVideo() if not startTime == 0 and endTime == 0: self.video.customCrop(startTime, endTime) while not os.path.exists( self.userRequest["dir"]): # Wait for path to exist time.sleep(1) abs_path = os.path.join(os.getcwd(), self.userRequest["dir"]) file_input_element.send_keys(abs_path) def checkFileExtensionValid(self): if self.userRequest["dir"].endswith('.mp4'): pass else: self.bot.close() exit(f"File: {self.userRequest['dir']} has wrong file extension.") def downloadIfYoutubeURL(self, video_dir): if "www.youtube.com/" in video_dir: print("Detected Youtube Video...") video = YouTube(video_dir) [ print(i) for i in video.streams.filter(file_extension="mp4").all() ] index = input("Enter iTag value of video you want to use:: ") while type(index) != int: try: index = int(index) except Exception as e: index = input( "Please enter an integer, iTag value of video you want to use:: " ) random_filename = str(int(time.time())) video_path = os.path.join(self.userPreference.video_save_dir, random_filename) + ".mp4" video.streams.get_by_itag(int(index)).download( output_path=self.userPreference.video_save_dir, filename=random_filename) return video_path return video_dir @staticmethod def checkTiktokStatus(): pass
class Upload: def __init__(self, user): self.bot = Bot().getBot() self.url = "https://www.tiktok.com/upload?lang=en" self.cookies = Cookies() self.userRequest = {"dir": "", "cap": "", "vidTxt": ""} self.video = None self.IO = IO("hashtags.txt") self.videoFormats = ["mov", "flv", "avi"] self.userPreference = user # Class used to upload video. def uploadVideo(self, video_dir, videoText, startTime=0, endTime=0, private=True, test=False): video_dir = self.downloadIfYoutubeURL(video_dir) if not video_dir: return self.userRequest["dir"] = video_dir self.checkFileExtensionValid() self.userRequest["cap"] = self.IO.getHashTagsFromFile() self.bot.get(self.url) self.userRequest["vidTxt"] = videoText if self.cookies.loadCookies(self.bot): self.bot.refresh() else: # User needs to sign in first... input( "Please sign in and do captcha to save captcha.\nPress any button to continue..." ) self.cookies.writeCookie(self.bot.get_cookies()) # User now has logged on and can upload videos time.sleep(3) self.inputVideo(startTime, endTime) self.addCaptions() utils.randomTimeQuery() if private: self.bot.execute_script( 'document.getElementsByClassName("radio-group")[0].children[2].click()' ) # private video selection else: self.bot.execute_script( 'document.getElementsByClassName("radio-group")[0].children[0].click()' ) # public video selection utils.randomTimeQuery() if not test: self.bot.execute_script( 'document.getElementsByClassName("btn-post")[0].click()' ) # upload button input("Exit") # Method to check file is valid. def checkFileExtensionValid(self): if self.userRequest["dir"].endswith('.mp4'): pass else: self.bot.close() exit(f"File: {self.userRequest['dir']} has wrong file extension.") # This gets the hashtags from file and adds them to the website input def addCaptions(self): # Add div elements to dom. self.bot.implicitly_wait(3) self.bot.execute_script( f'var element = document.getElementsByClassName("public-DraftStyleDefault-block")[0].children[0].getAttribute("data-offset-key");' ) caption_elem = self.bot.find_elements_by_class_name( "public-DraftStyleDefault-block")[0] for hashtag in self.IO.getHashTagsFromFile(): caption_elem.send_keys(hashtag) # This is in charge of adding the video into tiktok input element. def inputVideo(self, startTime=0, endTime=0): WebDriverWait(self.bot, 10).until( EC.presence_of_element_located( (By.CLASS_NAME, "upload-btn-input"))) file_input_element = self.bot.find_elements_by_class_name( "upload-btn-input")[0] # Check if file has correct .mp4 extension, else throw error. self.video = Video(self.userRequest["dir"], self.userRequest["vidTxt"], self.userPreference) self.video.createVideo() # Link to video class method print(f"startTime: {startTime}, endTime: {endTime}") if startTime != 0 and endTime != 0: print(f"Cropping Video timestamps: {startTime}, {endTime}") self.video.customCrop(startTime, endTime) while not os.path.exists(self.video.dir): # Wait for path to exist time.sleep(1) abs_path = os.path.join(os.getcwd(), self.video.dir) file_input_element.send_keys(abs_path) # This is in charge of determining if is youtube url and downloading video if available. def downloadIfYoutubeURL(self, video_dir): # https://stackoverflow.com/questions/6556559/youtube-api-extract-video-id/6556662#6556662 url_variants = [ "http://youtu.be/", "https://youtu.be/", "http://youtube.com/", "https://youtube.com/", "https://m.youtube.com/", "http://www.youtube.com/", "https://www.youtube.com/" ] # if "www.youtube.com/" in video_dir: if any(ext in video_dir for ext in url_variants): print("Detected Youtube Video...") video = YouTube(video_dir).streams.filter(file_extension="mp4", adaptive=True).first() audio = YouTube(video_dir).streams.filter(file_extension="webm", only_audio=True, adaptive=True).first() if video and audio: random_filename = str(int( time.time())) # extension is added automatically. video_path = os.path.join(self.userPreference.video_save_dir, "pre-processed") + ".mp4" resolution = int(video.resolution[:-1]) if resolution >= 360: video.download(output_path="VideosDirPath", filename=random_filename) print("Downloaded Video File") audio.download(output_path="VideosDirPath", filename="a" + random_filename) print("Downloaded Audio File") file_check_iter = 0 while not os.path.exists( "VideosDirPath\\" + random_filename + ".mp4") and os.path.exists("VideosDirPath\\" + "a" + random_filename + ".webm"): time.sleep(1) file_check_iter = +1 if file_check_iter > 10: print( "Error saving these files to directory, please try again" ) return video = VideoFileClip( os.path.join(self.userPreference.video_save_dir, random_filename + ".mp4")) audio = AudioFileClip( os.path.join(self.userPreference.video_save_dir, "a" + random_filename + ".webm")) composite_video = video.set_audio(audio) # composite_video = composite_video.subclip(t_start=0, t_end=3) composite_video.write_videofile(video_path) return video_path else: print("All videos have are too low of quality.") return print("No videos available with both audio and video available...") return False return video_dir
def __init__(self): self.cookies = Cookies()