forked from chrisbutner/ChessCoach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
312 lines (265 loc) · 10.1 KB
/
scrape.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
# ChessCoach, a neural network-based chess engine capable of natural-language commentary
# Copyright 2021 Chris Butner
#
# ChessCoach 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.
#
# ChessCoach 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 ChessCoach. If not, see <https://www.gnu.org/licenses/>.
import requests
from bs4 import BeautifulSoup
import base64
import os
import unicodedata
import time
import re
# --- Common ---
api_key = os.environ["SCRAPINGBEE_API_KEY"]
endpoint = "https://app.scrapingbee.com/api/v1"
sleep_seconds = 5
html_cache = "html_cache"
pgn_cache = "pgn_cache"
pgn_header = """[Event ""]
[Site ""]
[Date ""]
[Round ""]
[White ""]
[Black ""]
[Result "?"]
"""
# If you'd like to use something other than ScrapingBee, just replace "do_scrape()" with your own implementation,
# including appropriate ad blocking, rate limiting and error handling.
def do_scrape(url, render_js):
params = dict(api_key=api_key, url=url, render_js=render_js, block_ads="true")
while True:
print("ScrapingBee:", url)
response = requests.get(endpoint, params=params)
content = response.content.decode("utf-8")
if response.status_code == 200:
return content
elif response.status_code == 500:
print(f"Retrying {url} after {sleep_seconds} seconds ({response.status_code}): {content}")
time.sleep(sleep_seconds)
else:
raise Exception(f"Failed to scrape {url} ({response.status_code}): {content}")
def scrape(url, render_js=False, cache=True):
if cache:
cache_path = get_cache_path(html_cache, url) + ".html"
try:
with open(cache_path, "r", encoding="utf-8") as f:
content = f.read()
except:
content = do_scrape(url, render_js)
write_file(cache_path, content)
else:
content = do_scrape(url, render_js)
return content
def normalize(s):
return unicodedata.normalize("NFKC", s)
def get_cache_path(directory, url):
path = os.path.join(directory, base64.b32encode(url.encode("utf-8")).decode("utf-8"))
path = os.path.abspath(path)
path = path[:240]
return path
def write_file(path, content):
temporary_path = path + ".part"
with open(temporary_path, "w", encoding="utf-8") as f:
f.write(content)
os.rename(temporary_path, path)
# --- Site1 ---
class Site1:
url_base = "https://example.com/"
url_root = "https://example.com/list_annotated.pl?u=all&c=0&sb=0&rm=0&rn=0&rx=9999&sr=0&p=0"
def find_next_url(self, soup):
next_url = soup.select("table.paginator a[title='next page']")
return requests.compat.urljoin(self.url_base, next_url[0]["href"]) if next_url else None
def scrape_parse_game_segment(self, url):
content = scrape(url)
soup = BeautifulSoup(content, 'html.parser')
pgn = ""
# Parse this page and produce a .pgn segment (need tbody if JS-rendered in cache).
cells = soup.select("table.dialog > tr > td") or soup.select("table.dialog > tbody > tr > td")
expect_moves = True
for cell in cells:
if not "vertical-align: top" in cell.get("style", ""):
continue
text = cell.get_text().strip()
if expect_moves:
pgn += normalize(text.splitlines()[0]) + "\n"
elif text:
pgn += "{ " + normalize(text) + " }\n"
expect_moves = not expect_moves
# Check for a next page.
next_url = self.find_next_url(soup)
return pgn, next_url
def scrape_parse_list(self, url):
content = scrape(url)
soup = BeautifulSoup(content, 'html.parser')
games = []
# Parse this page and produce a list of game URLs.
links = soup.select("a")
for link in links:
if link.string and "Comments" in link.string and "gm=" in link.get("href", ""):
games.append(requests.compat.urljoin(self.url_base, link["href"]))
# Check for a next page.
next_url = self.find_next_url(soup)
return games, next_url
def scrape_parse_game(self, url):
print("Game:", url)
cache_path = get_cache_path(pgn_cache, url) + ".pgn"
try:
with open(cache_path, "r", encoding="utf-8") as f:
pgn = f.read()
except:
next_url = url
pgn = pgn_header
while next_url:
pgn_segment, next_url = self.scrape_parse_game_segment(next_url)
pgn += pgn_segment
write_file(cache_path, pgn)
return pgn
def scrape_parse_all_games(self):
next_url = self.url_root
while next_url:
games, next_url = self.scrape_parse_list(next_url)
for game_url in games:
self.scrape_parse_game(game_url)
# --- Site2 ---
class Site2:
url_base = "https://www.example.com/"
url_root = "https://www.example.com/perl/chess.pl?annotated=1"
def find_next_url(self, soup):
next_url = soup.select("img[src='/chessimages/next.gif']")
return requests.compat.urljoin(self.url_base, next_url[0].parent["href"]) if next_url else None
def scrape_parse_list(self, url):
content = scrape(url)
soup = BeautifulSoup(content, 'html.parser')
games = []
# Parse this page and produce a list of game URLs.
links = soup.select("a")
for link in links:
href = link.get("href", "")
if "gid=" in href and not "comp=1" in href:
game_id = int(re.match(".*gid=([0-9]+).*", href).group(1))
games.append(requests.compat.urljoin(self.url_base, f"/perl/nph-chesspgn?text=1&gid={game_id}"))
# Check for a next page.
next_url = self.find_next_url(soup)
return games, next_url
def scrape_parse_game(self, url):
print("Game:", url)
cache_path = get_cache_path(pgn_cache, url) + ".pgn"
try:
with open(cache_path, "r", encoding="utf-8") as f:
pgn = f.read()
except:
content = scrape(url)
soup = BeautifulSoup(content, 'html.parser')
pgn = soup.get_text().strip()
write_file(cache_path, pgn)
return pgn
def scrape_parse_all_games(self):
next_url = self.url_root
while next_url:
games, next_url = self.scrape_parse_list(next_url)
for game_url in games:
self.scrape_parse_game(game_url)
# --- Site3 ---
class Site3:
url_base = "https://www.example.com/"
url_root = "https://www.example.com/news"
def has_comments(self, pgn):
# Ignore games with almost no comments, or with only clock times.
return (pgn.count("{") - len(re.findall("{\s*\\[", pgn))) >= 3
def find_next_url(self, soup):
next_url = soup.select("a.pagination-next")
return requests.compat.urljoin(self.url_base, next_url[0]["href"]) if next_url else None
def scrape_parse_list(self, url):
# Can't cache news, it's most-recent-first.
content = scrape(url, cache=False)
soup = BeautifulSoup(content, 'html.parser')
# Parse this page and produce a list of article URLs, each of which may contain multiple games.
articles = [requests.compat.urljoin(self.url_base, link["href"]) for link in soup.select("a.post-preview-title")]
# Check for a next page.
next_url = self.find_next_url(soup)
return articles, next_url
def scrape_parse_game(self, synthetic_url, pgn):
print("Game:", synthetic_url)
cache_path = get_cache_path(pgn_cache, synthetic_url) + ".pgn"
try:
with open(cache_path, "r", encoding="utf-8"):
pass
except:
write_file(cache_path, pgn)
return pgn
def scrape_parse_article(self, url):
print("Article:", url)
content = scrape(url)
soup = BeautifulSoup(content, 'html.parser')
diagrams = soup.select(".chessDiagramDiv")
saved_headers = set()
game_number = 0
for diagram in diagrams:
if not diagram.string:
print("Broken diagram:", url)
continue
games = re.findall("(\\[Event.*?)(\\&-|$)", diagram.string, re.DOTALL)
for game in games:
game_number += 1
synthetic_url = f"{url}#game{game_number}"
pgn = game[0]
if not self.has_comments(pgn):
continue
headers = re.search("\\[.*\"\\]", pgn, re.DOTALL).group(0)
if headers in saved_headers:
print("Duplicate:", synthetic_url)
continue
saved_headers.add(headers)
self.scrape_parse_game(synthetic_url, pgn)
def scrape_parse_all_games(self):
next_url = self.url_root
while next_url:
articles, next_url = self.scrape_parse_list(next_url)
for article_url in articles:
self.scrape_parse_article(article_url)
# --- Utilities ---
# Really rough, final split is shorter. Intended to operate over one-game-per (or equal-per) pgn files.
def combine_split(dir_in, dir_out, split_count):
os.makedirs(dir_out, exist_ok=True)
count_in = len(os.listdir(dir_in))
per_split = (count_in + split_count - 1) // split_count
count_split = 0
count_out = 0
content_out = ""
for filename_in in os.listdir(dir_in):
path_in = os.path.join(dir_in, filename_in)
with open(path_in, "r", encoding="utf-8") as f:
content_in = f.read()
content_out += content_in + ("\n" if content_in[-1] == "\n" else "\n\n")
count_split += 1
if count_split >= per_split or count_split >= count_in:
count_in -= count_split
count_split = 0
count_out += 1
path_out = os.path.join(dir_out, f"split{count_out}.pgn")
content_out = content_out[:-1]
with open(path_out, "w", encoding="utf-8") as f:
f.write(content_out)
content_out = ""
# --- Run ---
print("Working directory:", os.getcwd())
os.makedirs(html_cache, exist_ok=True)
os.makedirs(pgn_cache, exist_ok=True)
#Site1().scrape_parse_all_games()
#Site2().scrape_parse_all_games()
#Site3().scrape_parse_all_games()
# --- Usage ---
# - Make sure that the working directory is short to avoid any collisions due to MAX_PATH issues with Base32 filenames.
# - Run "combine_split" before copying the resulting PGNs to longer directories to again avoid any MAX_PATH issues.
# - Use a "split_count" that is a multiple of 8 to allow ChessCoachPgnToGames to thread efficiently for large datasets.