-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalmanax-scraper.py
227 lines (182 loc) · 7.9 KB
/
almanax-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
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
"""
Almanax Scraper: Multilingual scraping of the Dofus Almanax.
Copyright (C) 2022 Christopher Sieh
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
import json
import argparse
import requests
import os
import sys
almanax_api_url="http://localhost:8080/dofus2/almanax"
base_url = "http://www.krosmoz.com"
date_format = '%Y-%m-%d'
almanax_start_date = datetime.strptime("2012-09-18", date_format)
iterate_link = ""
daily_json_file = "almanax-data.json"
client_secret = "secret"
_almanax = dict()
# https://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory
def get_script_path():
return os.path.dirname(os.path.realpath(sys.argv[0]))
daily_json_file_abs = get_script_path() + "/" + daily_json_file
def addLangArrIfNotExist(obj, lang):
if lang not in obj:
obj[lang] = []
# help looping over a range between two dates
def date_range(start, end):
for n in range(int((end - start).days)+1):
yield start + timedelta(n)
def scrape_all_langs(start_date=None, end_date=None, out_file_include_range=False):
start = datetime.strptime(start_date, date_format) if start_date else almanax_start_date
end = datetime.strptime(end_date, date_format) if end_date else datetime.today()
for curr_date in date_range(start, end):
curr_date = curr_date.strftime(date_format)
print("scraping " + curr_date)
try:
scrape(curr_date, "en")
except:
print(f"day {curr_date} failed")
continue
scrape(curr_date, "fr")
scrape(curr_date, "de")
scrape(curr_date, "it")
scrape(curr_date, "es")
out_file = f"almanax-data-{start.strftime(date_format)}_{end.strftime(date_format)}.json" if out_file_include_range else daily_json_file
with open(get_script_path() + "/" + out_file, 'w') as f:
json.dump(_almanax, f, indent=4, ensure_ascii=False)
def scrape(date, lang):
iterate_link = base_url + "/" + lang + "/almanax/" + date
take_begin = ""
take_end = ""
bonus_take = ""
if lang == "fr":
take_begin = "Récupérer "
take_end = " et rapporter"
bonus_take = "Bonus : "
if lang == "en":
take_begin = "Find "
take_end = " and take"
bonus_take = "Bonus: "
if lang == "de":
take_begin = "Sich "
take_end = " beschaffen"
bonus_take = "Bonus: "
if lang == "it":
take_begin = "Ottieni "
take_end = " e porta"
bonus_take = "Bonus: "
if lang == "es":
take_begin = "Recolectar "
take_end = " y llevárselo"
bonus_take = "Bonus: "
html = requests.get(iterate_link).content
soup = BeautifulSoup(html, 'html.parser')
dofus_container = soup.find(id="achievement_dofus")
mid_container = dofus_container.find("div", {"class": "more"})
bonus_type = str(mid_container.previousSibling).strip()[len(bonus_take):] # take only in english
offering = mid_container.find("p", {"class": "fleft"}).text
offering = offering.strip()
index_start = len(take_begin)
index_stop = offering.index(take_end)
offering = offering[index_start:index_stop]
offering_count = 0
for s in offering.split():
if s.isdigit():
offering_count = int(s)
break
offering = offering.replace(str(offering_count), '').strip()
pic = mid_container.img['src']
bonus = str(mid_container)
bonus = bonus[len('<div class="more">'):bonus.index('<div class="more-infos">')].strip()
bonus = bonus.replace('<b>', '').replace('</b>', '')
bonus = bonus.replace('<i>', '').replace('</i>', '')
bonus = bonus.replace('<u>', '').replace('</u>', '')
data = {
"date": date,
"item_quantity": offering_count,
"item": offering,
"description": bonus.replace('\n', " ").replace("\r\n", " "),
"bonus": bonus_type.replace('\n', " ").replace("\r\n", " "),
"language": lang,
"item_picture_url": pic
}
addLangArrIfNotExist(_almanax, lang)
_almanax[lang].append(data)
def json_to_api(path):
with open(path, 'r') as f:
data = json.load(f)
# insert all new entries in english
for offering in data["en"]:
r = requests.post(almanax_api_url, json=offering, headers={"Authorization": f"Bearer {client_secret}"}) # created 201 when all ok
if r.status_code > 214 and r.status_code != 406:
print(offering)
print(r.status_code)
print(r.json().get("errors"))
exit(r.status_code)
if r.status_code == 406:
# exists but can be outdated
# 200 == no update, 201 == created new one, expecting translations for that now
p = requests.put(almanax_api_url, json=offering, headers={"Authorization": f"Bearer {client_secret}"})
if p.status_code == 201:
print("just updated an offering")
# add the translations
for lang, offerings in data.items():
if lang == "en":
continue
for offering in offerings:
r = requests.put(f"{almanax_api_url}/translate", json=offering, headers={"Authorization": f"Bearer {client_secret}"})
if r.status_code > 214:
print(offering)
print(r.status_code)
print(r.json().get("errors"))
exit(r.status_code)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="scrape dofus almanax")
group = parser.add_mutually_exclusive_group()
group.add_argument("--scrape", action="store_true", help="scrapes a range with start and end params, else from beginning of almanax time till today")
group.add_argument("--api", action="store_true", help="sends the generated data to the api")
group.add_argument("--daily", action="store_true", help="scrapes next month and sends to api")
group.add_argument("--init", action="store_true", help="loads old almanax data if it exists and posts them to the api")
parser.add_argument("--start", help="start date", type=str)
parser.add_argument("--end", help="end date", type=str)
args = parser.parse_args()
if args.init:
dirs = os.listdir(get_script_path())
for filename in dirs:
if not filename.endswith(".json") or not filename.startswith("almanax-data-"):
continue
file_cleaned = filename.replace("almanax-data-", "").replace(".json", "")
file_date_range = filename.split("_")
if len(file_date_range) == 2:
with open(get_script_path() + "/" + filename, 'r') as f:
data = json.load(f)
json_to_api(filename)
if os.path.exists(daily_json_file_abs):
json_to_api(daily_json_file_abs)
if args.daily:
try:
os.remove(daily_json_file_abs)
except:
pass
date_start = (datetime.today() - timedelta(days=7)).strftime(date_format)
date_start_f = datetime.strptime(date_start, date_format)
date_in_a_month = (date_start_f + timedelta(days=38)).strftime(date_format)
scrape_all_langs(date_start, date_in_a_month)
json_to_api(daily_json_file_abs)
if args.scrape:
scrape_all_langs(args.start, args.end, True)
if args.api:
if os.path.exists(daily_json_file_abs):
json_to_api(daily_json_file_abs)