Best Python code snippet using playwright-python
downloadcontroller.py
Source:downloadcontroller.py
...21 def init_routes(self):22 @self.app.get('/<user>/<coll>/<rec>/$download')23 def logged_in_download_rec_warc(user, coll, rec):24 self.redir_host()25 return self.handle_download(user, coll, rec)26 @self.app.get('/<user>/<coll>/$download')27 def logged_in_download_coll_warc(user, coll):28 self.redir_host()29 return self.handle_download(user, coll, '*')30 def create_warcinfo(self, creator, title, metadata, source, filename):31 for name, value in iteritems(source):32 if name in self.COPY_FIELDS:33 metadata[name] = value34 info = OrderedDict([35 ('software', 'Webrecorder Platform v' + __version__),36 ('format', 'WARC File Format 1.0'),37 ('creator', creator),38 ('isPartOf', title),39 ('json-metadata', json.dumps(metadata)),40 ])41 wi_writer = BufferWARCWriter()42 wi_writer.write_record(wi_writer.create_warcinfo_record(filename, info))43 return wi_writer.get_contents()44 def create_coll_warcinfo(self, user, collection, filename=''):45 metadata = {}46 metadata['type'] = 'collection'47 title = quote(collection['title'])48 return self.create_warcinfo(user, title, metadata, collection, filename)49 def create_rec_warcinfo(self, user, collection, recording, filename=''):50 metadata = {}51 metadata['pages'] = self.manager.list_pages(user,52 collection['id'],53 recording['id'])54 metadata['type'] = 'recording'55 title = quote(collection['title']) + '/' + quote(recording['title'])56 return self.create_warcinfo(user, title, metadata, recording, filename)57 def handle_download(self, user, coll, rec):58 collection = self.manager.get_collection(user, coll, rec)59 if not collection:60 self._raise_error(404, 'Collection not found',61 id=coll)62 now = timestamp_now()63 name = collection['id']64 if rec != '*':65 rec_list = rec.split(',')66 if len(rec_list) == 1:67 name = rec68 else:69 name += '-' + rec70 else:71 rec_list = None...
DownloadUtils.py
Source:DownloadUtils.py
...8from utils.Bean import FileInfo9from utils.Constans import DOWNLOAD_PATH, MP4_TYPE, JPG_TYPE10from utils.CommonUtils import isNullOrBlank, get_index, isNull11from utils.FileUtils import get_valid_file_name, get_url_suffix, get_unique_file_name, write_file12def handle_download(url, name, suffix, header=None, file_path=DOWNLOAD_PATH):13 try:14 s = requests.Session()15 if header is not None:16 s.headers = header17 s.mount(url, HTTPAdapter(max_retries=5))18 downloaded = s.get(url, timeout=(5, 10))19 valid_name = get_valid_file_name(name)20 file_suffix = get_url_suffix(url, suffix)21 file_name = get_unique_file_name(valid_name + file_suffix)22 write_file(file_path + file_name, downloaded.content)23 print('ä¸è½½æå: ', file_name)24 except Exception as e:25 print('ä¸è½½å¤±è´¥: ', e)26 traceback.print_exc()27def handle_xunlei_download(url, name, suffix, xunlei_o=None):28 try:29 o = Dispatch("ThunderAgent.Agent64.1") if xunlei_o is None else xunlei_o30 valid_name = get_valid_file_name(name)31 file_suffix = get_url_suffix(url, suffix)32 file_name = get_unique_file_name(valid_name + file_suffix)33 o.AddTask(url, file_name)34 if xunlei_o is None:35 o.CommitTasks()36 print('ä¸è½½æå: ', file_name)37 except Exception as e:38 print('ä¸è½½å¤±è´¥: ', e)39 traceback.print_exc()40def handle_download_list(data_list: list[FileInfo], header=None, file_path=DOWNLOAD_PATH, **kwargs):41 if kwargs.get('xunlei', False):42 list_o = kwargs.get('o', Dispatch("ThunderAgent.Agent64.1"))43 for each_data in data_list:44 if isNull(each_data.urls):45 handle_xunlei_download(each_data.url, each_data.name, each_data.suffix, list_o)46 else:47 handle_download_index(each_data, **kwargs)48 list_o.CommitTasks()49 else:50 for each_data in data_list:51 if isNull(each_data.urls):52 handle_download_and_merag(each_data, header, file_path)53 else:54 handle_download_index(each_data, header, file_path)55def handle_download_index(data: FileInfo, header=None, file_path=DOWNLOAD_PATH, **kwargs):56 if kwargs.get('xunlei', False):57 index_o = kwargs.get('o', Dispatch("ThunderAgent.Agent64.1"))58 for index, each_url in enumerate(data.urls):59 handle_xunlei_download(each_url, data.name + get_index(data.urls, index), data.suffix, index_o)60 if kwargs.get('o') is None:61 index_o.CommitTasks()62 else:63 for index, each_url in enumerate(data.urls):64 data.name = data.name + get_index(data.urls, index)65 handle_download_and_merag(data, header, file_path)66def merge_video_audio(title, file_path=DOWNLOAD_PATH):67 audio_name = title + "_audio" + MP4_TYPE68 video_name = title + "_video" + MP4_TYPE69 command = 'ffmpeg.exe -i "{}" -i "{}" -acodec copy -vcodec copy "{}.mp4"'.format(audio_name, video_name, title)70 subprocess.check_output(command, cwd=file_path)71 os.remove(file_path + audio_name)72 os.remove(file_path + video_name)73def merge_video_pic(title, file_path=DOWNLOAD_PATH):74 video_name_no_cover = title + "_no_cover" + MP4_TYPE75 video_name = title + MP4_TYPE76 pic_name = title + "_pic" + JPG_TYPE77 os.rename(file_path + video_name, file_path + video_name_no_cover)78 command = 'ffmpeg -i "{}" -i "{}" -map 1 -map 0 -c copy -disposition:0 attached_pic "{}"'\79 .format(video_name_no_cover, pic_name, video_name)80 subprocess.check_output(command, cwd=file_path)81 os.remove(file_path + pic_name)82 os.remove(file_path + video_name_no_cover)83def handle_download_and_merag(data: FileInfo, header=None, file_path=DOWNLOAD_PATH):84 if not isNullOrBlank(data.url):85 handle_download(data.url, data.name, data.suffix, header, file_path)86 else:87 if not isNullOrBlank(data.videoInfo.audio_url):88 handle_download(data.videoInfo.video_url, data.name + "_video", MP4_TYPE, header, file_path)89 handle_download(data.videoInfo.audio_url, data.name + "_audio", MP4_TYPE, header, file_path)90 merge_video_audio(data.name, file_path)91 else:92 handle_download(data.videoInfo.video_url, data.name, MP4_TYPE, file_path)93 if not isNullOrBlank(data.videoInfo.cover_url):94 handle_download(data.videoInfo.cover_url, data.name + "_pic", JPG_TYPE, header, file_path)...
MegasharesCom.py
Source:MegasharesCom.py
...31 def setup(self):32 self.resume_download = True33 self.multiDL = self.premium34 def handle_premium(self, pyfile):35 self.handle_download(True)36 def handle_free(self, pyfile):37 if self.NO_SLOTS_PATTERN in self.data:38 self.retry(wait=5 * 60)39 m = re.search(self.REACTIVATE_PASSPORT_PATTERN, self.data)40 if m is not None:41 passport_num = m.group(1)42 request_uri = re.search(43 self.REQUEST_URI_PATTERN,44 self.data).group(1)45 random_num = re.search(46 self.REACTIVATE_NUM_PATTERN,47 self.data).group(1)48 verifyinput = self.captcha.decrypt("http://d01.megashares.com/index.php",49 get={'secgfx': "gfx", 'random_num': random_num})50 self.log_info(51 _("Reactivating passport %s: %s %s") %52 (passport_num, random_num, verifyinput))53 res = self.load("http://d01.megashares.com%s" % request_uri,54 get={'rs': "check_passport_renewal",55 'rsargs[]': verifyinput,56 'rsargs[]': random_num,57 'rsargs[]': passport_num,58 'rsargs[]': "replace_sec_pprenewal",59 'rsrnd[]': str(int(time.time() * 1000))})60 if 'Thank you for reactivating your passport' in res:61 self.captcha.correct()62 self.restart()63 else:64 self.retry_captcha(msg=_("Failed to reactivate passport"))65 m = re.search(self.PASSPORT_RENEW_PATTERN, self.data)66 if m is not None:67 time = [int(x) for x in m.groups()]68 renew = time[0] + (time[1] * 60) + (time[2] * 60)69 self.log_debug("Waiting %d seconds for a new passport" % renew)70 self.retry(wait=renew, msg=_("Passport renewal"))71 #: Check traffic left on passport72 m = re.search(self.PASSPORT_LEFT_PATTERN, self.data, re.M | re.S)73 if m is None:74 self.fail(_("Passport not found"))75 self.log_info(_("Download passport: %s") % m.group(1))76 data_left = float(m.group(2)) * \77 1024 ** {'B': 0, 'KB': 1, 'MB': 2, 'GB': 3}[m.group(3)]78 self.log_info(_("Data left: %s %s (%d MB needed)") %79 (m.group(2), m.group(3), self.pyfile.size / 1048576))80 if not data_left:81 self.retry(wait=600, msg=_("Passport renewal"))82 self.handle_download(False)83 def handle_download(self, premium=False):84 m = re.search(self.LINK_PATTERN % (1 if premium else 2), self.data)85 msg = _('%s download URL' % ('Premium' if premium else 'Free'))86 if m is None:87 self.error(msg)88 self.link = m.group(1)...
TenluaVn.py
Source:TenluaVn.py
...49 "password": bool(file_info["passwd"]),50 },51 }52 def handle_free(self, pyfile):53 self.handle_download()54 def handle_premium(self, pyfile):55 sid = self.account.info["data"]["sid"]56 self.handle_download(sid)57 def handle_download(self, sid=None):58 if self.info["tenlua"]["password"]:59 password = self.get_password()60 if password:61 file_id = self.info["pattern"]["ID"]62 args = dict(n=file_id, p=password, r=gen_r())63 if sid is not None:64 args["sid"] = sid65 password_status = self.api_request(66 "filemanager_builddownload_checkpassword", **args67 )68 if password_status["status"] == "0":69 self.fail(self._("Wrong password"))70 else:71 url = password_status["url"]...
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!