123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // SPDX-FileCopyrightText: Adam Evyčędo
- //
- // SPDX-License-Identifier: AGPL-3.0-or-later
- package traffic
- import (
- "bufio"
- "encoding/csv"
- "fmt"
- "io"
- "net/http"
- "os"
- "path/filepath"
- "time"
- "golang.org/x/text/transform"
- )
- type RockHillMyRide struct {
- client http.Client
- }
- func (RockHillMyRide) getTimezone() *time.Location {
- l, _ := time.LoadLocation("America/New_York")
- return l
- }
- func (g RockHillMyRide) ConvertVehicles() ([]Vehicle, error) {
- return []Vehicle{}, nil
- }
- func (z RockHillMyRide) GetVersions(_ time.Time, timezone *time.Location) ([]Version, error) {
- version, err := MakeVersionTimezone("00000101_99991231", timezone)
- if err != nil {
- return nil, err
- }
- version.Link = "https://rapid.nationalrtap.org/GTFSFileManagement/UserUploadFiles/11718/myriderockhill-sc-us.zip"
- return []Version{version}, nil
- }
- func (RockHillMyRide) String() string {
- return "rockhill_myride"
- }
- func (RockHillMyRide) RealtimeFeeds() map[RealtimeFeedType]string {
- return map[RealtimeFeedType]string{}
- }
- func (RockHillMyRide) Transformer() transform.Transformer {
- return transform.Nop
- }
- func (RockHillMyRide) Name() string {
- return "Rock Hill, SC My Ride"
- }
- func (RockHillMyRide) Flags() FeedFlags {
- return FeedFlags{
- Headsign: HeadsignTripHeadsing,
- StopIdFormat: "{{stop_code}}",
- StopName: "{{stop_name}} [{{stop_code}}]",
- LineName: "{{route_short_name}} {{route_long_name}}",
- }
- }
- func (RockHillMyRide) FeedPrepareZip(path string) error {
- // NOTE add timepoint
- stopTimesFile, err := os.Open(filepath.Join(path, "stop_times.txt"))
- if err != nil {
- return fmt.Errorf("while opening stop_times file: %w", err)
- }
- defer stopTimesFile.Close()
- stopTimes2File, err := os.OpenFile(filepath.Join(path, "stop_times2.txt"), os.O_RDWR|os.O_CREATE, 0644)
- if err != nil {
- return fmt.Errorf("while opening stop_times2 file: %w", err)
- }
- defer stopTimes2File.Close()
- r := csv.NewReader(bufio.NewReader(stopTimesFile))
- w := csv.NewWriter(stopTimes2File)
- header, err := r.Read()
- if err != nil {
- return fmt.Errorf("while reading stop_times header: %w", err)
- }
- fields := map[string]int{}
- for i, headerField := range header {
- fields[headerField] = i
- }
- header = append(header, "timepoint")
- err = w.Write(header)
- if err != nil {
- return fmt.Errorf("while writing stop_times header: %w", err)
- }
- for {
- record, err := r.Read()
- if err == io.EOF {
- break
- }
- if err != nil {
- return fmt.Errorf("while reading a stop_times record: %w", err)
- }
- arrivalTime := record[fields["arrival_time"]]
- if arrivalTime != "" {
- record = append(record, "1")
- } else {
- record = append(record, "0")
- }
- err = w.Write(record)
- if err != nil {
- return fmt.Errorf("while writing a stop_times record: %w", err)
- }
- }
- w.Flush()
- err = os.Remove(filepath.Join(path, "stop_times.txt"))
- if err != nil {
- return fmt.Errorf("while removing stop_times: %w", err)
- }
- err = os.Rename(filepath.Join(path, "stop_times2.txt"), filepath.Join(path, "stop_times.txt"))
- if err != nil {
- return fmt.Errorf("while renaming stop_times: %w", err)
- }
- return nil
- }
- func (RockHillMyRide) QRInfo() (string, QRLocation, string) {
- return "", QRLocationNone, ""
- }
|