今天把voddown.py修改了一下,添加了文件保存路径配置文件path.txt
支持读取命令行参数,这样就可以用于批量下载。
比如有个bat文件:
start cmd /k voddown.py 34000 start cmd /k voddown.py 34001 start cmd /k voddown.py 34002 start cmd /k voddown.py 34003
跟voddown.py放在同一目录下,就能同时下载34000-34003这四个文件了
下面是voddown.py的代码:
1 # -*- coding: cp936 -*- 2 #0.1 实现下载、断点续传 3 #0.2 自定义保存路径 4 #0.3 添加处理命令行参数,用于实现批处理 5 import httplib 6 import string 7 import os 8 import os.path 9 import time 10 import sys 11 12 def getfilename(vod_num): 13 vod_url="/webmedia/webmedia.das?cmd=1&clientver=4801&prog_id="+vod_num+"&customer_id=2&uuid=d6b9f987cf334ef3-b0fa-7e72&osver=Unknown&useragent=9.0.8112.16421&requesttype=1&Offset=0" 14 vod_headers={"Host":"vod.ccsu.cn","Accept":"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/vgplayer, application/x-shockwave-flash, */*","User-Agent":"Viewgood/1.0 (1; 1; 1)","Connection":"Keep-Alive","Pragma":"no-cache","Accept-Encoding":"gzip, deflate"} 15 try: 16 conn=httplib.HTTPConnection("vod.ccsu.cn") 17 conn.request("GET",vod_url,"",vod_headers) 18 res=conn.getresponse() 19 finally: 20 conn.close() 21 tmp=res.read() 22 tmp=string.split(tmp,chr(0)) 23 stype=string.split(tmp[3],"=") 24 sname=string.split(tmp[5],"=") 25 return sname[1]+"."+stype[1] 26 27 def getnetfilesize(firstblock): 28 filesize=1000000 29 headsize=1024 30 headsize=string.find(firstblock,"\r\n\r\n")+4 31 ss=string.split(firstblock,"\r\n",2) 32 ss=string.split(ss[1],":") 33 filesize=int(ss[1]) 34 return filesize,headsize 35 36 def getfullfilesize(vod_num): 37 vod_url="/webmedia/webmedia.tfs?cmd=1&clientver=4801&uuid=347f67d\d153c4887-9cf9-08a8&prog_id="\ 38 +vod_num+\ 39 "&server_id=1&customer_id=7&proxy=&requesttype=1&Offset=0" 40 vod_headers={"Host":"vod.ccsu.cn","Accept":"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/vgplayer, application/x-shockwave-flash, */*","User-Agent":"Viewgood/1.0 (1; 1; 1)","Connection":"Keep-Alive","Pragma":"no-cache","Accept-Encoding":"gzip, deflate"} 41 nfsize=0 42 hsize=0 43 try: 44 conn=httplib.HTTPConnection("vod.ccsu.cn") 45 conn.request("GET",vod_url,"",vod_headers) 46 res=conn.getresponse() 47 nfsize,hsize=getnetfilesize(res.read(100)) 48 finally: 49 conn.close() 50 return nfsize 51 def getsavepath(): 52 spath="" 53 try: 54 sfp=open("path.txt","r") 55 spath=sfp.read() 56 except: 57 # "path.txt open error" 58 sfp=open("path.txt","w") 59 finally: 60 sfp.close() 61 if os.path.isdir(spath) == False: 62 spath=os.getcwd() 63 sfp=open("path.txt","w") 64 print "new path:",spath 65 try: 66 sfp.write(spath) 67 except: 68 print "path.txt open error" 69 spath=os.getcwd() 70 finally: 71 sfp.close() 72 return spath 73 74 75 def downloadfile(vod_num,offset=0): 76 vod_url="/webmedia/webmedia.tfs?cmd=1&clientver=4801&uuid=347f67dd153c4887-9cf9-08a8&prog_id="+vod_num+"&server_id=1&customer_id=7&local=172.16.145.30&proxy=&requesttype=1&Offset="+str(offset) 77 vod_headers={"Host":"vod.ccsu.cn","Accept":"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/vgplayer, application/x-shockwave-flash, */*","User-Agent":"Viewgood/1.0 (1; 1; 1)","Connection":"Keep-Alive","Pragma":"no-cache","Accept-Encoding":"gzip, deflate"} 78 fp=file(savefilename,"ab+") 79 try: 80 conn=httplib.HTTPConnection("vod.ccsu.cn") 81 conn.request("GET",vod_url,"",vod_headers) 82 res=conn.getresponse() 83 fp.seek(0,2) 84 bs=1024*100 #block size 85 readsize=offset #size of read 86 filetoreadsize=0 # 87 longyu=0 88 baifenbi="" 89 print "downloading..." 90 while 1: 91 block=res.read(bs) 92 if block == "": 93 break 94 if readsize == offset: 95 filetoreadsize,headsize=getnetfilesize(block) 96 block=block[headsize:] 97 readsize += len(block) 98 fp.write(block) 99 100 else: 101 readsize += len(block) 102 fp.write(block) 103 baifenbi="%%%.2f"%(readsize*100.0/netfilesize) 104 os.system("title "+baifenbi+" "+str(readsize/1000)+"kb") 105 except: 106 print "download error" 107 finally: 108 conn.close() 109 fp.close() 110 print "-"*30 111 print "download_size:%db"%(readsize) 112 if readsize<netfilesize: 113 print "lost of size:%db"%(netfilesize-readsize) 114 115 116 if __name__ == "__main__": 117 #34778 is small 118 print "****vod download****" 119 print "v0.3" 120 print "--code by gk" 121 if len(sys.argv) == 1: 122 vod_num=raw_input("please input an id:") 123 else: 124 for vod_num in sys.argv[1:]: 125 print "connecting..." 126 global savefilename 127 global netfilesize 128 savefilename=getsavepath()+"\\"+vod_num+"_"+getfilename(vod_num) 129 offset=0 130 if os.path.isfile(savefilename): 131 offset=os.path.getsize(savefilename) 132 print "exist file size:%db"%(offset) 133 netfilesize=getfullfilesize(vod_num) 134 print "net file size:%db"%(netfilesize) 135 logfp=open("voddown.log","a") 136 logfp.writelines(time.strftime('%Y-%m-%d',time.localtime(time.time()))+" "+vod_num+" "+savefilename+"\n") 137 logfp.close() 138 print "saved in:",savefilename 139 if offset<netfilesize: 140 downloadfile(vod_num,offset) 141 print "ok" 142 os.system("pause")