scampion
commited on
Commit
·
43404e5
1
Parent(s):
9e6eb59
update
Browse files
mep.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import tempfile
|
3 |
+
import zipfile
|
4 |
+
|
5 |
+
import requests
|
6 |
+
import xml.etree.ElementTree as ET
|
7 |
+
import csv
|
8 |
+
import json
|
9 |
+
import os
|
10 |
+
|
11 |
+
|
12 |
+
def get_mep_gender():
|
13 |
+
url = "https://www.tttp.eu/data/meps.csv"
|
14 |
+
r = requests.get(url)
|
15 |
+
if r.status_code == 200:
|
16 |
+
csv_reader = csv.reader(r.content.decode('utf-8').splitlines(), delimiter=',')
|
17 |
+
genders = {}
|
18 |
+
for row in csv_reader:
|
19 |
+
genders[row[0]] = row[6], row[2] + row[3]
|
20 |
+
return genders
|
21 |
+
|
22 |
+
|
23 |
+
def get_mep_data_from_web():
|
24 |
+
alphabet = 'abcdefghijklmnopqrstuvwxyz'
|
25 |
+
url = "https://www.europarl.europa.eu/meps/en/full-list/xml/"
|
26 |
+
data = {}
|
27 |
+
genders = get_mep_gender()
|
28 |
+
for letter in alphabet:
|
29 |
+
letter_url = url + letter
|
30 |
+
r = requests.get(letter_url)
|
31 |
+
if r.status_code != 200:
|
32 |
+
continue
|
33 |
+
content = r.content
|
34 |
+
tree = ET.fromstring(content)
|
35 |
+
if not tree:
|
36 |
+
continue
|
37 |
+
for mep in tree.findall('mep'):
|
38 |
+
id = mep.find('id').text
|
39 |
+
name = mep.find('fullName').text
|
40 |
+
country = mep.find('country').text
|
41 |
+
# group-ep8
|
42 |
+
group = mep.find('politicalGroup').text
|
43 |
+
print(id, name, country)
|
44 |
+
try:
|
45 |
+
gender, _ = genders.get(id, ("X", "Not found"))
|
46 |
+
print(id, name, country, gender)
|
47 |
+
data[id] = {"name": name, "nationality": country, "group-ep9": group, "gender": gender}
|
48 |
+
except Exception as e:
|
49 |
+
print("🌩️ Error " + str(e))
|
50 |
+
continue
|
51 |
+
|
52 |
+
outgoing = "https://www.europarl.europa.eu/meps/en/incoming-outgoing/outgoing/xml"
|
53 |
+
r = requests.get(outgoing)
|
54 |
+
content = r.content
|
55 |
+
tree = ET.fromstring(content)
|
56 |
+
for mep in tree.findall('mep'):
|
57 |
+
id = mep.find('id').text
|
58 |
+
name = mep.find('fullName').text
|
59 |
+
country = mep.find('country').text
|
60 |
+
# group-ep8
|
61 |
+
group = mep.find('politicalGroup').text
|
62 |
+
print("Outgoing", id, name, country)
|
63 |
+
try:
|
64 |
+
gender, _ = genders.get(id, ("X", "Not found"))
|
65 |
+
print(id, name, country, gender)
|
66 |
+
data[id] = {"name": name, "nationality": country, "group-ep9": group, "gender": gender}
|
67 |
+
except Exception as e:
|
68 |
+
print("🌩️ Error " + str(e))
|
69 |
+
continue
|
70 |
+
|
71 |
+
return data
|
72 |
+
|
73 |
+
|
74 |
+
def get_previous_mep_data():
|
75 |
+
url = "https://zenodo.org/record/4709248/files/helpers.zip?download=1"
|
76 |
+
r = requests.get(url)
|
77 |
+
if r.status_code == 200:
|
78 |
+
iob = io.BytesIO(r.content)
|
79 |
+
with zipfile.ZipFile(iob, 'r') as zip_ref, tempfile.TemporaryDirectory() as tmpdir:
|
80 |
+
zip_ref.extract('helpers/meps.json', tmpdir)
|
81 |
+
with open(os.path.join(tmpdir, 'helpers/meps.json'), 'r') as f:
|
82 |
+
return json.load(f)
|
83 |
+
|
84 |
+
|
85 |
+
def get_mep_data():
|
86 |
+
user_dir = os.path.expanduser('~')
|
87 |
+
data_dir = os.path.join(user_dir, '.amendements2json')
|
88 |
+
if not os.path.exists(data_dir):
|
89 |
+
os.makedirs(data_dir)
|
90 |
+
data_file = os.path.join(data_dir, 'meps.json')
|
91 |
+
if os.path.exists(data_file):
|
92 |
+
with open(data_file, 'r') as f:
|
93 |
+
data = json.load(f)
|
94 |
+
else:
|
95 |
+
data = get_mep_data_from_web()
|
96 |
+
for k, v in get_previous_mep_data().items():
|
97 |
+
if k not in data.keys():
|
98 |
+
data[k] = v
|
99 |
+
else:
|
100 |
+
data[k] = {**data[k], **v}
|
101 |
+
with open(data_file, 'w') as f:
|
102 |
+
json.dump(data, f, indent=2)
|
103 |
+
return data
|
104 |
+
|
105 |
+
|
106 |
+
if __name__ == '__main__':
|
107 |
+
data = get_mep_data()
|
108 |
+
json.dumps(data, indent=2)
|
109 |
+
|
meps.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|