penguin/golifehk

@golifehk bot in Telegram

businfo/mtr/busstops.go

raw ยท 3228 bytes

package mtr

import (
    "encoding/csv"
    "fmt"
    "io"
    "net/http"
    "os"
    "path/filepath"
    "strconv"
    "strings"

    "github.com/tgckpg/golifehk/utils"
)

type BusStop struct {
    RouteId string
    Direction string
    StationSeq string
    StationId string
    Latitude float64
    Longtitude float64
    Name_zhant string
    Name_en string
}

var mBusStops *map[string]BusStop

var DEF_CSV string = filepath.Join( utils.WORKDIR, "mtr_bus_stops.csv" )

func readBusStopData( r io.Reader ) ( *map[string]BusStop, error ) {

    reader := csv.NewReader( r )
    entries, err := reader.ReadAll()
    if err != nil {
        return nil, err
    }

    busStops := map[string]BusStop{}
    var headers []string
    for i, line := range entries {
        if i == 0 {
            headers = line
            line[0] = strings.TrimLeft( line[0], utils.BOM )
            continue
        }

        var entry BusStop
        for j, value := range line {
            switch headers[j] {
                case "ROUTE_ID":
                    entry.RouteId = value
                case "DIRECTION":
                    entry.Direction = value
                case "STATION_SEQNO":
                    entry.StationSeq = value
                case "STATION_ID":
                    entry.StationId = value
                case "STATION_LATITUDE":
                    v, _ := strconv.ParseFloat( value, 64 )
                    entry.Latitude = v
                case "STATION_LONGITUDE":
                    v, _ := strconv.ParseFloat( value, 64 )
                    entry.Longtitude = v
                case "STATION_NAME_CHI":
                    entry.Name_zhant = value
                case "STATION_NAME_ENG":
                    entry.Name_en = value
                default:
                    return nil, fmt.Errorf( "Unknown header \"%s\"", headers[j] )
            }
        }

        if _, t := busStops[ entry.StationId ]; t {
            return nil, fmt.Errorf( "Duplicated entry %+v", entry )
        }

        busStops[ entry.StationId ] = entry
    }
    return &busStops, nil
}

func getBusStops() (*map[string]BusStop, error) {

    if mBusStops != nil {
        return mBusStops, nil
    }

    mBusStops, err := pullLocal()
    if mBusStops != nil {
        return mBusStops, err
    }

    mBusStops, err = pullRemote()
    if mBusStops != nil {
        return mBusStops, err
    }

    return nil, err
}

func pullRemote() (*map[string]BusStop, error) {

    err := os.MkdirAll( filepath.Dir( DEF_CSV ), 0750 )
    if err != nil {
        return nil, err
    }

    resp, err := http.Get( "https://opendata.mtr.com.hk/data/mtr_bus_stops.csv" )
    if err != nil {
        return nil, err
    }

    defer resp.Body.Close()

    f, err := os.OpenFile( DEF_CSV, os.O_CREATE | os.O_RDWR, 0644 )
    if err != nil {
        return nil, err
    }

    defer f.Close()

    _, err = io.Copy( f, resp.Body )
    if err != nil {
        return nil, err
    }

    f.Seek( 0, 0 )
    return readBusStopData( f )
}

func pullLocal() (*map[string]BusStop, error) {
    f, err := os.Open( DEF_CSV )
    if err != nil {
        return nil, err
    }

    defer f.Close()

    return readBusStopData( f )
}