-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathun_data.py
executable file
·235 lines (205 loc) · 7.14 KB
/
un_data.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
# -*- coding: utf-8 -*-
import re
import sys
import os
import requests
import time
from bs4 import BeautifulSoup
import pandas as pd
def read_page(url):
"""
downloads the page, sleeps for 60 seconds if this fails
"""
try:
html = requests.get(url).content
except:
print "sleeping..."
time.sleep(60)
html = requests.get(url).content
return BeautifulSoup(html)
def read_table(url, table_tag):
"""
then parses the specified html table
"""
soup = read_page(url)
return soup.find(lambda tag: tag.name == "table" and
tag.has_attr("id") and
tag["id"] == table_tag)
def get_chap_list(table_tag, base_url):
"""
parses the list of all UN treaty chapters
"""
table = read_table(base_url + "ParticipationStatus.aspx", table_tag)
return [[td.get_text(strip=True)
if td.find("a") is None
else base_url + td.find("a")["href"]
for td in tr.find_all("td")]
for tr in table.find_all("tr")]
def get_treaty_list(table_tag, base_url, chap_list):
"""
gets list of all treaties and writes index file
"""
df = []
for chap in range(len(chap_list)):
table = read_table(chap_list[chap][1], table_tag)
for row in table.find_all("tr")[0:]:
col = row.find_all("td")
number = re.sub("\\.$", "", col[0].get_text(strip=True))
name = clean(re.sub(r"\xc2|\xa0", "", col[1].get_text(strip=True)))
url = base_url + col[1].find("a").get("href")
df.append([chap_list[chap][0], number, name, url,
chap_list[chap][1], chap_list[chap][2]])
cols = ["chapter_no", "treaty_no", "treaty_name", "url",
"chapter_url", "chapter_name"]
pd.DataFrame(df, columns=cols).to_csv("index.csv",
index=False, encoding="utf-8")
return df
def clean(s, head=False):
"""
removes cruft from data entry
"""
s = re.compile(r"<[^<]*?/?>|\t|\r|\n|\f|\[|\]").sub("", s)
s = re.sub(r"\s+|\xc2|\xa0", " ", s)
if head:
s = re.sub("\d|,", "", s)
return s.strip()
def int_to_roman(input):
"""
Convert an integer to Roman numerals.
Examples:
>>> int_to_roman(0)
Traceback (most recent call last):
ValueError: Argument must be between 1 and 3999
>>> int_to_roman(-1)
Traceback (most recent call last):
ValueError: Argument must be between 1 and 3999
>>> int_to_roman(1.5)
Traceback (most recent call last):
TypeError: expected integer, got <type 'float'>
>>> for i in range(1, 21): print int_to_roman(i)
...
I
II
III
IV
V
VI
VII
VIII
IX
X
XI
XII
XIII
XIV
XV
XVI
XVII
XVIII
XIX
XX
>>> print int_to_roman(2000)
MM
>>> print int_to_roman(1999)
MCMXCIX
"""
if type(input) != type(1):
raise TypeError, "expected integer, got %s" % type(input)
if not 0 < input < 4000:
raise ValueError, "Argument must be between 1 and 3999"
ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
nums = ("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I")
result = ""
for i in range(len(ints)):
count = int(input / ints[i])
result += nums[i] * count
input -= ints[i] * count
return result
def get_xml(chap, treaty):
"""
finds the XML link on a treaty page
"""
base_xml = "https://treaties.un.org/doc/Publication/MTDSG/Volume%20I/Chapter%20"
url = base_xml + int_to_roman(chap) + "/" + int_to_roman(chap) + "-" + str(treaty) + ".en.xml"
return read_page(url)
def get_normal_table(soup):
"""
fetches data from the typical UN treaty page
"""
table = soup.find(lambda tag: tag.name == "participants")
if table is None:
return None
return [[clean(td.get_text(strip=True))
if td.find("a") is None
else base_url + td.find("a")["href"]
for td in tr.find_all("entry")]
for tr in table.find_all("row")]
def get_special_table(soup):
"""
function that fetches special cases that the normal function fails at
"""
table = soup.find(lambda tag: tag.name == "specialtables")
if table is None:
return None
header = [[clean(td.get_text(strip=True), True)
for td in tr.find_all("title")]
for tr in table.find_all("tableheader")]
data = [[clean(td.get_text(strip=True))
if td.find("a") is None
else base_url + td.find("a")["href"]
for td in tr.find_all("column")]
for tr in table.find_all("row")]
return header + data
def get_declarations(soup, treaty_id):
"""
fetches any reservations that countries register for a particular treaty
"""
table = soup.find(lambda tag: tag.name == "declarations")
if table is not None:
for row in table.find_all("declaration"):
name = clean(row.find("participant").get_text(strip=True), True)
text = row.find_all(lambda tag: tag.name == "text" and
tag.has_attr("type") and
tag["type"] == "para")
with open("declarations/" + treaty_id + ".txt", "a+") as f:
f.writelines("#" + name.encode("utf-8") + "\n")
for entry in text:
entry = clean(entry.get_text(strip=True))
f.writelines(entry.encode("utf-8"))
f.writelines("\n\n")
f.close()
def get_treaties(base_url, treaty_list):
"""
fetches all treaty data and writes it to file
"""
for treaty in range(len(treaty_list)):
soup = get_xml(int(treaty_list[treaty][0]), re.sub("\.", "-", treaty_list[treaty][1]))
df = get_normal_table(soup)
if df is None:
df = get_special_table(soup)
treaty_id = str(treaty_list[treaty][0]) + "-" + str(treaty_list[treaty][1])
treaty_id = re.sub("\.", "-", treaty_id)
if not os.path.exists("declarations"):
os.makedirs("declarations")
if os.path.exists("declarations/" + treaty_id + ".txt"):
os.remove("declarations/" + treaty_id + ".txt")
get_declarations(soup, treaty_id)
if not os.path.exists("treaties"):
os.makedirs("treaties")
df = pd.DataFrame(df)
if not df.empty and len(df.index) > 1:
df.ix[:, 0] = df.ix[:, 0].map(lambda x: re.sub("\d|,", "", x))
df.to_csv("treaties/" + treaty_id + ".csv", header=False,
index=False, encoding="utf-8")
sys.stdout.write(str(treaty) + " of " +
str(len(treaty_list)) + " complete\r")
sys.stdout.flush()
base_url = "http://treaties.un.org/pages/"
chap_list_table_tag = "ctl00_ContentPlaceHolder1_dgChapterList"
chap_table_tag = "ctl00_ContentPlaceHolder1_dgSubChapterList"
if __name__ == "__main__":
chap_list = get_chap_list(chap_list_table_tag, base_url)
print("chapter list successfully fetched")
treaty_list = get_treaty_list(chap_table_tag, base_url, chap_list)
print("treaty list successfully fetched")
get_treaties(base_url, treaty_list)