This repository has been archived by the owner on Apr 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvgnaloader.py
136 lines (115 loc) · 4.27 KB
/
pvgnaloader.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
134
135
136
import requests
import subprocess
import os
import string
import json
import math
settings = json.load(open('pvgnaloader.ini'))
res = settings['resolution']
links = settings['links']
PVGNALOGIN = 'https://pvgna.com/login'
FINDTOKENL = 'name="authenticity_token" value="'
FINDTOKENR = '"'
FINDVLINKL = ';'
FINDVLINKR = res + '.m3u8'
FINDVNAMEL = '<h1 class="ui header">'
FINDVNAMER = '</h1>'
FINDCHPTRL = '<a class="link step" href="'
FINDCHPTRR = '"'
def find_between(s, left, right):
start = s.find(left) + len(left)
end = s.find(right, start)
return s[start:end]
def find_between_r(s, right, left):
end = s.rindex(right)
start = s.rindex(left, 0, end) + len(left)
return s[start:end]
def sanitize_filename(s):
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
return ''.join(c for c in s if c in valid_chars)
def print_progressbar(iteration, total, prefix = '', suffix = '', decimals = 1, length = 50):
blocks = ["", "▌"]
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
blockindex = math.floor(((length * iteration / total) - filledLength) * len(blocks))
bar = '█' * filledLength + blocks[blockindex] + '-' * (length - filledLength - len(blocks[blockindex]))
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')
if iteration == total:
print()
print("Loading pvgna...")
s = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=10)
s.mount('https://', adapter)
s.mount('http://', adapter)
r = s.get(PVGNALOGIN)
token = find_between(r.text, FINDTOKENL, FINDTOKENR)
payload = {
'utf8': '✓',
'authenticity_token': token,
'user[email]': settings['email'],
'user[password]': settings['password'],
'commit': 'Log In'
}
print("Logging in...")
r = s.post(PVGNALOGIN, data=payload)
if r.text.find('Invalid email or password.') != -1:
print('Login incorrect')
exit()
vlinks = []
vnames = []
chapterlinks = links[:]
if settings['crawlchapters']:
for idx,link in enumerate(links):
print('\rFetching chapters... [%s]' % (str(idx+1)+'/'+str(len(links))), end='\r')
r = s.get(link)
restsrc = r.text
while restsrc.find(FINDCHPTRL) != -1:
chapterlink = 'https://pvgna.com' + find_between(restsrc, FINDCHPTRL, FINDCHPTRR)
restsrc = restsrc[restsrc.find(FINDCHPTRL) + len(FINDCHPTRL):]
if chapterlink not in chapterlinks:
chapterlinks.append(chapterlink)
print()
links = chapterlinks
for idx,link in enumerate(links):
print('\rFetching video links... [%s]' % (str(idx+1)+'/'+str(len(links))), end='\r')
r = s.get(link)
vlink = find_between_r(r.text, FINDVLINKR, FINDVLINKL)
vname = find_between(r.text, FINDVNAMEL, FINDVNAMER)
vname = sanitize_filename(vname)
vlinks.append(vlink)
vnames.append(vname)
print()
idx = 0
while(idx < len(vnames)):
mp4_name = 'videos/' + vnames[idx] + '.mp4'
if os.path.isfile(mp4_name):
print('"' + vnames[idx] + '" already exists.')
del vlinks[idx]
del vnames[idx]
else:
idx = idx + 1
if len(vnames) == 0:
print('Nothing to download.')
exit()
print('Downloading videos...')
for idx,link in enumerate(vlinks):
r = s.get(link + res + '.m3u8')
lines = r.text.splitlines()
ts_name = 'videos/' + vnames[idx] + '.ts'
with open(ts_name, 'wb') as outfile:
for idx2,ts_link in enumerate(lines):
if (ts_link.endswith('.ts')):
r = s.get(link + ts_link, stream=True)
for chunk in r:
outfile.write(chunk)
print_progressbar(idx*100+100/len(lines)*(idx2), len(vlinks)*100, str(idx+1)+'/'+str(len(vlinks)))
print_progressbar(100, 100, str(len(vlinks))+'/'+str(len(vlinks)))
for idx,vname in enumerate(vnames):
print('\rConverting videos... [%s]' % (str(idx+1)+'/'+str(len(vnames))), end='\r')
ts_name = 'videos/' + vname + '.ts'
mp4_name = 'videos/' + vname + '.mp4'
with open(os.devnull, 'w') as f:
subprocess.call('ffmpeg -y -i "' + ts_name + '" -acodec copy -vcodec copy "' + mp4_name + '"', shell=True, stdout=f, stderr=subprocess.STDOUT)
os.remove(ts_name)
print()
print("Done.")