insert_country.py 2.92 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/env python3

"""
Script to insert the country data in the database

IT HAS TO BE RUN INSIDE ./manage.py shell
"""
import os
Florent Chehab's avatar
Florent Chehab committed
9
import argparse
Florent Chehab's avatar
Florent Chehab committed
10
import pandas as pd
Florent Chehab's avatar
Florent Chehab committed
11
import requests
12

Florent Chehab's avatar
Florent Chehab committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
if __name__ != "__main__":
    print(__name__)
    raise Exception("Script has to be run directly")

# getting args from command line
parser = argparse.ArgumentParser(
    description='Country and Region Insertion')

parser.add_argument('--address', '-a', dest='address',
                    action='store', default='http://127.0.0.1:8000/api',
                    help='Adresse of the web REST API')

parser.add_argument('--token', '-t', dest='token',
                    action='store', default='NO_TOKEN_PROVIDED',
                    help='Permanent token to use')

args = parser.parse_args()

api_address = str(args.address)
api_token = str(args.token)
33

Florent Chehab's avatar
Florent Chehab committed
34
tmp = os.path.join(os.path.realpath(__file__), '../../assets/country.csv')
35
country_file_loc = os.path.abspath(tmp)
36
37
38
tmp = os.path.join(os.path.realpath(__file__),
                   '../../assets/alpha-conv-table.csv')
conv_alpha_file_loc = os.path.abspath(tmp)
39

Florent Chehab's avatar
Florent Chehab committed
40
41
42
43
44
45
46
47
data = pd.read_csv(country_file_loc, sep=',', header=0,
                   dtype=object).fillna('')
data = data.drop(["Least Developed Countries (LDC)",
                  "Land Locked Developing Countries (LLDC)",
                  "Small Island Developing States (SIDS)",
                  "Developed / Developing Countries"],
                 axis=1)

48
49
50
51
52
53
54
55
# Need to load the information for converting
# Countries alpha-3 code to alpha-2 code
data_conv = pd.read_csv(conv_alpha_file_loc, sep=',',
                        header=0, na_filter=False)
conv_alpha_code = {}
for index, row in data_conv.iterrows():
    conv_alpha_code[row["alpha-3"]] = row["alpha-2"]

Florent Chehab's avatar
Florent Chehab committed
56
57
58
59
60
61
62
63
64
65
66
67

def make_post(address, data):
    h = {'Authorization': 'Token ' + api_token}
    r = requests.post(address, headers=h, data=data)
    if r.status_code is 403:
        raise Exception("Authentification failed")
    if not r.ok:
        print(r.content)
        print(data)
    return r.ok


68
def save_country(attributes):
Florent Chehab's avatar
Florent Chehab committed
69
    a = api_address + "/country/"
70
    return make_post(a, attributes)
Florent Chehab's avatar
Florent Chehab committed
71
72


73
74
75
76
file_attr = ["Region Code", "Region Name",
             "Sub-region Code", "Sub-region Name",
             "Intermediate Region Code", "Intermediate Region Name",
             "ISO-alpha3 Code", "Country or Area"]
Florent Chehab's avatar
Florent Chehab committed
77

78
79
80
81
82
db_attr = [
    'region_un_code', 'region_name',
    'sub_region_un_code', 'sub_region_name',
    'intermediate_region_un_code', 'intermediate_region_name',
    'iso_alpha3_code', 'name']
Florent Chehab's avatar
Florent Chehab committed
83

Florent Chehab's avatar
Florent Chehab committed
84
85

for index, row in data.iterrows():
86
87
88
89
90
91
92
93
94
    country_attributes = {}

    for f_a, db_a in zip(file_attr, db_attr):
        country_attributes[db_a] = str(row[f_a])

    tmp_c = str(row['ISO-alpha3 Code'])
    if tmp_c in conv_alpha_code.keys():
        country_attributes['iso_alpha2_code'] = conv_alpha_code[tmp_c]
        save_country(country_attributes)
95
    else:
96
        print("ignoring country :", country_attributes)