Beispiel #1
0
 def upload_curl(self, savedir, localfile):
     '''
     使用curl上传文件
     上传大文件时使用此接口,可显示上传进度
     @param savedir: 远端保存路径
     @param localfile: 本地文件完整路径
     '''
     savedir = util.encode_utf8(savedir)
     localfile = util.encode_utf8(localfile)
     url = 'https://c.pcs.baidu.com/rest/2.0/pcs/file'
     query = [('method', 'upload'), ('app_id', _APP_ID),
              ('ondup', 'newcopy'), ('dir', savedir),
              ('filename', os.path.basename(localfile)),
              ('BDUSS', urllib.unquote(self.bduss))]
     url = '%s?%s' % (url, urllib.urlencode(query))
     # 创建临时文件用来保存输出结果
     _, tmp_file = tempfile.mkstemp(suffix='.json')
     # 调用curl上传文件
     cmd = [
         'curl', '-A', _USER_AGENT, '-H', 'Expect:', '-F',
         'file=@%s;type=application/octet-stream' % localfile, '-o',
         tmp_file, url
     ]
     util.subprocess_call(cmd)
     # 读取输出结果
     with open(tmp_file, 'r') as fp:
         result = json.load(fp)
     return result
Beispiel #2
0
 def upload_curl(self, savedir, localfile):
     '''
     使用curl上传文件
     上传大文件时使用此接口,可显示上传进度
     @param savedir: 远端保存路径
     @param localfile: 本地文件完整路径
     '''
     savedir = util.encode_utf8(savedir)
     localfile = util.encode_utf8(localfile)
     url = 'https://c.pcs.baidu.com/rest/2.0/pcs/file'
     query = [
         ('method', 'upload'),
         ('app_id', _APP_ID),
         ('ondup', 'newcopy'),
         ('dir', savedir),
         ('filename', os.path.basename(localfile)),
         ('BDUSS', urllib.unquote(self.bduss))
          ]
     url = '%s?%s' % (url, urllib.urlencode(query))
     # 创建临时文件用来保存输出结果
     _, tmp_file = tempfile.mkstemp(suffix='.json')
     # 调用curl上传文件
     cmd = ['curl',
            '-A', _USER_AGENT,
            '-H', 'Expect:',
            '-F', 'file=@%s;type=application/octet-stream' % localfile,
            '-o', tmp_file, url]
     util.subprocess_call(cmd)
     # 读取输出结果
     with open(tmp_file, 'r') as fp:
         result = json.load(fp)
     return result
Beispiel #3
0
def download(download_req, save_path):
    cmd = ['wget']
    for hdr in download_req.header_items():
        cmd.append('--header')
        cmd.append('%s: %s' % hdr)
    # download url
    cmd.append(download_req.get_full_url())
    # save_path
    cmd.extend(['-O', save_path])
    # run it
    return util.subprocess_call(cmd)
Beispiel #4
0
def download(download_req, save_path):
    cmd = ['wget']
    for hdr in download_req.header_items():
        cmd.append('--header')
        cmd.append('%s: %s' % hdr)
    # download url
    cmd.append(download_req.get_full_url())
    # save_path
    cmd.extend(['-O', save_path])
    # run it
    return util.subprocess_call(cmd)
Beispiel #5
0
def download(download_req, save_path):
    cmd = ['curl', '-L']
    # request header
    for hdr in download_req.header_items():
        cmd.append('-H')
        cmd.append('%s: %s' % hdr)
    # save path
    cmd.extend(['--output', save_path])
    # download url
    cmd.append(download_req.get_full_url())
    # run it!
    return util.subprocess_call(cmd)
Beispiel #6
0
def download(download_req, save_path):
    cmd = ['curl', '-L']
    # request header
    for hdr in download_req.header_items():
        cmd.append('-H')
        cmd.append('%s: %s' % hdr)
    # save path
    cmd.extend(['--output', save_path])
    # download url
    cmd.append(download_req.get_full_url())
    # run it!
    return util.subprocess_call(cmd)
def download(download_req, save_path):
    # build command
    cmd = ['aria2c', '--file-allocation=none']
    # request header
    for hdr in download_req.header_items():
        cmd.append('--header')
        cmd.append('%s: %s' % hdr)
    # save path
    save_dir, save_name = os.path.split(save_path)
    cmd.extend(['--dir', save_dir, '--out', save_name])
    # download url
    cmd.append(download_req.get_full_url())
    # run it
    return util.subprocess_call(cmd)
Beispiel #8
0
def download(download_req, save_path):
    # build command
    cmd = ['aria2c', '--file-allocation=none']
    # request header
    for hdr in download_req.header_items():
        cmd.append('--header')
        cmd.append('%s: %s' % hdr)
    # save path
    save_dir, save_name = os.path.split(save_path)
    cmd.extend(['--dir', save_dir, '--out', save_name])
    # download url
    cmd.append(download_req.get_full_url())
    # run it
    return util.subprocess_call(cmd)
Beispiel #9
0
def play(video_req, zoom=None):
    '''
    播放视频
    @param video_req: 视频请求
    @param zoom: 缩放设置
    '''
    cmd = [
        'mplayer',
        '-quiet',  # 减少输出
        '-osdlevel',
        '1',  # 界面选项
        '-prefer-ipv4',  # 优先使用IPv4
        '-cache',
        '8192'
    ]  # 缓冲区大小
    # 缩放参数的处理
    if zoom:
        if type(zoom) is str and zoom == 'fs':
            cmd.append('-fs')
        elif type(zoom) is int:
            cmd.append('-xy')
            cmd.append(str(zoom))
        elif type(zoom) is tuple:
            cmd.append('-x')
            cmd.append(str(zoom[0]))
            cmd.append('-y')
            cmd.append(str(zoom[1]))
    # user-agent
    ua = video_req.get_header('User-agent')  # 取出header时只有首字母大写
    if ua:
        cmd.append('-user-agent')
        cmd.append(ua)
    # referer
    ref = video_req.get_header('Referer')
    if ref:
        cmd.append('-referrer')
        cmd.append(ref)
    # cookie
    cmd.extend(['-cookies', '-cookies-file', context.cookie_file])
    # 视频地址
    cmd.append(video_req.get_full_url())
    # 执行命令
    return util.subprocess_call(cmd)
Beispiel #10
0
def play(video_req, zoom=None):
    '''
    播放视频
    @param video_req: 视频请求
    @param zoom: 缩放设置
    '''
    cmd = ['mplayer',
           '-quiet',            # 减少输出
           '-osdlevel', '1',    # 界面选项
           '-prefer-ipv4',      # 优先使用IPv4
           '-cache', '8192']    # 缓冲区大小
    # 缩放参数的处理
    if zoom:
        if type(zoom) is str and zoom == 'fs':
            cmd.append('-fs')
        elif type(zoom) is int:
            cmd.append('-xy')
            cmd.append(str(zoom))
        elif type(zoom) is tuple:
            cmd.append('-x')
            cmd.append(str(zoom[0]))
            cmd.append('-y')
            cmd.append(str(zoom[1]))
    # user-agent
    ua = video_req.get_header('User-agent') # 取出header时只有首字母大写
    if ua:
        cmd.append('-user-agent')
        cmd.append(ua)
    # referer
    ref = video_req.get_header('Referer')
    if ref:
        cmd.append('-referrer')
        cmd.append(ref)
    # cookie
    cmd.extend(['-cookies', '-cookies-file', context.cookie_file])
    # 视频地址
    cmd.append(video_req.get_full_url())
    # 执行命令
    return util.subprocess_call(cmd)