acupload:命令行Acfun直传上传工具

在dantmnf的激励下做了这个东西。
其实也算是向biliupload致敬了。
至少思路是一样的,虽然具体操作有所不同。
最终的检验我用的是API,不是简单的返回值。加上处理一些特殊情况,代码就长了一些。这不代表Python比ruby难看。。。
地址:
https://github.com/cnbeining/acupload
代码:

#!/usr/bin/env python3
#coding:utf-8
# Author:  Beining --<ACICFG>
# Purpose:  Upload to Letv via Acfun's API
# Created: 07/17/2014
#python3 acup.py 0.flv 1.flv
import urllib.request
import sys
import os
import json
import subprocess
import hashlib
global cookiepath
cookiepath = './accookies'
global vu_list
vu_list = []
#----------------------------------------------------------------------
def check_upload(source_id):
    """"""
    str2Hash = 'cfflashformatjsonran0.7214574650861323uu2d8c027396ver2.1vu' + source_id + 'bie^#@(%27eib58'
    sign = hashlib.md5(str2Hash.encode('utf-8')).hexdigest()
    request_info = urllib.request.Request('http://api.letvcloud.com/gpc.php?&sign='+sign+'&cf=flash&vu='+source_id+'&ver=2.1&ran=0.7214574650861323&qr=2&format=json&uu=2d8c027396')
    response = urllib.request.urlopen(request_info)
    data = response.read()
    print(json.loads(data.decode('utf-8'))['message'])
#----------------------------------------------------------------------
def upload(file2Upload):
    """"""
    #Get filename
    filename = file2Upload.split('/')[-1].strip()
    #print(filename)
    if not os.path.isfile(file2Upload):
        print('Not file!')
        exit()
    #Calculate Filesize
    filesize = os.path.getsize(file2Upload)
    #print(filesize)
    #Read Cookie.....Damn it I didn't have my supper!
    try:
        cookies = open(cookiepath, 'r').readline()
        #print(cookies)
    except:
        print('I am hungry, please give me your Cookie!')
        exit()
    #Fetch UploadUrl
    request_full = urllib.request.Request('http://www.acfun.com/video/createVideo.aspx?type=letv&filesize=' + str(filesize), headers={ 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' , 'Cookie': cookies,})
    try:
        response = urllib.request.urlopen(request_full)
    except:
        print('Cannot get response from server!')
    data = response.read()
    uploadresponse = json.loads(data.decode('utf-8'))
    if uploadresponse["success"] is 'false':
        print('ERROR: '+ uploadresponse['info'])
        exit()
    #print(uploadresponse['upload_url'])
    #make filename
    remote_name = uploadresponse['sourceId'] + '|' + str(filesize)
    status_url = uploadresponse['progress_url']
    source_id = uploadresponse['sourceId']
    #start upload
    upload_url = str(uploadresponse['upload_url'])
    #print(upload_url)
    os.system('curl -F \"file=@'+filename+'\"  \"'+upload_url+'\"')
    print('Hope everything is fine. '+source_id+'\n')
    vu_list.append([filename, source_id])
#----------------------------------------------------------------------
def cmd_exists(cmd):
    return subprocess.call("type " + cmd, shell=True,
        stdout=subprocess.PIPE, stderr=subprocess.PIPE) == 0
#----------------------------------------------------------------------
if __name__=='__main__':
    #Test curl
    if not cmd_exists('curl'):
        print ('We need curl to upload your file! Get one with apt-get or yum.')
        exit()
    total_file_num = len(sys.argv[1:])
    i = 0
    for name in sys.argv[1:]:
        #print(name)
        i = i + 1
        print('Uploading '+ str(i)+' in '+str(total_file_num)+' files...')
        upload(name)
    for media in vu_list:
        check_upload(media[1])
    for vu in vu_list:
        print(vu[0]+' : '+vu[1])

 

Leave a Reply

Your email address will not be published. Required fields are marked *