Improved ec.europa.eu web grabbing
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 9 May 2012 12:09:52 +0000 (12:09 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Wed, 9 May 2012 12:09:52 +0000 (12:09 +0000)
bin/extras/europa.py

index ad4c503d804af087547da7826142b58dca0ad029..dafc735b00fc3e8872916e7344610138cdff8228 100755 (executable)
@@ -102,13 +102,35 @@ def europa_get(callsign):
     return result
 
 
+def normalize_info(info, callsign):
+    if not info[u'IRCS']:
+        info[u'IRCS'] = callsign
+    info[u'Country Code'] = info[u'Country Code'].split(u' - ', 1)[1]
+    info[u'Port Code'] = info[u'Port Code'].split(u' - ', 1)[1]
+    info[u'LOA'] = info[u'LOA'].replace(u',', u'.')
+    info[u'Tonnage GT'] = info[u'Tonnage GT'].replace(u',', u'.').rstrip(' T')
+    info[u'Main Gear type'] = info[u'Main Gear type'].split(u' - ', 1)[1]
+    info[u'Secondary Gear type'] = info[u'Secondary Gear type'].split(u' - ', 1)[1]
+    if info[u'Public aid']:
+        info[u'Public aid'] = info[u'Public aid'].split(u' - ', 1)[1]
+    return info
+
+def myprint(string):
+    sys.stdout.write(string.encode('utf-8'))
+
 if __name__ == '__main__':
     from optparse import OptionParser
     parser = OptionParser(usage='%prog [options] callsign [callsign] ...')
     parser.add_option('-d', '--debug',
         action='store_true', dest='debug', default=False,
         help="debug mode")
-    parser.add_option('--download-sleep', help="how many seconds do we sleep after each download. default=%default", action='store', type='int', dest='sleep', default=DOWNLOAD_SLEEP_TIME)
+    parser.add_option('--download-sleep',
+        action='store', type='int', dest='sleep', default=DOWNLOAD_SLEEP_TIME,
+        help="how many seconds do we sleep after each download. default=%default")
+    parser.add_option('--table',
+        action='store_true', dest='table_format', default=False,
+        help='Format in table')
+
     (options, args) = parser.parse_args()
 
     if len(args) == 0:
@@ -136,11 +158,26 @@ if __name__ == '__main__':
         u'Main Gear type',
         u'Secondary Gear type',
         u'External Marking',
+        u'Public aid',
     ]
+    if options.table_format:
+        for i, key in enumerate(keys):
+            if i:
+                myprint(u'\t')
+            myprint(key)
+        myprint(u'\n')
     for callsign in args:
         info = europa_get(callsign)
         if not info:
             continue
-        for key in keys:
-            print key, ':', info[key]
-        print
+        info = normalize_info(info, callsign)
+        if options.table_format:
+            for i, key in enumerate(keys):
+                if i:
+                    myprint(u'\t')
+                myprint(info[key])
+            myprint(u'\n')
+        else:
+            for key in keys:
+                myprint(key+u': '+info[key]+u'\n')
+            myprint(u'\n')