def extract(self, **kwargs): if not self.streams_sorted: # No stream is available return if 'stream_id' in kwargs and kwargs['stream_id']: # Extract the stream stream_id = kwargs['stream_id'] if stream_id not in self.streams \ and stream_id not in self.dash_streams: log.e('[Error] Invalid video format.') log.e('Run \'-i\' command with no specific video format to ' 'view all available formats.') exit(2) else: # Extract stream with the best quality stream_id = self.streams_sorted[0]['itag'] if stream_id in self.streams: src = self.streams[stream_id]['url'] if self.streams[stream_id]['sig'] is not None: sig = self.streams[stream_id]['sig'] src += '&signature={}'.format(sig) elif self.streams[stream_id]['s'] is not None: if not hasattr(self, 'js'): self.js = get_content(self.html5player) s = self.streams[stream_id]['s'] sig = self.__class__.decipher(self.js, s) src += '&signature={}'.format(sig) self.streams[stream_id]['src'] = [src] self.streams[stream_id]['size'] = urls_size( self.streams[stream_id]['src'])
def extract(self, **kwargs): if 'stream_id' in kwargs and kwargs['stream_id']: # Extract the stream stream_id = kwargs['stream_id'] if stream_id not in self.streams: log.e('[Error] Invalid video format.') log.e('Run \'-i\' command with no specific video format to ' 'view all available formats.') exit(2) else: # Extract stream with the best quality stream_id = self.streams_sorted[0]['id']
def xiami_download_showcollect(cid, output_dir='.', info_only=False): html = get_content( 'http://www.xiami.com/song/showcollect/id/{}'.format(cid)) collect_name = match1(html, r'<title>(.*)</title>') xml = get_content( 'http://www.xiami.com/song/playlist/id/{}/type/3'.format(cid)) doc = parseString(xml) output_dir = '{}/[{}]'.format(output_dir, collect_name) tracks = doc.getElementsByTagName('track') track_nr = 1 for i in tracks: artist = album_name = song_title = url = '' try: song_id = i.getElementsByTagName('song_id')[0].firstChild.nodeValue artist = i.getElementsByTagName('artist')[0].firstChild.nodeValue album_name = i.getElementsByTagName( 'album_name')[0].firstChild.nodeValue song_title = i.getElementsByTagName( 'title')[0].firstChild.nodeValue url = location_dec( i.getElementsByTagName('location')[0].firstChild.nodeValue) except Exception: log.e( 'Song {} failed. [Info Missing] artist: {}, album: {}, title:' ' {}, url: {}'.format(song_id, artist, album_name, song_title, url)) continue try: lrc_url = i.getElementsByTagName('lyric')[0].firstChild.nodeValue except Exception: pass type_, ext, size = url_info(url) if not ext: ext = 'mp3' print_info(site_info, song_title, ext, size) if not info_only: file_name = '{:0>2d}.{} - {} - {}'.format(track_nr, song_title, artist, album_name) download_urls([url], file_name, ext, size, output_dir) try: xiami_download_lyric(lrc_url, file_name, output_dir) except Exception: pass track_nr += 1
def extract(self, **kwargs): for i in self.streams: s = self.streams[i] _, s['container'], s['size'] = url_info(s['url']) s['src'] = [s['url']] if 'stream_id' in kwargs and kwargs['stream_id']: # Extract the stream stream_id = kwargs['stream_id'] if stream_id not in self.streams: log.e('[Error] Invalid video format.') log.e('Run \'-i\' command with no specific video format to ' 'view all available formats.') exit(2) else: # Extract stream with the best quality stream_id = self.streams_sorted[0]['id'] _, s['container'], s['size'] = url_info(s['url']) s['src'] = [s['url']]
def script_main(download, download_playlist, **kwargs): logging.basicConfig(format='[%(levelname)s] %(message)s') def print_version(): version = __version__ log.i('version {}, a tiny downloader that scrapes the web.'.format( version)) parser = argparse.ArgumentParser( prog='lulu', usage='lulu [OPTION]... URL...', description='A tiny downloader that scrapes the web', add_help=False, ) parser.add_argument('-V', '--version', action='store_true', help='Print version and exit') parser.add_argument('-h', '--help', action='store_true', help='Print this help message and exit') dry_run_grp = parser.add_argument_group('Dry-run options', '(no actual downloading)') dry_run_grp = dry_run_grp.add_mutually_exclusive_group() dry_run_grp.add_argument('-i', '--info', action='store_true', help='Print extracted information') dry_run_grp.add_argument('-u', '--url', action='store_true', help='Print extracted information with URLs') dry_run_grp.add_argument('--json', action='store_true', help='Print extracted URLs in JSON format') download_grp = parser.add_argument_group('Download options') download_grp.add_argument('-n', '--no-merge', action='store_true', default=False, help='Do not merge video parts') download_grp.add_argument( '--no-caption', action='store_true', help='Do not download captions (subtitles, lyrics, danmaku, ...)') download_grp.add_argument('-f', '--force', action='store_true', default=False, help='Force overwriting existing files') download_grp.add_argument('-F', '--format', metavar='STREAM_ID', help='Set video format to STREAM_ID') download_grp.add_argument('-O', '--output-filename', metavar='FILE', help='Set output filename') download_grp.add_argument('-o', '--output-dir', metavar='DIR', default='.', help='Set output directory') download_grp.add_argument('-p', '--player', metavar='PLAYER', help='Stream extracted URL to a PLAYER') download_grp.add_argument('-c', '--cookies', metavar='COOKIES_FILE', help='Load cookies.txt or cookies.sqlite') download_grp.add_argument('-t', '--timeout', metavar='SECONDS', type=int, default=600, help='Set socket timeout') download_grp.add_argument('-d', '--debug', action='store_true', help='Show traceback and other debug info') download_grp.add_argument('-I', '--input-file', metavar='FILE', type=argparse.FileType('r'), help='Read non-playlist URLs from FILE') download_grp.add_argument('-P', '--password', help='Set video visit password to PASSWORD') download_grp.add_argument('-l', '--playlist', action='store_true', help='Prefer to download a playlist') download_grp.add_argument( '-T', '--thread', type=int, default=0, help=('Use multithreading to download (only works for multiple-parts ' 'video)')) proxy_grp = parser.add_argument_group('Proxy options') proxy_grp = proxy_grp.add_mutually_exclusive_group() proxy_grp.add_argument('-x', '--http-proxy', metavar='HOST:PORT', help='Use an HTTP proxy for downloading') proxy_grp.add_argument('-y', '--extractor-proxy', metavar='HOST:PORT', help='Use an HTTP proxy for extracting only') proxy_grp.add_argument('--no-proxy', action='store_true', help='Never use a proxy') proxy_grp.add_argument('-s', '--socks-proxy', metavar='HOST:PORT', help='Use an SOCKS5 proxy for downloading') download_grp.add_argument('--stream', help=argparse.SUPPRESS) download_grp.add_argument('--itag', help=argparse.SUPPRESS) parser.add_argument('URL', nargs='*', help=argparse.SUPPRESS) args = parser.parse_args() if args.help: print_version() parser.print_help() sys.exit() if args.version: print_version() sys.exit() if args.debug: # Set level of root logger to DEBUG logging.getLogger().setLevel(logging.DEBUG) global force global dry_run global json_output global player global extractor_proxy global output_filename output_filename = args.output_filename extractor_proxy = args.extractor_proxy info_only = args.info if args.url: dry_run = True if args.json: json_output = True # to fix extractors not use VideoExtractor dry_run = True info_only = False if args.cookies: load_cookies(args.cookies) caption = True stream_id = args.format or args.stream or args.itag if args.no_caption: caption = False if args.player: player = args.player caption = False if args.no_proxy: unset_proxy() else: if args.http_proxy: set_proxy(parse_host(args.http_proxy)) if args.socks_proxy: set_socks_proxy(args.socks_proxy) URLs = [] if args.input_file: logging.debug('you are trying to load urls from %s', args.input_file) if args.playlist: log.e("reading playlist from a file is unsupported " "and won't make your life easier") sys.exit(2) URLs.extend(args.input_file.read().splitlines()) args.input_file.close() URLs.extend(args.URL) if not URLs: parser.print_help() sys.exit() socket.setdefaulttimeout(args.timeout) try: extra = {} if extractor_proxy: extra['extractor_proxy'] = extractor_proxy if stream_id: extra['stream_id'] = stream_id download_main(download, download_playlist, URLs, args.playlist, output_dir=args.output_dir, merge=not args.no_merge, info_only=info_only, json_output=json_output, caption=caption, password=args.password, thread=args.thread, **extra) except KeyboardInterrupt: if args.debug: raise else: sys.exit(1) except UnicodeEncodeError: if args.debug: raise log.e('[error] oops, the current environment does not seem to support ' 'Unicode.') log.e('please set it to a UTF-8-aware locale first,') log.e( 'so as to save the video (with some Unicode characters) correctly.' ) log.e('you can do it like this:') log.e(' (Windows) % chcp 65001 ') log.e(' (Linux) $ LC_CTYPE=en_US.UTF-8') sys.exit(1) except Exception: if not args.debug: log.e('[error] oops, something went wrong.') log.e( 'don\'t panic, c\'est la vie. please try the following steps:') log.e(' (1) Rule out any network problem.') log.e(' (2) Make sure lulu is up-to-date.') log.e(' (3) Check if the issue is already known, on') log.e(' https://github.com/iawia002/Lulu/issues') log.e(' (4) Run the command with \'--debug\' option,') log.e(' and report this issue with the full output.') else: print_version() log.i(args) raise sys.exit(1)
#!/usr/bin/env python3 import os import sys _srcdir = '%s/' % os.path.dirname(os.path.realpath(__file__)) _filepath = os.path.dirname(sys.argv[0]) sys.path.insert(1, os.path.join(_filepath, _srcdir)) sys.path.insert(1, os.path.join(_filepath, '../lib/bs4_lib/')) sys.path.insert(1, os.path.join(_filepath, '../lib/requests_lib/')) if sys.version_info[0] == 3: import lulu if __name__ == '__main__': lulu.main() else: # Python 2 from lulu.util import log log.e('[fatal] Python 3 is required!')