| 1 |
|
| 2 |
"""Create a MID csv table from the ITU web page. |
| 3 |
|
| 4 |
Locations with more than one line of MID values will have a dash in |
| 5 |
their country name. After creating a draft mid.csv, you will need |
| 6 |
to edit those entries. |
| 7 |
|
| 8 |
There are multiple locations listed for a MID (e.g. 306). It is unclear how |
| 9 |
to handle those. |
| 10 |
""" |
| 11 |
|
| 12 |
import re |
| 13 |
import urllib2 |
| 14 |
from bs4 import BeautifulSoup |
| 15 |
|
| 16 |
mid_url = 'http://www.itu.int/online/mms/glad/cga_mids.sh?lng=E' |
| 17 |
|
| 18 |
data = urllib2.urlopen(mid_url).read() |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
soup = BeautifulSoup(data) |
| 24 |
|
| 25 |
with open('dacs.h', 'w') as dac_out, open('mid2.csv', 'w') as mid_out: |
| 26 |
|
| 27 |
mid_out.write("""prefix,country |
| 28 |
# prefix is used as DAC for binary messages or as the 1st three of the MMSI |
| 29 |
# http://www.itu.int/online/mms/glad/cga_mids.sh?lng=E\n""") |
| 30 |
|
| 31 |
for tr in soup.find_all('tr'): |
| 32 |
td = tr.find('td') |
| 33 |
try: |
| 34 |
text = td.get_text() |
| 35 |
except AttributeError: |
| 36 |
continue |
| 37 |
if re.match('^\d{3}', text): |
| 38 |
|
| 39 |
|
| 40 |
mid_vals = [int(val) for val in text.split(',')] |
| 41 |
|
| 42 |
else: |
| 43 |
continue |
| 44 |
country = td.findNextSibling().get_text().strip() |
| 45 |
for mid in mid_vals: |
| 46 |
try: |
| 47 |
mid_out.write('%s,"%s"\n' % (mid, country)) |
| 48 |
except UnicodeEncodeError: |
| 49 |
mid_out.write('BAD mid %s\n"' % mid) |
| 50 |
print 'BAD mid', mid |
| 51 |
|
| 52 |
for mid in mid_vals: |
| 53 |
try: |
| 54 |
header_country = country.replace(' ', '_').split('(')[0].upper() |
| 55 |
header_country = header_country.rstrip('_') |
| 56 |
header_country = header_country.split('_-_')[0] |
| 57 |
dac_out.write(' AIS_DAC_%d_%s = %d,\n' % (mid, |
| 58 |
header_country, |
| 59 |
mid)) |
| 60 |
except UnicodeEncodeError: |
| 61 |
dac_out.write('BAD mid %s\n"' % mid) |
| 62 |
|
| 63 |
|