用Python写的学校图书馆视频下载工具
学校图书馆视频用webplayer建的,上面的电影更新得比较快,主要还是在局域网内,速度应该可以保证。
但是这个这能在线看,同学都希望能有一个可以下载视频的工具。
我用酷抓6获取了url和http请求头后,用python写了这样一个下载工具,还比较简陋。
刚学Python不久,写的时候都是一边翻着手册的,代码也很乱,凑合着。
1 import httplib 2 import string 3 import os 4 import os.path 5 import time 6 7 def getfilename(vod_num): 8 vod_url="http://vod.ccsu.cn/webmedia/webmedia.das?cmd=1&clientver=4801&prog_id="+vod_num+"&customer_id=2&local=172.16.145.30&proxy=172.16.145.30&uuid=c4cb4544a8c54133-a7ab-0700&osver=Unknown&useragent=9.0.8112.16421&requesttype=1&Offset=0" 9 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"} 10 try: 11 conn=httplib.HTTPConnection("vod.ccsu.cn") 12 conn.request("GET",vod_url,"",vod_headers) 13 res=conn.getresponse() 14 finally: 15 conn.close() 16 tmp=res.read() 17 print tmp 18 tmp=string.split(tmp,chr(0)) 19 stype=string.split(tmp[3],"=") 20 sname=string.split(tmp[5],"=") 21 return sname[1]+"."+stype[1] 22 23 def getnetfilesize(firstblock): 24 filesize=1000000 25 headsize=1024 26 headsize=string.find(firstblock,"\r\n\r\n")+4 27 ss=string.split(firstblock,"\r\n",2) 28 ss=string.split(ss[1],":") 29 filesize=int(ss[1]) 30 return filesize,headsize 31 32 def getfullfilesize(vod_num): 33 vod_url="/webmedia/webmedia.tfs?cmd=1&clientver=4801&uuid=347f67d\d153c4887-9cf9-08a8&prog_id="\ 34 +vod_num+\ 35 "&server_id=1&customer_id=7&proxy=&requesttype=1&Offset=0" 36 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"} 37 nfsize=0 38 hsize=0 39 try: 40 conn=httplib.HTTPConnection("vod.ccsu.cn") 41 conn.request("GET",vod_url,"",vod_headers) 42 res=conn.getresponse() 43 nfsize,hsize=getnetfilesize(res.read(100)) 44 finally: 45 conn.close() 46 return nfsize 47 48 def downloadfile(vod_num,offset=0): 49 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) 50 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"} 51 fp=file(savefilename,"ab+") 52 try: 53 conn=httplib.HTTPConnection("vod.ccsu.cn") 54 conn.request("GET",vod_url,"",vod_headers) 55 res=conn.getresponse() 56 fp.seek(0,2) 57 bs=1024*100 #block size 58 readsize=offset #size of read 59 filetoreadsize=0 # 60 longyu=0 61 baifenbi="" 62 print "saved in:",savefilename 63 print "downloading..." 64 while 1: 65 block=res.read(bs) 66 if block == "": 67 break 68 if readsize == offset: 69 filetoreadsize,headsize=getnetfilesize(block) 70 block=block[headsize:] 71 readsize += len(block) 72 fp.write(block) 73 74 else: 75 readsize += len(block) 76 fp.write(block) 77 baifenbi="%%%.2f"%(readsize*100.0/netfilesize) 78 os.system("title "+baifenbi+" "+str(readsize/1000)+"kb") 79 80 finally: 81 conn.close() 82 fp.close() 83 print "-"*30 84 print "download_size:%db"%(readsize) 85 if readsize<netfilesize: 86 print "lost of size:%db"%(netfilesize-readsize) 87 88 def getsavepath(): 89 # 90 # 91 pfp=open("path.txt","r+") 92 try: 93 ppath=pfp.readline() 94 except: 95 print "read path.txt error" 96 return False 97 finally: 98 pfp.close() 99 if os.path.isdir(ppath) == False: 100 print "path error" 101 print "use cwd" 102 ppath=os.getcwd() 103 pfp=open("path.txt","w") 104 try: 105 pfp.write(ppath) 106 except: 107 print "crate path.txt error" 108 return False 109 finally: 110 pfp.close() 111 return ppath 112 113 def main(): 114 #34778 is small 115 print "****vod download****".decode("gbk") 116 print "--code by gk" 117 vod_num=raw_input("please input an id:") 118 print "connecting..." 119 global savefilename 120 global netfilesize 121 savefilename=getsavepath()+"\\"+vod_num+"_"+getfilename(vod_num) 122 offset=0 123 if os.path.isfile(savefilename): 124 offset=os.path.getsize(savefilename) 125 print "exist file size:%db"%(offset) 126 netfilesize=getfullfilesize(vod_num) 127 print "net file size:%db"%(netfilesize) 128 logfp=open("vodlog.log","a") 129 logfp.writelines(time.strftime('%Y-%m-%d',time.localtime(time.time()))+" "+vod_num+" "+savefilename+"\n") 130 logfp.close() 131 if offset<netfilesize: 132 downloadfile(vod_num,offset) 133 print "ok" 134 os.system("pause") 135 136 if __name__ == "__main__": 137 main()
用GUI2EXE打包成exe文件就可以给同学用了。
以后打算用wxPython写个图形界面