-
Notifications
You must be signed in to change notification settings - Fork 0
/
maple-dl.py
137 lines (118 loc) · 4.49 KB
/
maple-dl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from BeautifulSoup import BeautifulSoup
import os,sys,string,re
import subprocess,getopt
import urllib2
URLPREFIX="http://www.youtube.com/watch?feature=player_embedded&v="
'''
kangxiurl=urllib2.urlopen("http://www.maplestage.com/show/%E5%BA%B7%E7%86%99%E4%BE%86%E4%BA%86/")
kangxicontent=kangxiurl.read()
kangxisoup = BeautifulSoup(kangxicontent)
episodes= kangxisoup.findAll(attrs={"class":"shows"})
if len(episodes)!=1:
print "Error! Found multiple tags"
sys.exit(1)
print len(episodes[0].contents)
print episodes[0].contents[0]
print episodes[0].contents[1]
print episodes[0].contents[800]
#episodessoup = BeautifulSoup(episodes[0].contents)
#episodes=episodesoup.findAll(attrs={"class":"show"})
#print len(episodes)
'''
KANGXI="http://www.maplesi.com/node/81949/%E5%BA%B7%E7%86%99%E4%BE%86%E4%BA%86-20130619-%E9%BB%83%E7%BE%A9%E9%81%94-%E9%84%A7%E7%A6%8F%E5%A6%82-%E4%B8%81%E5%99%B9-%E5%AE%B6%E5%AE%B6-mp%E9%AD%94%E5%B9%BB%E5%8A%9B%E9%87%8F-%E9%BB%83%E5%9C%8B%E5%80%AB-%E9%82%A3%E4%BA%9B%E5%B9%B4%E6%88%91%E5%80%91%E6%B5%81%E8%A1%8C%E7%9A%84%E7%B6%93%E5%85%B8%E6%AD%8C%E6%9B%B2/"
def usage():
print "Usage: %s [-h] [-u url] [-r retry_times]"%sys.argv[0]
print " -u url: the target url on maplestage site"
print " -r retry_times: retry how many times at most if failed"
def main(argv):
limits=5
try:
opts,args=getopt.getopt(argv,"hu:r:")
except getopt.GetoptError:
usage()
sys.exit(1)
for opt,arg in opts:
if opt in ('-h','--help'):
usage()
sys.exit(0)
elif opt in('-u'):
urlAddr=arg
elif opt in('-r'):
limits=int(arg)
if urlAddr=="":
print "Please input target url"
sys.exit(1)
url = urllib2.urlopen(urlAddr)
content = url.read()
soup = BeautifulSoup(content)
links = soup.findAll(attrs={"class":"video_ids"})
#for link in links:
for i in range(0,len(links)):
link = links[i]
print "Found video_ids set %d:"%(i+1)
ids=link.contents[0]
#print ids
ids=string.split(ids,",")
for j in range(0,len(ids)):
print " Part %d: %s"%(j+1,ids[j])
#ids=['a','b','c','d','e']
#if 1:
print "Which parts would u like to download?"
print "Starting input, e.g. 1-5(inclusive) or 2,3,4?"
parts=string.strip(raw_input())
print parts
pstr1="[1-9]\d*-[1-9]\d*"
pattern1=re.compile(pstr1)
pstr2="[1-9]\d*(,[1-9]\d*)*[,]*"
pattern2=re.compile(pstr2)
res=pattern1.search(parts)
urlstr=""
if res!= None:
parts_index=string.split(res.group(0),'-')
ibegin=int(parts_index[0])
iend=int(parts_index[1])
if ibegin<1 :
print "Begin from Part 1 since %d is negative"%ibegin
ibegin = 1
if iend > len(ids):
print "End at Part %d since %d is too big"%(len(ids),iend)
iend = len(ids)
urlstr="%s%s"%(URLPREFIX,ids[ibegin-1])
for i in range(ibegin,iend):
urlstr=urlstr+" %s%s"%(URLPREFIX,ids[i])
else:
res=pattern2.search(parts)
if res!= None:
strres=res.group(0)
if strres[len(strres)-1]==',':
strres=strres[0:len(strres)-1]
parts_index=string.split(strres,',')
print parts_index
added=[]
for one_index in parts_index:
one_index=int(one_index)
if one_index <1 or one_index > len(ids):
print "Part %d ignored since it is invalid"%one_index
else:
if not one_index in added:
urlstr=urlstr+" %s%s"%(URLPREFIX,ids[one_index-1])
added.append(one_index)
else:
print "Part %d ignored since it is repetive"%one_index
urlstr=string.lstrip(urlstr)
print urlstr
cmd=['.\\youtube-dl.exe']+string.split(urlstr)
print cmd
#If failed try 5 times at most
for i in range(0,limits):
res=subprocess.call(cmd)
if res == 0:
break
#if string.find(string.lower(cmdout),'error') == -1:
break
if __name__=='__main__':
if len(sys.argv) == 1:
print "No args"
usage()
sys.exit(1)
main(sys.argv[1:])