Add `-S' option to list all stations - lefrecce - Retrieve information about next trains and stations via lefrecce.it
 (HTM) hg clone https://bitbucket.org/iamleot/lefrecce
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) changeset 2683673ef292d94dd4376c4c47794fbaad6cefee
 (DIR) parent 9f2725450b4cbb1ace9ec84830eeb6962a184f06
 (HTM) Author: Leonardo Taccari <iamleot@gmail.com>
       Date:   Sat, 17 Feb 2018 19:53:33 
       
       Add `-S' option to list all stations
       
       trenitalia.com instead of doing possible HTTP request just download this
       JSON... Add support for it as well via `-S' option and list_stations() function
       that just return a list of all the stations.
       
       XXX: According to:
       XXX:
       XXX:  % lefrecce -S | sort | uniq -c | sort -n
       XXX:
       XXX: there are duplicates...
       
       Diffstat:
        lefrecce.py |  24 ++++++++++++++++++++++++
        1 files changed, 24 insertions(+), 0 deletions(-)
       ---
       diff -r 9f2725450b4c -r 2683673ef292 lefrecce.py
       --- a/lefrecce.py       Sat Feb 17 18:51:29 2018 +0100
       +++ b/lefrecce.py       Sat Feb 17 19:53:33 2018 +0100
       @@ -137,11 +137,29 @@
            return stations
        
        
       +def list_stations():
       +    url = 'http://www.trenitalia.com/cms-file/common/js/themes/trenitalia_2014/001/list_json.js'
       +
       +    res = ''
       +    req = request.Request(url, headers={'Accept-Language': 'en-US'})
       +    with request.urlopen(req) as r:
       +        for l in r:
       +            res += l.decode()
       +    j = json.loads(res)
       +
       +    stations = []
       +    for station in j:
       +        stations.append(list(station)[0])
       +
       +    return stations
       +
       +
        if __name__ == '__main__':
            import argparse
            import sys
        
            ap = argparse.ArgumentParser(description='Retrieve information from lefrecce.it')
       +    ap.add_argument('-S', action='store_true', help='List all stations')
            ap.add_argument('-s', metavar='search_station', type=str)
            ap.add_argument('-o', metavar='origin', type=str, \
                help='origin station')
       @@ -154,6 +172,12 @@
            ap.add_argument('-q', action='store_true', help='silent output')
            args = ap.parse_args()
        
       +    # List all stations and exit
       +    if args.S:
       +        for s in list_stations():
       +            print(s)
       +        sys.exit(0)
       +
            # Search stations and exit
            if args.s:
                for s in search_stations(args.s):