~aleteoryx/muditaos

ref: f2ce6e946b1488b10442b9e93242c5c61522a13a muditaos/doc/Scripts/country_codes_get.py -rwxr-xr-x 1.4 KiB
f2ce6e94 — Michał Kamoń [EGD-6707] Add single number message notification 4 years ago
                                                                                
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
#!/usr/bin/python2.7
# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

# Get the Mobile Country Codes (MCC) and Mobile Network Codes (MNC) table
# from mcc-mnc.com and output it in JSON format.

import re
import urllib2
import json
import sqlite3 
td_re = re.compile('<td>([^<]*)</td>'*6)

html = urllib2.urlopen('http://mcc-mnc.com/').read()

tbody_start = False
conn = sqlite3.connect('country-codes.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS codes (mcc INTEGER,mnc INTEGER,iso TEXT NOT NULL,country TEXT NOT NULL,country_code INTEGER,network TEXT NOT NULL)")
mcc_mnc_list = []

for line in html.split('\n'):
    if '<tbody>' in line:
        tbody_start = True
    elif '</tbody>' in line:
        break
    elif tbody_start:
        td_search = td_re.search(line)
        i = {}
        td_search = td_re.split(line)

        i['mcc'] = td_search[1]
        i['mnc'] = td_search[2]
        i['iso'] = td_search[3]
        i['country'] = td_search[4]
        i['country_code'] = td_search[5]
        i['network'] = td_search[6][0:-1]
        sql = ("INSERT INTO codes VALUES ('%s', '%s', '%s', \"%s\", '%s', \"%s\")" % (i['mcc'], i['mnc'], i['iso'], i['country'], i['country_code'], i['network']))
        print(sql)
        c.execute(sql)
        #, i['mnc'], i['iso'], i['country'], i['country_code'], i['network'])
conn.commit()
#print json.dumps(mcc_mnc_list, indent=2)