voddown.py v0.3 基本上就这样吧
如果不影响使用的话,不打算改了。
或许等过段时间学习图形界面的时候再改一下。
1 # -*- coding: gbk -*- 2 #0.1 实现下载、断点续传 3 #0.2 自定义保存路径,添加处理命令行参数,用于实现批处理 4 #0.3 修复了一个0.3的bug,增加下载速度显示,改进批量下载 5 import httplib 6 import string 7 import os 8 import os.path 9 import time 10 import sys 11 import urllib2 12 13 def getvodheaders(vod_num): 14 vod_url="/webmedia/webmedia.tfs?cmd=1&clientver=4801&uuid=ef47ddd514e24e45-af0b-b9f9&"+\ 15 "prog_id="+vod_num+\ 16 "&server_id=1&customer_id=2&"+\ 17 "requesttype=1&Offset=0" 18 vod_headers={"Host":"vod.ccsu.cn","Accept":" */*",\ 19 "User-Agent":"Viewgood/1.0 (1; 1; 1)",\ 20 "Connection":"Keep-Alive",\ 21 "Pragma":"no-cache",\ 22 "Accept-Encoding":"gzip, deflate"} 23 return vod_url,vod_headers 24 25 def formatsize(size): 26 if size<1000: 27 return str(size)+"Byte" 28 else: 29 if size<1000000: 30 return "%.2fKB"%(size/1000.00) 31 else: 32 if size<1000000000: 33 return "%.2fMB"%(size/1000000.00) 34 else: 35 return "%.2fGB"%(size/1000000000.00) 36 37 38 def getfilename(vod_num): 39 url="http://vod.ccsu.cn/webmedia/webmedia.das?"+\ 40 "cmd=1&clientver=4801&"\ 41 "prog_id="+vod_num+\ 42 "&customer_id=2&uuid=ef47ddd514e24e45-af0b-b9f9"+\ 43 "&useragent=9.0.8112.16421&requesttype=1&Offset=0" 44 try: 45 res=urllib2.urlopen(url) 46 tmp=res.read() 47 except: 48 print "no file",vod_num 49 os.system("pause") 50 sys.exit(0) 51 tmp=string.split(tmp,chr(0)) 52 stype=string.split(tmp[3],"=") 53 sname=string.split(tmp[5],"=") 54 return sname[1]+"."+stype[1] 55 56 def getnetfilesize(firstblock): 57 filesize=1000000 58 headsize=1024 59 headsize=string.find(firstblock,"\r\n\r\n")+4 60 ss=string.split(firstblock,"\r\n",2) 61 ss=string.split(ss[1],":") 62 filesize=int(ss[1]) 63 return filesize,headsize 64 65 def getfullfilesize(vod_num): 66 vod_url,vod_headers=getvodheaders(vod_num) 67 nfsize=0 68 hsize=0 69 try: 70 conn=httplib.HTTPConnection("vod.ccsu.cn") 71 conn.request("GET",vod_url,"",vod_headers) 72 res=conn.getresponse() 73 nfsize,hsize=getnetfilesize(res.read(100)) 74 except: 75 print "net connect error" 76 os.system("pause") 77 sys.exit(0) 78 finally: 79 conn.close() 80 return nfsize 81 82 def getsavepath(): 83 spath=os.path.dirname(sys.argv[0]) 84 pathfile=os.path.join(spath,"path.txt") 85 try: 86 sfp=open(pathfile,"r") 87 spath=sfp.read() 88 except: 89 # "path.txt open error" 90 sfp=open(pathfile,"w") 91 finally: 92 sfp.close() 93 if os.path.isdir(spath) == False: 94 spath=os.path.dirname(sys.argv[0]) 95 sfp=open(pathfile,"w") 96 print "new path:",spath 97 try: 98 sfp.write(spath) 99 except: 100 print "path.txt write error" 101 finally: 102 sfp.close() 103 return spath 104 105 106 def downloadfile(vod_num,offset=0): 107 vod_url,vod_headers=getvodheaders(vod_num) 108 speed=0 109 readsize_befor=0 110 start_time=0 111 end_time=0 112 escap_time=0 113 speed_c=True 114 fp=file(savefilename,"ab+") 115 try: 116 conn=httplib.HTTPConnection("vod.ccsu.cn") 117 conn.request("GET",vod_url,"",vod_headers) 118 res=conn.getresponse() 119 fp.seek(0,2) 120 bs=1024*10 #block size 121 readsize=offset #size of read 122 filetoreadsize=0 # 123 longyu=0 124 baifenbi="" 125 print "downloading..." 126 while 1: 127 if speed_c==True: 128 start_time=time.time() 129 #print "start_time:",start_time 130 speed_c=False 131 readsize_befor=readsize 132 block=res.read(bs) 133 if block == "": 134 baifenbi="%%%.2f"%(readsize*100.0/netfilesize) 135 os.system("title "+baifenbi+" "+formatsize(readsize)+" "+formatsize(speed)+"/s") 136 break 137 if readsize == offset: 138 filetoreadsize,headsize=getnetfilesize(block) 139 block=block[headsize:] 140 readsize += len(block) 141 fp.write(block) 142 143 else: 144 readsize += len(block) 145 if readsize>netfilesize: 146 longyu=readsize-netfilesize 147 block=block[:-longyu] 148 readsize=netfilesize 149 fp.write(block) 150 break 151 fp.write(block) 152 end_time=time.time() 153 #print "end_time:",end_time 154 escap_time=end_time-start_time 155 if escap_time>= 1: 156 speed=(readsize-readsize_befor)/escap_time 157 speed_c=True 158 baifenbi="%%%.2f"%(readsize*100.0/netfilesize) 159 os.system("title "+baifenbi+" "+formatsize(readsize)+" "+formatsize(speed)+"/s") 160 except: 161 print "download error" 162 finally: 163 conn.close() 164 fp.close() 165 print "-"*30 166 print "download size:%dByte"%(readsize) 167 if readsize<netfilesize: 168 print "lost of size:%dByte"%(netfilesize-readsize) 169 os.system("pause") 170 sys.exit(0) 171 else: 172 print "OK!" 173 174 def startdownload(vod_num): 175 global savefilename 176 global netfilesize 177 global savepath 178 print "connecting..." 179 savefilename=vod_num+"_"+getfilename(vod_num) 180 savefilename=os.path.join(savepath,savefilename) 181 offset=0 182 if os.path.isfile(savefilename): 183 offset=os.path.getsize(savefilename) 184 print "exist file size:"+formatsize(offset) 185 netfilesize=getfullfilesize(vod_num) 186 print "net file size:"+formatsize(netfilesize) 187 logfp=open(os.path.join(os.path.dirname(sys.argv[0]),"voddown.log"),"a") 188 logfp.writelines(time.strftime('%Y-%m-%d',time.localtime(time.time()))+" "+vod_num+" "+savefilename+"\n") 189 logfp.close() 190 print "saved in:",savefilename 191 if offset<netfilesize: 192 downloadfile(vod_num,offset) 193 else: 194 print "OK!" 195 196 197 if __name__ == "__main__": 198 #34778 is small 199 print "****vod download****" 200 print "v0.3" 201 print "--code by gk" 202 print "********************" 203 global savepath 204 savepath=getsavepath() 205 if len(sys.argv) == 1: 206 vod_num=raw_input("please input an id:") 207 startdownload(vod_num) 208 os.system("pause") 209 else: 210 if os.path.isfile(sys.argv[1]): 211 tfp=open(sys.argv[1],"r") 212 try: 213 while 1: 214 id=tfp.readline() 215 if len(id)<3: 216 break 217 else: 218 cmd="start cmd /k "+'''"'''+sys.argv[0]+'''"'''+" "+str(id) 219 os.system(cmd) 220 finally: 221 tfp.close() 222 else: 223 for vod_num in sys.argv[1:]: 224 startdownload(vod_num)
没有装Python的同学可以使用这个导出的exe文件。
顺便做了一个导出视频列表的东西,前几天把现有的视频都导出来了。
1 # -*- coding: gbk -*- 2 #list all vod file 3 import httplib 4 import string 5 import os 6 import os.path 7 import time 8 import sys 9 import urllib2 10 11 def getvodheaders(vod_num): 12 vod_url="/webmedia/webmedia.tfs?cmd=1&clientver=4801&uuid=ef47ddd514e24e45-af0b-b9f9&"+\ 13 "prog_id="+vod_num+\ 14 "&server_id=1&customer_id=2&"+\ 15 "requesttype=1&Offset=0" 16 vod_headers={"Host":"vod.ccsu.cn","Accept":" */*",\ 17 "User-Agent":"Viewgood/1.0 (1; 1; 1)",\ 18 "Connection":"Keep-Alive",\ 19 "Pragma":"no-cache",\ 20 "Accept-Encoding":"gzip, deflate"} 21 return vod_url,vod_headers 22 23 def formatsize(size): 24 if size<1000: 25 return str(size)+"Byte" 26 else: 27 if size<1000000: 28 return "%.2fKB"%(size/1000.00) 29 else: 30 if size<1000000000: 31 return "%.2fMB"%(size/1000000.00) 32 else: 33 return "%.2fGB"%(size/1000000000.00) 34 35 36 def getfilename(vod_num): 37 url="http://vod.ccsu.cn/webmedia/webmedia.das?"+\ 38 "cmd=1&clientver=4801&"\ 39 "prog_id="+vod_num+\ 40 "&customer_id=2&uuid=ef47ddd514e24e45-af0b-b9f9"+\ 41 "&useragent=9.0.8112.16421&requesttype=1&Offset=0" 42 try: 43 res=urllib2.urlopen(url) 44 tmp=res.read() 45 tmp=string.split(tmp,chr(0)) 46 stype=string.split(tmp[3],"=") 47 sname=string.split(tmp[5],"=") 48 except: 49 return "" 50 return sname[1]+"."+stype[1] 51 52 def getnetfilesize(firstblock): 53 filesize=1000000 54 headsize=1024 55 headsize=string.find(firstblock,"\r\n\r\n")+4 56 ss=string.split(firstblock,"\r\n",2) 57 ss=string.split(ss[1],":") 58 filesize=int(ss[1]) 59 return filesize,headsize 60 61 def getfilesize(vod_num): 62 vod_url,vod_headers=getvodheaders(vod_num) 63 nfsize=0 64 hsize=0 65 try: 66 conn=httplib.HTTPConnection("vod.ccsu.cn") 67 conn.request("GET",vod_url,"",vod_headers) 68 res=conn.getresponse() 69 nfsize,hsize=getnetfilesize(res.read(100)) 70 except: 71 return 0 72 finally: 73 conn.close() 74 return nfsize 75 76 77 if __name__ == "__main__": 78 #34778 is small 79 print "****vod file list****" 80 print "v0.1" 81 print "--code by gk" 82 print "********************" 83 start_id=raw_input("start id:") 84 end_id=raw_input("end id:") 85 fp=open("vodlist"+"_"+start_id+"-"+end_id+".txt","a+") 86 err_num=0 87 fileid=0 88 filename="" 89 filesize=0 90 filenum=0 91 txt="" 92 try: 93 for fileid in range(int(start_id),int(end_id)+1,1): 94 filename=getfilename(str(fileid)) 95 if filename == "": 96 os.system("title "+start_id+"-"+end_id+" bad id"+str(fileid)) 97 continue 98 else: 99 filesize=getfilesize(str(fileid)) 100 if filesize == 0: 101 os.system("title "+start_id+"-"+end_id+" bad id"+str(fileid)) 102 continue 103 else: 104 err_num=0 105 txt=str(fileid)+" "+filename+" "+str(filesize) 106 print txt 107 fp.write(txt+"\n") 108 filenum+=1 109 finally: 110 fp.close() 111 print "list %d files"%(filenum) 112 113 114
这个是倒出来的文件:猛击这里下载