| 1 |
|
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
import sys |
| 5 |
import urllib,re |
| 6 |
import sqlite3 |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
def mmsi_codes(filename=None): |
| 16 |
'''Create a table of 3 digit MMSI codes |
| 17 |
@return: code lookup table |
| 18 |
@rtype: dict |
| 19 |
''' |
| 20 |
|
| 21 |
entry_re_str = r'''\<tr\>\<td\>(?P<prefixes>[0-9 ,]*)\</td\>\<td\>(?P<country>[^<]*)\</td\>\</tr\>''' |
| 22 |
entry_re = re.compile(entry_re_str) |
| 23 |
|
| 24 |
if not filename: |
| 25 |
html = urllib.urlopen('http://www.itu.int/cgi-bin/htsh/glad/cga_mids.sh?lng=E') |
| 26 |
else: |
| 27 |
html = file(filename) |
| 28 |
|
| 29 |
|
| 30 |
last_country = None |
| 31 |
codes = {} |
| 32 |
for line in html: |
| 33 |
if '<tr>' != line[0:4]: continue |
| 34 |
matches = entry_re.search(line) |
| 35 |
if not matches: continue |
| 36 |
|
| 37 |
prefixes = matches.group('prefixes') |
| 38 |
country = matches.group('country') |
| 39 |
|
| 40 |
if chr(244) in country: |
| 41 |
country = country.replace(chr(244),'o') |
| 42 |
if ' - ' == country: |
| 43 |
country = last_country |
| 44 |
|
| 45 |
for prefix in prefixes.split(','): |
| 46 |
codes[int(prefix)] = country |
| 47 |
|
| 48 |
last_country = country |
| 49 |
|
| 50 |
return codes |
| 51 |
|
| 52 |
if __name__=='__main__': |
| 53 |
codes = mmsi_codes('mid.html') |
| 54 |
for code in codes: |
| 55 |
print ('%s,"%s"' % (code,codes[code])) |