-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpull_templates.py
More file actions
61 lines (42 loc) · 1.74 KB
/
Copy pathpull_templates.py
File metadata and controls
61 lines (42 loc) · 1.74 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
NB= Reduce time in line 28 if necessary, but no less than 1s between requests.
@author: lenakmeth
"""
import os
import requests
import random
import time
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
def template_download(templates, directory):
""" Downloads templats from the english wiktionary.
If using differnet language, update urls.
If IP address blocked, use higher values in pause_between_request. """
urls = ["https://en.wiktionary.org/wiki/" + t for t in templates]
for u in urls:
title = u[40:]
pause_between_request = random.randint(5,10)
time.sleep(pause_between_request)
print('Downloading ' + title + "...")
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
r = session.get(u)
print('Request successful!')
with open(directory + '/' + title.replace("/", "&") + ".txt", 'w', encoding='utf-8') as f:
f.write(r.text)
print('Saved as ' + title.replace("/", "&") + ".txt")
print()
def find_templates(templates_folder, templates_names):
""" Finds which templates need to be downloaded, in case there are none
downloaded or there are new ones online. """
templates = []
downloaded = [i[:-4] for i in os.listdir(templates_folder) if i.endswith(".txt")]
for t in templates_names:
if t[9:] not in downloaded:
templates.append(t)
return templates