-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcarchivescraper.py
64 lines (54 loc) · 1.9 KB
/
mcarchivescraper.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
import os
import requests
import bs4
import io
import urllib3
def match_class(target):
def do_match(tag):
classes = tag.get('class', [])
return all(c in classes for c in target)
return do_match
site = "https://mcarchive.net"
mods = []
parentDir = "./mcarchivescraper_data"
try:
os.mkdir(parentDir)
except:
pass
os.chdir(parentDir)
first = True
with requests.get(site + "/mods") as response:
siteData = response.text
siteDict = bs4.BeautifulSoup(siteData, "html.parser")
for mod in siteDict.findAll(match_class(["block"])):
if first:
first = False
else:
mod = mod.find("a").get("href")
mods.append(mod)
downloaded = 0
for mod in mods:
with requests.get(site + mod) as response:
page = bs4.BeautifulSoup(response.text, "html.parser")
print("Parsing " + site + mod)
for link in page.findAll(match_class(["btn"])):
if link.contents[0].strip() == "IPFS Download":
try:
name = os.path.basename(link.get("href"))
if os.path.exists(name):
print("\"" + name + "\" exists. Skipping.")
else:
print("Trying to download \"" + name + "\"")
with requests.get(link.get("href"), stream=True, timeout=20) as response:
with io.open(name, 'wb') as fd:
oldDone = 0
for chunk in response.iter_content(chunk_size=4096):
fd.write(chunk)
downloaded += 1
except Exception as e:
print(e)
try:
os.unlink(name)
except:
pass
print("Downloaded " + str(downloaded) + (" mod." if downloaded == 1 else "mods."))