szczanieckiej.git

commit cc95e67c9b8514b9e8d1cc337b5d103cf20717c6

Author: Adam <git@apiote.xyz>

use translations for description and attribute

 go.mod | 1 
 go.sum | 1 
 main.go | 6 +++
 traffic/berlin_vbb.go | 29 +++------------------
 traffic/brussels_stib_mivb.go | 24 +----------------
 traffic/convert.go | 48 +++++++++++++++++++++++++++++-------
 traffic/feeds.go | 3 --
 traffic/gzm_ztm.go | 17 -------------
 traffic/poznan_ztm.go | 19 --------------


diff --git a/go.mod b/go.mod
index a99a65fb076ba60a94b001b3b96889f582444eb2..63d5b72d7c6aaf45287c07ad46c69dfbc1668923 100644
--- a/go.mod
+++ b/go.mod
@@ -13,6 +13,7 @@ 	github.com/ulikunitz/xz v0.5.10
 	golang.org/x/text v0.3.6
 	golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e
 	google.golang.org/protobuf v1.26.0
+	gopkg.in/yaml.v3 v3.0.1
 	notabug.org/apiote/gott v1.1.2
 )
 




diff --git a/go.sum b/go.sum
index 8b15fc35a5b914735ada513311b7d2a03eff529c..12aad1e8e2991b464bc6052d8fb9cf19d4c70087 100644
--- a/go.sum
+++ b/go.sum
@@ -39,6 +39,7 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
 google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=




diff --git a/main.go b/main.go
index 1f18d78744ff943677e90f0a3eff0cc04a87d7c5..9e4f3fe8a2d8d7e3d8d7187d365dad44d2144bd4 100644
--- a/main.go
+++ b/main.go
@@ -6,12 +6,16 @@ 	"apiote.xyz/p/szczanieckiej/server"
 	"apiote.xyz/p/szczanieckiej/traffic"
 
 	"context"
+	"embed"
 	"fmt"
 	"log"
 	"os"
 	"os/signal"
 	"syscall"
 )
