-
Notifications
You must be signed in to change notification settings - Fork 18
/
parse_various_csv.py
executable file
·170 lines (149 loc) · 6.62 KB
/
parse_various_csv.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# /**
# * Software Name : MCC_MNC
# * Version : 0.1
# *
# * Copyright 2020. Benoit Michau. P1 Security.
# *
# * This program is free software: you can redistribute it and/or modify
# * it under the terms of the GNU Affero General Public License as
# * published by the Free Software Foundation, either version 3 of the
# * License, or (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU Affero General Public License for more details.
# *
# * You should have received a copy of the GNU Affero General Public License
# * along with this program. If not, see <https://www.gnu.org/licenses/>.
# *
# *--------------------------------------------------------
# * File Name : parse_various_csv.py
# * Created : 2020-09-14
# * Authors : Benoit Michau
# *--------------------------------------------------------
# */
import sys
import os
import argparse
import urllib.request
import csv
from parse_wikipedia_tables import (
generate_json,
generate_python,
)
PATH_PRE = 'raw/'
#------------------------------------------------------------------------------#
# get Egallic minimum distance between countries
#------------------------------------------------------------------------------#
# download the dataset with minimum distance between countries, from here
URL_MIN_DIST = 'https://gist.githubusercontent.com/mtriff/185e15be85b44547ed110e412a1771bf/'\
'raw/1bb4d287f79ca07f63d4c56110099c26e7c6ee7d/countries_distances.csv'
# which is an updated version of the dataset computed here:
# http://egallic.fr/en/closest-distance-between-countries/
# this dataset is computed thanks to various R packages
def get_egal_min_dist():
#
if not os.path.exists(PATH_PRE + 'csv_country_dist.csv'):
resp = urllib.request.urlopen(URL_MIN_DIST)
if resp.code != 200:
raise(Exception('resource %s not available, HTTP code %i' % (url, resp.code)))
with open(PATH_PRE + 'csv_country_dist.csv', 'w') as fd:
# as the data provided through the URL is not updated
# we better keep a local copy of it
fd.write(resp.read().decode('utf-8'))
print('> downloaded and stored to csv_country_dist.csv')
#
with open(PATH_PRE + 'csv_country_dist.csv', encoding='utf-8') as fd:
csv_lines = fd.readlines()
if 'pays1' in csv_lines[0]:
# remove header
del csv_lines[0]
while not csv_lines[-1].strip():
# remove blank lines
del csv_lines[-1]
#
# make it a dictionnary
D = {}
for n, src, dst, dist in csv.reader(csv_lines, delimiter=','):
src, dst, dist = map(lambda t: t.replace('"', '').strip(), (src, dst, dist))
if src not in D:
D[src] = {}
elif dst in D[src]:
print('> duplicate entry in Egallic csv for %s in %s' % (dst, src))
D[src][dst] = float(dist)
#
print('[+] parsed Egallic-related csv file with minimum distance between countries')
return D
#------------------------------------------------------------------------------#
# get txtnation csv list of MCC-MNC
#------------------------------------------------------------------------------#
# download the csv file corresponding to the list of MCC MNC provided by txtNation
# as explained here:
# https://clients.txtnation.com/hc/en-us/articles/218719768-MCCMNC-mobile-country-code-and-mobile-network-code-list
URL_TXTN_MNC = 'https://clients.txtnation.com/hc/en-us/article_attachments/206752938/MCCMNCs_v2.csv'
def get_txtn_mnc():
#
if not os.path.exists(PATH_PRE + 'csv_txtn_mnc.csv'):
resp = urllib.request.urlopen(URL_TXTN_MNC)
if resp.code != 200:
raise(Exception('resource %s not available, HTTP code %i' % (url, resp.code)))
with open(PATH_PRE + 'csv_txtn_mnc.csv', 'w') as fd:
# as the data provided through the URL is not updated
# we better keep a local copy of it
fd.write(resp.read().decode('latin_1'))
print('> downloaded and stored to csv_txtn_mnc.csv')
#
with open(PATH_PRE + 'csv_txtn_mnc.csv', encoding='utf-8') as fd:
csv_lines = fd.readlines()
if 'MCCMNC' in csv_lines[0]:
# remove header
del csv_lines[0]
while not csv_lines[-1].strip():
# remove blank lines
del csv_lines[-1]
#
# make it a dictionnary
D = {}
for mccmnc, _, mcc, _, mnc, _, country, mno in csv.reader(csv_lines, delimiter=','):
if mccmnc in D:
print('> duplicate entry in txtNation for %s' % mccmnc)
if isinstance(D[mccmnc], list):
D[mccmnc].append( (country, mno) )
else:
D[mccmnc] = [D[mccmnc], (country, mno)]
else:
assert(mccmnc.startswith(mcc) and mnc in mccmnc[3:])
D[mccmnc] = (country, mno)
#
print('[+] parsed txtNation csv file with list of MCC-MNC')
return D
#------------------------------------------------------------------------------#
# Main
#------------------------------------------------------------------------------#
URL_LICENSE_EGAL = 'http://egallic.fr/en/closest-distance-between-countries/'
URL_LICENSE_TXTN = 'https://clients.txtnation.com/hc/en-us/articles/218719768-MCCMNC-mobile-country-code-and-mobile-network-code-list'
def main():
parser = argparse.ArgumentParser(description=
'dump csv files from the Egallic blog (distance between countries) and the txtNation website (list of MCC-MNC)')
parser.add_argument('-j', action='store_true', help='produce a JSON file (with suffix .json)')
parser.add_argument('-p', action='store_true', help='produce a Python file (with suffix .py)')
args = parser.parse_args()
try:
DE = get_egal_min_dist()
DT = get_txtn_mnc()
except Exception as err:
print('> error occured: %s' % err)
return 1
#
if args.j:
generate_json(DE, PATH_PRE + 'csv_egal_min_dist.json', [URL_MIN_DIST], URL_LICENSE_EGAL)
generate_json(DT, PATH_PRE + 'csv_txtn_mccmnc.json', [URL_TXTN_MNC], URL_LICENSE_TXTN)
if args.p:
generate_python(DE, PATH_PRE + 'csv_egal_min_dist.py', [URL_MIN_DIST], URL_LICENSE_EGAL)
generate_python(DT, PATH_PRE + 'csv_txtn_mccmnc.py', [URL_TXTN_MNC], URL_LICENSE_TXTN)
return 0
if __name__ == '__main__':
sys.exit(main())