-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrq2-download.py
144 lines (135 loc) · 3.41 KB
/
rq2-download.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
import requests
import time
import pandas as pd
from random import randint
from retrying import retry
def retry_if_connection_error(exception):
return isinstance(exception, JSONDecodeError)
# if exception retry with a 600-seconds plus wait
@retry(retry_on_exception=retry_if_connection_error, wait_random_min=600000, wait_random_max=610000)
def safe_request(url):
return requests.get(url)
if __name__ == '__main__':
langs = ["Albanian",
"Amharic",
"Arabic",
"Armenian",
"Asue Awyu",
"Austronesian (Other)",
"Basque",
"Belarussian",
"Bengali",
"Bosnian",
"Bulgarian",
"Burmese",
"Cebuano",
"Central Huasteca Nahuatl",
"Chechen",
"Cherokee",
"Chinese",
"Choctaw",
"Cree",
"Creek",
"Creoles and Pidgins",
"Creoles and Pidgins, French-based (Other)",
"Croatian",
"Croatian",
"Czech",
"Dakota",
"Danish",
"Dutch",
"English",
"Esperanto",
"Estonian",
"Finnish",
"French",
"German",
"Germanic",
"Greek",
"Gujarati",
"Hawaiian",
"Hebrew",
"Hindi",
"Hmong",
"Hungarian",
"Icelandic",
"Iloko",
"Indonesian",
"Inupiaq",
"Iranian",
"Irish",
"Italian",
"Japanese",
"Khmer",
"Korean",
"Kurdish",
"Ladino",
"Latvian",
"Lithuanian",
"Macedonian",
"Malayalam",
"Marathi",
"Mohawk",
"Multiple",
"Navajo",
"Negidal",
"No Linguistic Content",
"Northwest Alaska Inupiatun",
"Norwegian",
"Norwegian",
"Panjabi",
"Persian",
"Polish",
"Portuguese",
"Romanian",
"Russian",
"Samaritan Aramaic",
"Samoan",
"Serbian",
"Serbian",
"Slovak",
"Slovenian",
"Sorbian",
"Spanish",
"Swedish",
"Syriac",
"Tagalog",
"Tajik",
"Tamil",
"Thai",
"Turkish",
"Ukrainian",
"Urdu",
"Uzbek",
"Vietnamese",
"Welsh",
"Yiddish",
"Yupik"]
years = list(range(1690, 2023+1, 10))
data_dict={}
for y in years[years.index(1690):]:
print(y, y+10)
for lang in langs[langs.index("Albanian"):]:
time.sleep(randint(10,15))
url = r"https://chroniclingamerica.loc.gov/search/titles/results/?year1={0}&year2={1}&language={2}&format=json".format(y, y+10, lang)
print(y, lang)
rsp = safe_request(url)
if rsp.status_code == 429:
wait_time = int(rsp.headers["Retry-After"])
time.sleep(wait_time)
print("sleeping for:", wait_time)
n_titles = safe_request(url).json()['totalItems']
title_dict = {lang: n_titles}
if y not in data_dict.keys():
data_dict.update({y: title_dict})
else:
data_dict[y].update(title_dict)
else:
n_titles = safe_request(url).json()['totalItems']
title_dict = {lang: n_titles}
if y not in data_dict.keys():
data_dict.update({y: title_dict})
else:
data_dict[y].update(title_dict)
lang_asrow = pd.DataFrame.from_dict(data_dict, orient='index').T
lang_asrow.to_csv("data/data_lang_asrow_test.csv", encoding="utf-8")