Ejemplo n.º 1
0
    def prepare(self):
        add_default_handler(HTTPCookieProcessor)
        install_default_handlers()
        add_header('Referer', self.url)
        add_header('User-Agent', ua)

        info = VideoInfo(self.name)
        if self.url and not self.vid:
            self.vid = match1(self.url,
                              'https?://(?:www.)?mgtv.com/b/\d+/(\d+).html')
            if self.vid is None:
                html = get_content(self.url)
                self.vid = match1(html, 'vid=(\d+)', 'vid=\"(\d+)',
                                  'vid: (\d+)')

        did = str(uuid.uuid4())
        tk2 = generate_tk2(did)

        api_info_url = 'https://pcweb.api.mgtv.com/player/video?tk2={}&video_id={}&type=pch5'.format(
            tk2, self.vid)
        meta = json.loads(get_content(api_info_url))
        self.logger.debug('meta >\n%s', meta)

        assert meta['code'] == 200, '[failed] code: {}, msg: {}'.format(
            meta['code'], meta['msg'])
        assert meta['data'], '[Failed] Video info not found.'

        pm2 = meta['data']['atc']['pm2']
        info.title = meta['data']['info']['title'] + ' ' + meta['data'][
            'info']['desc']

        api_source_url = 'https://pcweb.api.mgtv.com/player/getSource?pm2={}&tk2={}&video_id={}&type=pch5'.format(
            pm2, tk2, self.vid)
        meta = json.loads(get_content(api_source_url))

        assert meta['code'] == 200, '[failed] code: {}, msg: {}'.format(
            meta['code'], meta['msg'])
        assert meta['data'], '[Failed] Video source not found.'

        data = meta['data']
        domain = data['stream_domain'][0]
        for lstream in data['stream']:
            lurl = lstream['url']
            if lurl:
                lurl = '{}{}&did={}'.format(domain, lurl, did)
                url = json.loads(get_content(lurl))['info']
                video_profile = lstream['name']
                stream = self.profile_2_types[video_profile]
                info.streams[stream] = {
                    'container': 'm3u8',
                    'video_profile': video_profile,
                    'src': [url]
                }
                info.stream_types.append(stream)
        info.stream_types = sorted(info.stream_types,
                                   key=self.supported_stream_types.index)
        info.extra['referer'] = self.url
        info.extra['ua'] = ua
        return info
Ejemplo n.º 2
0
    def prepare(self):
        add_default_handler(HTTPCookieProcessor)
        install_default_handlers()

        info = VideoInfo(self.name, True)
        html = get_content(self.url)
        self.vid = match1(html, '"userNum":(\d+)')
        live_id = match1(html, '"liveId":\s*(\d+)')
        assert live_id, u"主播正在休息"
        info.stream_types.append('current')
        info.streams['current'] = {
            'container':
            'mp4',
            'src': [
                'http://extapi.live.netease.com/redirect/video/{}'.format(
                    self.vid)
            ],
            'size':
            float('inf')
        }
        info.artist = match1(html, '"nick":"([^"]+)')
        info.title = match1(html, '<title>([^<]+)').split('-')[0]
        return info
Ejemplo n.º 3
0
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')
Ejemplo n.º 4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from ykdl.util.html import (get_content, get_location, add_header,
                            add_default_handler, install_default_handlers)
from ykdl.util.match import match1, matchall
from ykdl.compact import HTTPCookieProcessor

from .idconvertor import av2bv

import json

API_view = 'https://api.bilibili.com/x/web-interface/view?bvid='

add_default_handler(HTTPCookieProcessor)
install_default_handlers()
add_header('Referer', 'https://www.bilibili.com/')


def get_extractor(url):
    if 'live.bilibili' in url:
        from . import live as s
        return s.site, url
    elif 'vc.bilibili' in url:
        from . import vc as s
        return s.site, url
    elif '/bangumi/' in url:
        from . import bangumi as s
        return s.site, url

    page_index = match1(url, '(?:page|\?p)=(\d+)', 'index_(\d+)\.') or '1'
