def main(): arg_parser() if not args.debug: logging.root.setLevel(logging.WARNING) else: logging.root.setLevel(logging.DEBUG) if args.timeout: socket.setdefaulttimeout(args.timeout) if args.proxy == 'system': proxy_handler = ProxyHandler() args.proxy = os.environ.get('HTTP_PROXY', 'none') else: proxy_handler = ProxyHandler({'http': args.proxy, 'https': args.proxy}) if not args.proxy == 'none': opener = build_opener(proxy_handler) install_opener(opener) #mkdir and cd to output dir if not args.output_dir == '.': try: if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) except: logger.warning("No permission or Not found " + args.output_dir) logger.warning("use current folder") args.output_dir = '.' if os.path.exists(args.output_dir): os.chdir(args.output_dir) try: exit = 0 for url in args.video_urls: try: m, u = url_to_module(url) if args.playlist: parser = m.parser_list else: parser = m.parser info = parser(u) if type(info) is types.GeneratorType or type(info) is list: ind = 0 for i in info: if ind < args.start: ind += 1 continue handle_videoinfo(i, index=ind) ind += 1 else: handle_videoinfo(info) except AssertionError as e: logger.critical(compact_str(e)) exit = 1 except (RuntimeError, NotImplementedError, SyntaxError) as e: logger.error(compact_str(e)) exit = 1 sys.exit(exit) except KeyboardInterrupt: logger.info('Interrupted by Ctrl-C')
def main(): arg_parser() if args.timeout: socket.setdefaulttimeout(args.timeout) if args.proxy == 'system': proxy_handler = ProxyHandler() else: proxy_handler = ProxyHandler({ 'http': args.proxy, 'https': args.proxy }) opener = build_opener(proxy_handler) install_opener(opener) #mkdir and cd to output dir if not args.output_dir == '.': try: if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) except: log.w("No permission or Not found " + args.output_dir) log.w("use current folder") args.output_dir = '.' if os.path.exists(args.output_dir): os.chdir(args.output_dir) try: exit = 0 for url in args.video_urls: try: m,u = url_to_module(url) if args.playlist: parser = m.parser_list else: parser = m.parser info = parser(u) if type(info) is list: if args.start >= len(info): log.w('invalid argument -s/--start') log.w('start from beginning') args.start = 0 for i in info[args.start:]: handle_videoinfo(i) else: handle_videoinfo(info) except AssertionError as e: log.wtf(compact_str(e)) exit = 1 except (RuntimeError, NotImplementedError, SyntaxError) as e: log.e(compact_str(e)) exit = 1 sys.exit(exit) except KeyboardInterrupt: print('\nInterrupted by Ctrl-C')
def main(): args = arg_parser() logging.root.setLevel(logging.WARNING) # if not args.debug: # logging.root.setLevel(logging.WARNING) # else: # logging.root.setLevel(logging.DEBUG) if args.timeout: socket.setdefaulttimeout(args.timeout) if args.proxy == 'system': proxy_handler = ProxyHandler() args.proxy = os.environ.get('HTTP_PROXY', 'none') else: proxy_handler = ProxyHandler({'http': args.proxy, 'https': args.proxy}) if not args.proxy == 'none': opener = build_opener(proxy_handler) install_opener(opener) default_proxy_handler[:] = [proxy_handler] import ssl ssl._create_default_https_context = ssl._create_unverified_context try: url = args.video_url try: m, u = url_to_module(url) parser = m.parser info = parser(u) print( json.dumps(info.jsonlize(), indent=4, sort_keys=True, ensure_ascii=False)) except AssertionError as e: logger.critical(e) except (RuntimeError, NotImplementedError, SyntaxError) as e: logger.error(e) except KeyboardInterrupt: logger.info('Interrupted by Ctrl-C')
def main(): args = arg_parser() if args.timeout: socket.setdefaulttimeout(args.timeout) if args.proxy: proxy_handler = ProxyHandler({'http': args.proxy, 'https': args.proxy}) opener = build_opener(proxy_handler) install_opener(opener) #mkdir and cd to output dir if not args.output_dir == '.': try: if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) except: log.w("No permission or Not found " + args.output_dir) log.w("use current folder") args.output_dir = '.' if os.path.exists(args.output_dir): os.chdir(args.output_dir) try: exit = 0 for url in args.video_urls: try: m, u = url_to_module(url) if not u == url: args.video_urls[args.video_urls.index(url)] = u if args.playlist: m.download_playlist(u, args) else: m.download(u, args) except AssertionError as e: log.wtf(str(e)) exit = 1 except (RuntimeError, NotImplementedError, SyntaxError) as e: log.e(str(e)) exit = 1 sys.exit(exit) except KeyboardInterrupt: print('\nInterrupted by Ctrl-C')
def main(): args = arg_parser() handlers = [] if args.timeout: socket.setdefaulttimeout(args.timeout) if args.user_agent: fake_headers['User-Agent'] = args.user_agent if args.http_proxy: proxy_handler = ProxyHandler({ 'http': args.http_proxy, 'https': args.http_proxy }) handlers.append(proxy_handler) elif args.socks_proxy: try: import socks addr, port = args.socks_proxy.split(':') socks.set_default_proxy(socks.SOCKS5, addr, int(port)) socket.socket = socks.socksocket except: print('Failed to set socks5 proxy. Please install PySocks.', file=sys.stderr) opener = build_opener(*handlers) install_opener(opener) default_proxy_handler[:] = handlers m, u = url_to_module(args.video_url) info = m.parser(u) # Is a playlist? if m.list_only(): video_list = m.prepare_list() result = [ {'title': match1(get_content(url), r'<title>(.+?)</title>'), 'url': url} for url in video_list ] else: result = info.jsonlize() print(json.dumps(result, indent=4, sort_keys=True, ensure_ascii=False))
def main(): arg_parser() if not args.debug: logging.root.setLevel(logging.WARNING) else: logging.root.setLevel(logging.DEBUG) if args.timeout: socket.setdefaulttimeout(args.timeout) if args.insecure: ssl._create_default_https_context = ssl._create_unverified_context proxies = None if args.proxy == 'system': proxies = getproxies() args.proxy = proxies.get('http') or proxies.get('https', 'none') args.proxy = args.proxy.lower() if not args.proxy.startswith(('http', 'socks', 'none')): args.proxy = 'http://' + args.proxy if args.proxy == 'none': proxies = {} elif args.proxy.startswith(('http', 'socks')): if args.proxy.startswith(('https', 'socks')): try: import extproxy except ImportError: logger.error('Please install ExtProxy to use proxy: ' + args.proxy) raise proxies = { 'http': args.proxy, 'https': args.proxy } proxy_handler = ProxyHandler(proxies) add_default_handler(proxy_handler) install_default_handlers() #mkdir and cd to output dir if not args.output_dir == '.': try: if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) except: logger.warning("No permission or Not found " + args.output_dir) logger.warning("use current folder") args.output_dir = '.' if os.path.exists(args.output_dir): os.chdir(args.output_dir) try: exit = 0 for url in args.video_urls: try: m, u = url_to_module(url) if args.playlist: parser = m.parser_list else: parser = m.parser info = parser(u) if type(info) is types.GeneratorType or type(info) is list: ind = 0 for i in info: if ind < args.start: ind += 1 continue handle_videoinfo(i, index=ind) ind += 1 else: handle_videoinfo(info) except AssertionError as e: logger.critical(compact_str(e)) exit = 1 except (RuntimeError, NotImplementedError, SyntaxError) as e: logger.error(compact_str(e)) exit = 1 sys.exit(exit) except KeyboardInterrupt: logger.info('Interrupted by Ctrl-C')
def main(): arg_parser() if not args.debug: logging.root.setLevel(logging.WARNING) else: logging.root.setLevel(logging.DEBUG) if args.timeout: socket.setdefaulttimeout(args.timeout) if args.proxy == 'system': proxy_handler = ProxyHandler() args.proxy = os.environ.get('HTTP_PROXY', 'none') elif args.proxy.upper().startswith('SOCKS'): try: import socks from sockshandler import SocksiPyHandler except ImportError: logger.error('To use SOCKS proxy, please install PySocks first!') raise parsed_socks_proxy = urlparse(args.proxy) sockstype = socks.PROXY_TYPES[parsed_socks_proxy.scheme.upper()] rdns = True proxy_handler = SocksiPyHandler(sockstype, parsed_socks_proxy.hostname, parsed_socks_proxy.port, rdns, parsed_socks_proxy.username, parsed_socks_proxy.password) else: proxy_handler = ProxyHandler({'http': args.proxy, 'https': args.proxy}) if not args.proxy == 'none': opener = build_opener(proxy_handler) install_opener(opener) default_proxy_handler[:] = [proxy_handler] #mkdir and cd to output dir if not args.output_dir == '.': try: if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) except: logger.warning("No permission or Not found " + args.output_dir) logger.warning("use current folder") args.output_dir = '.' if os.path.exists(args.output_dir): os.chdir(args.output_dir) try: exit = 0 for url in args.video_urls: try: m, u = url_to_module(url) if args.playlist: parser = m.parser_list else: parser = m.parser info = parser(u) if type(info) is types.GeneratorType or type(info) is list: ind = 0 for i in info: if ind < args.start: ind += 1 continue handle_videoinfo(i, index=ind) ind += 1 else: handle_videoinfo(info) except AssertionError as e: logger.critical(compact_str(e)) exit = 1 except (RuntimeError, NotImplementedError, SyntaxError) as e: logger.error(compact_str(e)) exit = 1 sys.exit(exit) except KeyboardInterrupt: logger.info('Interrupted by Ctrl-C')