-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain
183 lines (159 loc) · 5.3 KB
/
main
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 24 21:04:06 2016
@author: iliuh
"""
import httplib
import urllib
import subprocess
import os
import gc
from pyquery import PyQuery as pq
# note all urls are bamgumi pages
# wolf and spicy season 1 and 2
wolf1 = 'http://bangumi.bilibili.com/anime/1071'
wolf2 = 'http://bangumi.bilibili.com/anime/1072'
ua = r"Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
def list2txt(input_list, save_dir):
f = open(save_dir, 'wb')
for item in input_list:
f.write(item + '\n')
f.close()
def dic2txt(input_dic, save_dir):
f = open(save_dir, 'wb')
for d in input_dic:
f.write(str(d) + '\n')
for i in input_dic[d]:
f.write('\t' + i + '\n')
f.close()
def txt2list(save_dir):
f = open(save_dir, 'rb')
output = []
for line in f.readlines():
output.append(line)
f.close()
return output
def txt2dic(save_dir):
f = open(save_dir, 'rb')
output = {}
for line in f.readlines():
if line[0] != '\t':
header = int(line.split('\n')[0])
output[header] = []
else:
output[header].append(line.split('\t')[-1].split('\n')[0])
f.close()
return output
# get urls for each video in the bangumi
def bili_bangumi_analysis(url):
html = pq(url)
li = html('li[class="v1-bangumi-list-part-child"]')
N = len(li) / 2
a = li.children('a')
output = []
for i in range(N):
temp = pq(a[N + i])
output.append(temp.attr('href'))
return output
# use KISSTUDOU to analysis the video sources
def response(url, fmt = "real"):
http = httplib.HTTP("www.flvcd.com")
http.putrequest("GET", "/parse.php?format=" + fmt + "&kw=" + urllib.quote(url))
http.putheader("User-Agent", ua)
http.putheader("Host", "www.flvcd.com")
http.putheader("Accept", "*/*")
http.endheaders()
errcode, errmsg, headers = http.getreply()
#print "Status:", errcode, errmsg
if errcode!=200:
print "Error encountered while parsing url"
return -1
res = http.getfile()
html = ''
data = res.read(512)
while data != '':
html += data
data = res.read(512)
html = html.decode('gbk') # gbk chinese simplify encoding method
return html
# take out the video source
def html_extract(html):
q = pq(html)
form = q('form[name="mform"]')
#print form
file_a = form.parent('td').parent('tr').prev().children().children('a')
# notice this file_a includes all the <a> and form a "list"
clip_url = []
for f in file_a:
a = pq(f)
clip_url.append(a.attr('href'))
return clip_url
# download one clip using WGET
def single_download(clip_url, save_dir):
#subprocess.call("wget " + clip_url[0] + " -P " + save_dir)
#subprocess.call("wget -help", shell = True)
cmd = 'wget "' + clip_url + '" -O ' + save_dir
try:
exe(cmd)
except:
print 'break'
print clip_url
# helperfunction to do cmd in windows
def exe(cmd, std = True):
print cmd
p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)
if std:
while True:
line = p.stdout.readline()
line = line.replace('\n', '')
print line
if not line: break
def i2s_2digit(x):
return str(x) if x > 9 else '0'+str(x)
def get_bili_video_address(bili_url, fmt = "real"):
output = {}
for i, url in enumerate(bili_url):
h = response(url, fmt)
c = html_extract(h)
output[i + 1] = c
return output
# download all the clips using WGET
def massive_download(list_dict, save_location):
os.chdir(save_location)
for i in range(len(list_dict)):
print len(list_dict[i + 1])
if len(list_dict[i + 1]) == 1:
single_download(list_dict[i + 1][0], save_location + '/' + str(i + 1) + '.flv')
continue
ffmpeg_merge_txt = 'temp_' + str(i + 1) +'.txt'
f = open(ffmpeg_merge_txt, 'w')
for j, address in enumerate(list_dict[i + 1]):
filename = str(i + 1) + '_' + str(j + 1) + '.flv'
single_download(address, save_location + '/' + filename)
f.write("file '" + filename + "' \n")
f.close()
ffmpeg_cmd = 'ffmpeg -f concat -i temp_' + str(i + 1) +'.txt -c copy ' + str(i + 1) + '.flv'
#os.remove(ffmpeg_merge_txt)
try:
FNULL = open(os.devnull, 'w')
subprocess.call(ffmpeg_cmd, stdout=FNULL, stderr=FNULL, shell=False)
except:
print 'ffmpeg merge error'
# munully merge videos using FFMPEG
def merge_munully(target_dir, N):
os.chdir(target_dir)
for i in range(1, N):
cmd = 'ffmpeg -f concat -i temp_' + str(i) +'.txt -c copy ' + str(i) + '.flv'
FNULL = open(os.devnull, 'w')
subprocess.call(cmd, stdout=FNULL, stderr=FNULL, shell=False)
def main(bangumi_url, save_folder):
bangumi = bili_bangumi_analysis(bangumi_url)
download_list = get_bili_video_address(bangumi)
dic2txt(download_list, save_folder + '/.logger.txt')
# download
if not os.path.exists(save_folder):
os.mkdir(save_folder)
#temp = txt2dic(save_folder + '/.logger.txt')
#massive_download(download_list, save_folder)
gc.collect()
#main('http://bangumi.bilibili.com/anime/444', 'c:/wget/mao')