+
+//go:embed translations
+var feedTranslations embed.FS
 
 func main() {
 	if len(os.Args) == 1 {
@@ -35,7 +39,7 @@ 	traffic.EnableFeeds(cfg, &t)
 
 	switch command {
 	case "convert":
-		err := traffic.Prepare(cfg, t, -1)
+		err := traffic.Prepare(cfg, t, -1, feedTranslations)
 		if err != nil {
 			log.Println(err)
 			os.Exit(1)




diff --git a/traffic/berlin_vbb.go b/traffic/berlin_vbb.go
index 3484d5479b452aadec3ab865031f360b718d7076..d70e57f0eb8b181cc15df6baaca7a76b607206ce 100644
--- a/traffic/berlin_vbb.go
+++ b/traffic/berlin_vbb.go
@@ -1,20 +1,18 @@
 package traffic
 
 import (
-	"bufio"
-	"encoding/csv"
-	"io"
-	"path/filepath"
-	"strings"
-
 	"apiote.xyz/p/szczanieckiej/transformers"
 
+	"bufio"
+	"encoding/csv"
 	"fmt"
+	"io"
 	"net/http"
 	"os"
+	"path/filepath"
+	"strings"
 	"time"
 
-	"golang.org/x/text/language"
 	"golang.org/x/text/transform"
 )
 
@@ -61,23 +59,6 @@ }
 
 func (z VbbBerlin) Name() string {
 	return "Berlin/Brandenburg VBB"
-}
-
-func (z VbbBerlin) Attribution() map[language.Tag]string {
-	// TODO
-	return map[language.Tag]string{
-		language.Und: "GTFS files downloaded from https://www.vbb.de/vbbgtfs and converted to TRAFFIC",
-	}
-}
-
-func (z VbbBerlin) Description() map[language.Tag]string {
-	return map[language.Tag]string{
-		language.Und:             "Timetable for public transport in German metropolitan region including the city state of Berlin and the surrounding state of Brandenburg organised by Verkehrsverbund Berlin-Brandenburg (VBB). Includes trams, busses, U-Bahn (underground), S-Bahn, ferries, and intercity trains",
-		language.BritishEnglish:  "Timetable for public transport in German metropolitan region including the city state of Berlin and the surrounding state of Brandenburg organised by Verkehrsverbund Berlin-Brandenburg (VBB). Includes trams, busses, U-Bahn (underground), S-Bahn, ferries, and intercity trains",
-		language.AmericanEnglish: "Timetable for public transport in German metropolitan region including the city state of Berlin and the surrounding state of Brandenburg organized by Verkehrsverbund Berlin-Brandenburg (VBB). Includes trams, busses, U-Bahn (subway), S-Bahn, ferries, and intercity trains",
-		language.Polish:          "Rozkład jazdy transportu publicznego w niemieckim rejonie metropolitalnym (Berlin i Brandenburgia) organizowanego przez Verkehrsverbund Berlin-Brandenburg (VBB). Zawiera tramwaje, autobusy, U-Bahn (metro), S-Bahn, promy i koleje międzymiastowe",
-		language.German:          "Fahrplan für den ÖPNV in Berlin und Brandenburg (VBB). Umfasst Straßenbahnen, Busse, U-Bahn, S-Bahn, Fähren und Fernverkehr",
-	}
 }
 
 func (z VbbBerlin) Flags() FeedFlags {




diff --git a/traffic/brussels_stib_mivb.go b/traffic/brussels_stib_mivb.go
index 3deab2208cfeef53274e7f7c55710af5eb49249b..de4402b187af2ee3f86838672be8b47834f93292 100644
--- a/traffic/brussels_stib_mivb.go
+++ b/traffic/brussels_stib_mivb.go
@@ -1,18 +1,16 @@
 package traffic
 
 import (
+	"apiote.xyz/p/szczanieckiej/transformers"
+
 	"encoding/csv"
 	"encoding/xml"
-
-	"apiote.xyz/p/szczanieckiej/transformers"
-
 	"fmt"
 	"net/http"
 	"os"
 	"path/filepath"
 	"time"
 
-	"golang.org/x/text/language"
 	"golang.org/x/text/transform"
 	"golang.org/x/tools/blog/atom"
 )
@@ -71,24 +69,6 @@ }
 
 func (z StibMivbBrussels) Name() string {
 	return "Brussels STIB-MIVB"
-}
-
-func (z StibMivbBrussels) Attribution() map[language.Tag]string {
-	return map[language.Tag]string{
-		language.Und:     "GTFS files downloaded from https://stibmivb.opendatasoft.com and converted to TRAFFIC",
-		language.English: "GTFS files downloaded from https://stibmivb.opendatasoft.com and converted to TRAFFIC",
-		language.Polish:  "Pliki GTFS pobrane z https://stibmivb.opendatasoft.com i przekonwertowane do formatu TRAFFIC",
-	}
-}
-
-func (z StibMivbBrussels) Description() map[language.Tag]string {
-	return map[language.Tag]string{
-		language.Und:             "Timetable for Brussels public transport organised by STIB-MIVB",
-		language.English:         "Timetable for Brussels public transport organised by STIB-MIVB",
-		language.BritishEnglish:  "Timetable for Brussels public transport organised by STIB-MIVB",
-		language.AmericanEnglish: "Timetable for Brussels public transport organised by STIB-MIVB",
-		language.Polish:          "Rozkład jazdy brukselskiego transportu publicznego organizowanego przez STIB-MIVB",
-	}
 }
 
 func (z StibMivbBrussels) Flags() FeedFlags {




diff --git a/traffic/convert.go b/traffic/convert.go
index 3a929614cb729449ea965121752188ebc8a0b5e4..c4f70bf77d39e64b9093e04c4bcb5c2e2f48c63c 100644
--- a/traffic/convert.go
+++ b/traffic/convert.go
@@ -10,6 +10,7 @@
 // FIXME LineName is not unique
 
 import (
+
 	"apiote.xyz/p/szczanieckiej/config"
 	"apiote.xyz/p/szczanieckiej/file"
 
@@ -27,6 +28,9 @@ 	"sort"
 	"strings"
 	"syscall"
 	"time"
+	"embed"
+
+	"gopkg.in/yaml.v3"
 
 	gott2 "apiote.xyz/p/gott/v2"
 	"git.sr.ht/~sircmpwn/go-bare"
@@ -62,6 +66,7 @@ 	updatesFile        *os.File
 	updates            map[string]string
 	etags              map[string]string
 	newEtags           map[string]string
+	feedTranslations embed.FS
 }
 
 type feedConverter struct {
@@ -69,6 +74,7 @@ 	TmpFeedPath  string
 	GtfsFilename string
 	Feed         Feed
 	HomeFeedPath string
+	feedTranslations embed.FS
 
 	TrafficCalendarFile *os.File
 	Departures          map[string][]Departure
@@ -1344,20 +1350,40 @@ 	}
 
 	feedInfo.QrHost, feedInfo.QrLocation, feedInfo.QrSelector = c.Feed.QRInfo()
 
-	feedInfo.Attributions = map[string]string{}
-	for lng, attr := range c.Feed.Attribution() {
-		feedInfo.Attributions[lng.String()] = attr
-	}
-	feedInfo.Descriptions = map[string]string{}
-	for lng, attr := range c.Feed.Description() {
-		feedInfo.Descriptions[lng.String()] = attr
-	}
+	feedInfo.Attributions, feedInfo.Descriptions, err = getAttrDesc(c.Feed.String(), c.feedTranslations)
 
 	feedInfo.Name = c.Feed.Name()
 
 	c.feedInfo = feedInfo
 
-	return c, nil
+	return c, err
+}
+
+func getAttrDesc(feedID string, feedTranslations embed.FS) (map[string]string, map[string]string, error) {
+	attributions := map[string]string{}
+	descriptions := map[string]string{}
+	dir, err := feedTranslations.ReadDir("translations")
+	if err != nil {
+		return attributions, descriptions, err
+	}
+	for _, f := range dir {
+		translation := map[string]string{}
+		name := f.Name()
+		lang := strings.Split(name, ".")[1]
+		fileContent, err := feedTranslations.ReadFile("translations/"+name)
+		if err != nil {
+			log.Printf("error reading translation %s\n", name)
+			continue
+		}
+		yaml.Unmarshal(fileContent, &translation)
+		attributions[lang] = translation[feedID + "_attribution"]
+		descriptions[lang] = translation[feedID + "_description"]
+		if lang == "en" {
+			attributions["und"] = translation[feedID + "_attribution"]
+			descriptions["und"] = translation[feedID + "_description"]
+		}
+	}
+	return attributions, descriptions, nil
 }
 
 func readTranslations(c feedConverter) (feedConverter, error) { // O(n:translations) ; ( -- translations >>)
@@ -1588,6 +1614,7 @@ 				TmpFeedPath:  args.tmpFeedPath,
 				GtfsFilename: gtfsFile,
 				Feed:         args.feed,
 				HomeFeedPath: args.homeFeedPath,
+				feedTranslations: args.feedTranslations,
 			},
 			LogLevel: gott2.Debug,
 		}
@@ -1716,7 +1743,7 @@ 	args.updatesFile.Close()
 	return err
 }
 
-func Prepare(cfg config.Config, t Traffic, bimbaPid int) error { // todo(BAF18) remove pid
+func Prepare(cfg config.Config, t Traffic, bimbaPid int, feedTranslations embed.FS) error { // todo(BAF18) remove pid
 	etags, err := readEtags(cfg)
 	if err != nil {
 		return fmt.Errorf("while reading etags: %w", err)
@@ -1734,6 +1761,7 @@ 			location: feed.GetLocation(),
 			updates:  map[string]string{},
 			etags:    etags,
 			newEtags: newEtags,
+			feedTranslations: feedTranslations,
 		}}
 		s, err := gott.NewResult(r).
 			SetLevelLog(gott.Debug).




diff --git a/traffic/feeds.go b/traffic/feeds.go
index 1d47fe2f6aabbf6c3f542427f091ea1a6a38e3fe..15c58d63f753dad86fc8bd06aaaaf6102ee39b6b 100644
--- a/traffic/feeds.go
+++ b/traffic/feeds.go
@@ -10,7 +10,6 @@ 	"path/filepath"
 	"strings"
 	"time"
 
-	"golang.org/x/text/language"
 	"golang.org/x/text/transform"
 )
 
@@ -22,8 +21,6 @@ 	GetLocation() *time.Location
 	RealtimeFeeds() []string
 	Transformer() transform.Transformer
 	Name() string
-	Attribution() map[language.Tag]string // SHOULD have und tag
-	Description() map[language.Tag]string // SHOULD have und tag
 	Flags() FeedFlags
 	FeedPrepareZip(string) error
 	QRInfo() (string, QRLocation, string)




diff --git a/traffic/gzm_ztm.go b/traffic/gzm_ztm.go
index b7b7a16671f980fa07ed6ad54f0f60c733e3a778..a4041052c0943fc8be3f4d2b76e27832beae10c3 100644
--- a/traffic/gzm_ztm.go
+++ b/traffic/gzm_ztm.go
@@ -13,7 +13,6 @@ 	"path/filepath"
 	"regexp"
 	"time"
 
-	"golang.org/x/text/language"
 	"golang.org/x/text/transform"
 )
 
