-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
71 lines (59 loc) · 1.89 KB
/
scraper.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
import requests
import json
import os, os.path
import errno
URL_DOMAIN = "https://31minutos.fandom.com"
ARTICLE_ENDPOINT = "/api.php?action=parse&pageid={}&prop=wikitext&format=json&utf8=1"
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
def safe_open_w(path):
''' Open "path" for writing, creating any parent directories as needed.
'''
mkdir_p(os.path.dirname(path))
return open(path, 'w')
class Article():
def __init__(self, id):
self.id = id
self._title = None
self._wikitext = None
def retrieve(self):
response = requests.get(
url=URL_DOMAIN+ARTICLE_ENDPOINT.format(self.id)
).json()
return response
def fill(self):
response = self.retrieve()
self._title = response["parse"]["title"]
self._wikitext = response["parse"]["wikitext"]["*"]
@property
def title(self):
if self._title is None:
self.fill()
return self._title
@property
def wikitext(self):
if self._wikitext is None:
self.fill()
return self._wikitext
def write(self,folder="Articles"):
filename = os.path.join(folder, self.title+".txt")
with safe_open_w(filename) as file:
file.write(self.wikitext)
def get_articles(limit=1000):
response = requests.get(
url=URL_DOMAIN+"/api/v1/Articles/List?limit={}".format(limit)
).json()
return response
def download_articles():
articles = get_articles()["items"]
for article in articles:
article_id = article["id"]
article_title = article["title"]
print("Writing article {} with id {}".format(article_title, article_id))
new_article = Article(article_id)
new_article.write()