-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_files_from_pgnmentor.py
76 lines (63 loc) · 2.08 KB
/
fetch_files_from_pgnmentor.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
from bs4 import BeautifulSoup
import urllib.request
import requests
import os
import sys
# Main menu
print('\nSelect download target:')
print(' 1) By player (ZIP with PGN file inside)')
print(' 2) By opening type (ZIP with PGN file inside)')
print(' 3) By event (PGN)')
choice = input(' > ')
if (choice == '1'): DOWNLOAD_TARGET = 'players'
elif (choice == '2'): DOWNLOAD_TARGET = 'openings'
elif (choice == '3'): DOWNLOAD_TARGET = 'events'
else: exit('Wrong choice')
print('')
# Fetch urls
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'}
page = requests.get('https://www.pgnmentor.com/files.html', headers = headers)
soup = BeautifulSoup(page.content, 'html.parser')
links = soup.findAll('a', href=True)
# Clean urls
urls = []
for link in links:
if (
link['href'].startswith(DOWNLOAD_TARGET)
and (
# 'players' and 'openings' files are in zip format
link['href'].endswith('zip')
or
# 'events' files are in pgn format
link['href'].endswith('pgn')
)
):
url = 'https://www.pgnmentor.com/' + link['href']
# The same url usually appears twice in the web page
if url not in urls:
urls.append(url)
# Download files
if not os.path.exists('pgn'):
os.makedirs('pgn')
for index, url in enumerate(urls):
filename = url.split('/')[-1]
if not os.path.isfile(filename):
try:
print(
'Downloading file {index:d} of {total:d}: {filename}'
.format(
index = index + 1,
total = len(urls),
filename = filename
)
)
urllib.request.urlretrieve(url, 'pgn/' + filename)
except:
print(
'Unable to download file {index:d}: {filename}'
.format(
index = index + 1,
total = len(urls),
filename = filename
)
)