-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive-dl-1.0.py
executable file
·328 lines (304 loc) · 12.5 KB
/
archive-dl-1.0.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#! /bin/python
import re
import sys
import argparse
import requests
from urllib.parse import unquote
from clint.textui import progress
from bs4 import BeautifulSoup
def determinePage(url):
if url.find('@') >= 0: # Test for a userpage
print('Found userpage: ' + url)
userpage(url)
else:
getpage= requests.get(url, headers={'user-agent': 'archive-dl'}, stream=True)
getpage_soup=BeautifulSoup(getpage.text, 'html.parser')
items= getpage_soup.find('div', {'id':'theatre-ia-wrap'}) # Test for a videopage
if items is not None:
sronly= getpage_soup.find('div', {'class':'streamo'}) # Test for stream only
if sronly is not None:
print('Found Stream Only page: ' + url)
streamonlypage(url)
else:
print('Found video page: ' + url)
videopage(url)
elif getpage_soup.find('div', {'class':'download-directory-listing'}) is not None:
print('Found download page: ' + url)
downloadpage(url)
else:
print('Maybe a collection? ' + url)
userpage(url) # If it's not a userpage or videopage it's probably a collection
def downloadFile(name, dl_url):
r=requests.get(dl_url, headers={'user-agent': 'archive-dl'}, stream=True)
if r.status_code == 200:
with open(name, 'wb') as f:
print("Downloading %s" % name)
total_length = int(r.headers.get('content-length'))
for chunk in progress.bar(r.iter_content(chunk_size=1024), expected_size=(total_length/1024) + 1):
if chunk:
f.write(chunk)
f.flush()
f.close()
else:
print('File not found.')
def downloadpage(url):
getpage = requests.get(url, headers={'user-agent': 'archive-dl'}, stream=True)
getpage_soup = BeautifulSoup(getpage.text, 'html.parser')
download = getpage_soup.findAll('a')
if verbose is True:
print('Looking in ' + url)
findFiles(url, download) # Find files on the download page
findDirectories(url, download) # Look for directories
def findDirectories(url, download):
loop = True # Create a loop to search through directories if they are found
while loop is True:
linksFound = False
for links in download: # Looking for directories
folder = links.get('href')
if folder is not None and folder.endswith('/') and folder.endswith('create/') <= 0 and folder.endswith('web/') <= 0 and folder.endswith('about/') <= 0 and folder.endswith('.org/') <= 0 and folder.endswith('projects/') <= 0 and folder.endswith('donate/') <= 0 and folder.endswith('../') <= 0:
upurl = url + folder
if verbose is True:
print('Looking in ' + upurl)
getpage = requests.get(upurl, headers={'user-agent': 'archive-dl'}, stream=True)
getpage_soup = BeautifulSoup(getpage.text, 'html.parser')
updownload = getpage_soup.findAll('a')
findFiles(upurl, updownload) # Find files in the directory
linksFound = True # Continue the loop if more links are found
if linksFound is not False:
loop = True
download = updownload
url = upurl
else:
loop = False
def findFiles(url, download):
foundlist = []
for i in download:
dl= i.get('href')
if dl is not None and verbose is True:
print('Looking at ' + dl)
if dl is not None and yesAudio is False and dl.find('.ia.mp4') >= 0:
dl_url= url + dl
name=unquote(dl.rsplit('/', 1)[-1]) # Grab the name from the url
foundlist.append(dl.rsplit('.ia',1)[0]) # .ia.mp4 files to compare against later
if yesDownload == True:
fileExists(name, dl_url)
elif justList == True:
print('Found ' + name)
else:
writeFile(dl_url)
for i in download:
dl = i.get('href')
if dl is not None and yesAudio is False and dl.find('.mp4') >= 0:
dl_url= url + dl
name=unquote(dl.rsplit('/', 1)[-1]) # Grab the name from the url
test=dl.rsplit('.mp4',1)[0] # Test to see if file has already been grabbed
if test not in foundlist:
foundlist.append(test)
if yesDownload == True:
fileExists(name, dl_url)
elif justList == True:
print('Found ' + name)
else:
writeFile(dl_url)
for i in download:
dl = i.get('href')
if dl is not None and yesAudio is False and dl.find('.mkv') >= 0:
dl_url= url + dl
name=unquote(dl.rsplit('/', 1)[-1]) # Grab the name from the url
test=dl.rsplit('.mkv',1)[0] # Test to see if file has already been grabbed
if test not in foundlist:
foundlist.append(test)
if yesDownload == True:
fileExists(name, dl_url)
elif justList == True:
print('Found ' + name)
else:
writeFile(dl_url)
for i in download:
dl = i.get('href')
if dl is not None and yesAudio is False and dl.find('.m.k.v') >= 0:
dl_url= url + dl
name=unquote(dl.rsplit('/', 1)[-1]) # Grab the name from the url
test=dl.rsplit('.m.k.v',1)[0] # Test to see if file has already been grabbed
if test not in foundlist:
foundlist.append(test)
if yesDownload == True:
fileExists(name, dl_url)
elif justList == True:
print('Found ' + name)
else:
writeFile(dl_url)
for i in download:
dl = i.get('href')
if dl is not None and yesAudio is False and dl.find('.avi') >= 0:
dl_url= url + dl
name=unquote(dl.rsplit('/', 1)[-1]) # Grab the name from the url
test=dl.rsplit('.avi',1)[0] # Test to see if file has already been grabbed
if test not in foundlist:
foundlist.append(test)
if yesDownload == True:
fileExists(name, dl_url)
elif justList == True:
print('Found ' + name)
else:
writeFile(dl_url)
for i in download:
dl = i.get('href')
if dl is not None and yesAudio is True and dl.find('.mp3') >= 0:
dl_url= url + dl
name=unquote(dl.rsplit('/', 1)[-1]) # Grab the name from the url
test=dl.rsplit('.mp3',1)[0] # Test to see if file has already been grabbed
if test not in foundlist:
foundlist.append(test)
if yesDownload == True:
fileExists(name, dl_url)
elif justList == True:
print('Found ' + name)
else:
writeFile(dl_url)
for i in download:
dl = i.get('href')
if dl is not None and dl.find('.zip') >= 0:
dl_url= url + dl
name=unquote(dl.rsplit('/', 1)[-1]) # Grab the name from the url
test=dl.rsplit('.zip',1)[0] # Test to see if file has already been grabbed
if test not in foundlist:
foundlist.append(test)
if yesDownload == True:
fileExists(name, dl_url)
elif justList == True:
print('Found ' + name)
else:
writeFile(dl_url)
def fileExists(name, dl_url):
try:
file = open(name)
file.close()
return
yes = set(['yes','y'])
no = set(['no','n',''])
while True:
ans=input("File " + name + " already exists. Overwrite? N/y ").lower()
if ans in yes:
downloadFile(name, dl_url)
elif ans in no:
return
else:
print('Yes or No?')
except FileNotFoundError:
downloadFile(name, dl_url)
def getArguments():
global url, textFile, yesDownload, justList, yesAudio, batchFile, verbose
parser = argparse.ArgumentParser(description='Downloads media from Archive.org')
group = parser.add_mutually_exclusive_group()
group.add_argument('-w',
action='store',
dest='textFile',
default='video-dl.txt',
help='Create a list of the videos found with this name.',
required=False
)
group.add_argument('-d',
action='store_true',
dest='yesDownload',
default=False,
help='Downloads videos',
required=False
)
group.add_argument('-l',
action='store_true',
dest='justList',
default=False,
help='Just list the found videos.',
required=False
)
parser.add_argument('-v',
action='store_true',
dest='verbose',
default=False,
help='Print detail info.',
required=False
)
parser.add_argument('--audio',
action='store_true',
dest='yesAudio',
default=False,
help='Find audio files',
required=False
)
parser.add_argument('--batch',
action='store_true',
dest='batchFile',
default=False,
help='Run using a batch of urls.',
required=False
)
parser.add_argument('URL',
type=str,
help='<Required> url link'
)
results = parser.parse_args()
url = results.URL.strip().rstrip('/') + '/'
textFile = results.textFile.strip()
yesDownload = results.yesDownload
yesAudio = results.yesAudio
justList = results.justList
batchFile = results.batchFile
verbose = results.verbose
return
def main():
getArguments()
if batchFile is True:
with open(url.rstrip('/'), "r") as f:
for content in f.readlines():
print('Finding files for ' + content.strip())
determinePage(content.strip())
print('\n')
else:
determinePage(url)
def streamonlypage(url):
parseurl = url.split('/')
game= parseurl[4]
gameparsed= game.split('_')
platform= gameparsed[0]
xmlurl = 'https://archive.org/download/'+game+'/'+game+'_files.xml'
findfile= requests.get(xmlurl, headers={'user-agent': 'archive-dl'}, stream=False)
if findfile.status_code != 200:
print('Unable to find file data')
return
findfile_soup= BeautifulSoup(findfile.text, 'xml')
findfile_file= findfile_soup.find('file', {'name': re.compile(r'3ds$|a78$|bin$|chd$|cso$|gba$|gb$|gbc$|iso$|64$|nes$|sfc$|wad$|wbfs$|zip$')})
if findfile_file == None:
print('No file available for download')
return
filename = (findfile_file.get('name'))
url = 'https://archive.org/download/'+game+'/'+filename
if yesDownload == True:
fileExists(filename, url)
elif justList == True:
print('Found ' + url)
else:
writeFile(url)
def userpage(url):
getpage=requests.get(url, headers={'user-agent': 'archive-dl'}, stream=True)
getpage_soup=BeautifulSoup(getpage.text, 'html.parser')
items= getpage_soup.findAll('div', {'class':'item-ttl C C2'})
for shows in items:
show= shows.find('a').get('href')
url= 'https://archive.org'+show+'/'
videopage(url)
def videopage(url):
parseurl = url.split('/')
folderurl = '/'.join(parseurl[4:]).rstrip('/')
url = 'https://archive.org/download/'+folderurl+'/'
if verbose is True:
print('Looking in ' + url)
downloadpage(url)
def writeFile(dl_url):
print('Writing ' + dl_url + ' to ' + textFile)
f = open(textFile, "a")
f.write(dl_url)
f.write("\n")
f.close()
if __name__ == "__main__":
main()