@@ -156,22 +155,6 @@ }
 
 func (z GzmZtm) Name() string {
 	return "Metropolia GZM ZTM"
-}
-
-func (z GzmZtm) Attribution() map[language.Tag]string {
-	return map[language.Tag]string{
-		language.Und:     "GTFS files downloaded from https://otwartedane.metropoliagzm.pl/dataset/groups/rozklady-jazdy-i-lokalizacja-przystankow-gtfs and converted to TRAFFIC",
-		language.English: "GTFS files downloaded from https://otwartedane.metropoliagzm.pl/dataset/groups/rozklady-jazdy-i-lokalizacja-przystankow-gtfs and converted to TRAFFIC",
-		language.Polish:  "Pliki GTFS pobrane z https://otwartedane.metropoliagzm.pl/dataset/groups/rozklady-jazdy-i-lokalizacja-przystankow-gtfs i przekonwertowane do formatu TRAFFIC",
-	}
-}
-
-func (z GzmZtm) Description() map[language.Tag]string {
-	return map[language.Tag]string{
-		language.Und:     "Timetable for Metropolis GZM public transport organised by Metropolitan Transport Authority (ZTM)",
-		language.English: "Timetable for Metropolis GZM public transport organised by Metropolitan Transport Authority (ZTM)",
-		language.Polish:  "Rozkład jazdy transportu publicznego w Metropolii GZM organizowanego przez Zarząd Transportu Miejskiego (ZTM)",
-	}
 }
 
 func (z GzmZtm) Flags() FeedFlags {




diff --git a/traffic/poznan_ztm.go b/traffic/poznan_ztm.go
index 23c4370386c52023728edae73af1622b05ca7810..30b796c7695eff76e6f21dac34a0cfc5c1932759 100644
--- a/traffic/poznan_ztm.go
+++ b/traffic/poznan_ztm.go
@@ -13,7 +13,6 @@ 	"regexp"
 	"strings"
 	"time"
 
-	"golang.org/x/text/language"
 	"golang.org/x/text/transform"
 
 	"git.sr.ht/~sircmpwn/go-bare"
@@ -160,24 +159,6 @@ }
 
 func (z ZtmPoznan) Name() string {
 	return "Poznań ZTM"
-}
-
-func (z ZtmPoznan) Attribution() map[language.Tag]string {
-	return map[language.Tag]string{
-		language.Und:     "GTFS files downloaded from https://ztm.poznan.pl/pl/dla-deweloperow/gtfsFiles and converted to TRAFFIC",
-		language.English: "GTFS files downloaded from https://ztm.poznan.pl/pl/dla-deweloperow/gtfsFiles and converted to TRAFFIC",
-		language.Polish:  "Pliki GTFS pobrane z https://ztm.poznan.pl/pl/dla-deweloperow/gtfsFiles i przekonwertowane do formatu TRAFFIC",
-	}
-}
-
-func (z ZtmPoznan) Description() map[language.Tag]string {
-	return map[language.Tag]string{
-		language.Und:             "Timetable for Poznań public transport organised by Zarząd Transportu Miejskiego (ZTM)",
-		language.English:         "Timetable for Poznań public transport organised by Zarząd Transportu Miejskiego (ZTM)",
-		language.BritishEnglish:  "Timetable for Poznań public transport organised by Zarząd Transportu Miejskiego (ZTM)",
-		language.AmericanEnglish: "Timetable for Poznań public transport organized by Zarząd Transportu Miejskiego (ZTM)",
-		language.Polish:          "Rozkład jazdy poznańskiego transportu publicznego organizowanego przez Zarząd Transportu Miejskiego (ZTM)",
-	}
 }
 
 func (z ZtmPoznan) Flags() FeedFlags {