Ejemplo n.º 5
0
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
        args.certs = None
    else:
        certs = args.append_certs or []
        try:
            import certifi
        except ImportError:
            pass
        else:
            certs.append(certifi.where())
        if certs:
            context = ssl._create_default_https_context()
            for cert in certs:
                if os.path.exists(cert):
                    context.load_verify_locations(cert)
            https_handler = HTTPSHandler(context=context)
            add_default_handler(https_handler)
            args.certs = certs

    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)

    exit = 0
    try:
        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(str(e))
                exit = 1
            except (RuntimeError, NotImplementedError, SyntaxError) as e:
                logger.error(str(e))
                exit = 1
    except KeyboardInterrupt:
        logger.info('Interrupted by Ctrl-C')
    except Exception as e:
        errmsg = str(e)
        logger.debug(errmsg, exc_info=True)
        if 'local issuer' in errmsg:
            logger.warning('Please install or update Certifi, and try again:\n'
                           'pip3 install certifi --upgrade')
        exit = 1
    sys.exit(exit)
Ejemplo n.º 6
0
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

    if args.proxy == 'system':
        args.proxy = getproxies().get('http', 'none')

    if args.proxy.lower().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 = None
        proxy_handler = SocksiPyHandler(sockstype,
                                        parsed_socks_proxy.hostname,
                                        parsed_socks_proxy.port,
                                        rdns,
                                        parsed_socks_proxy.username,
                                        parsed_socks_proxy.password)
    elif args.proxy == 'none':
        proxy_handler = ProxyHandler({})
    else:
        if not args.proxy.lower().startswith('http'):
            args.proxy = 'http://' + args.proxy
        proxy_handler = ProxyHandler({
            'http': args.proxy,
            'https': args.proxy
        })

    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')
Ejemplo n.º 7
0
    def prepare(self):
        add_default_handler(HTTPCookieProcessor)
        install_default_handlers()
        add_header('Referer', self.url)

        info = VideoInfo(self.name)
        if self.url and not self.vid:
            self.vid = match1(self.url, 'com/[bl]/\d+/(\d+).html')
            if self.vid is None:
                self.vid = match1(self.url, 'com/s/(\d+).html')
            if self.vid is None:
                html = get_content(self.url)
                if match1(self.url, 'com/h/(\d+).html'):
                    from ykdl.util.jsengine import JSEngine
                    assert JSEngine, 'No JS Interpreter found!!!'
                    js_ctx = JSEngine()
                    js = match1(html, '<script>window.__NUXT__=(.+);</script>')
                    data = str(js_ctx.eval(js))
                    self.vid = match1(data, "PartId': '(\d+)'")
                else:
                    self.vid = match1(
                        html, 'window.location = "/b/\d+/(\d+).html"',
                        r'routePath:"\\u002Fl\\u002F\d+\\u002F(\d+).html"',
                        'vid[=:]\D?(\d+)')
        assert self.vid, 'can not find video!!!'

        did = str(uuid.uuid4())
        tk2 = generate_tk2(did)

        api_info_url = 'https://pcweb.api.mgtv.com/player/video?tk2={}&video_id={}&type=pch5'.format(
            tk2, self.vid)
        meta = json.loads(get_content(api_info_url))
        self.logger.debug('meta >\n%s', meta)

        assert meta['code'] == 200, '[failed] code: {}, msg: {}'.format(
            meta['code'], meta['msg'])
        assert meta['data'], '[Failed] Video info not found.'

        pm2 = meta['data']['atc']['pm2']
        info.title = meta['data']['info']['title'] + ' ' + meta['data'][
            'info']['desc']

        api_source_url = 'https://pcweb.api.mgtv.com/player/getSource?pm2={}&tk2={}&video_id={}&type=pch5'.format(
            pm2, tk2, self.vid)
        meta = json.loads(get_content(api_source_url))

        assert meta['code'] == 200, '[failed] code: {}, msg: {}'.format(
            meta['code'], meta['msg'])
        assert meta['data'], '[Failed] Video source not found.'

        data = meta['data']
        domain = data['stream_domain'][0]
        for lstream in data['stream']:
            lurl = lstream['url']
            if lurl:
                lurl = '{}{}&did={}'.format(domain, lurl, did)
                url = json.loads(get_content(lurl))['info']
                video_profile = lstream['name']
                stream = self.profile_2_types[video_profile]
                info.streams[stream] = {
                    'container': 'm3u8',
                    'video_profile': video_profile,
                    'src': [url]
                }
                info.stream_types.append(stream)
        info.stream_types = sorted(info.stream_types,
                                   key=self.supported_stream_types.index)
        info.extra['referer'] = self.url
        return info