feed_info.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package traffic
  5. import (
  6. "log"
  7. "apiote.xyz/p/szczanieckiej/config"
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "time"
  12. "git.sr.ht/~sircmpwn/go-bare"
  13. )
  14. func getLastFeedsUpdated(feedsPath string) (map[string]time.Time, error) {
  15. lastUpdates := map[string]time.Time{}
  16. updatesFilename := filepath.Join(feedsPath, "updated.bare")
  17. updatesFile, err := os.Open(updatesFilename)
  18. if err != nil {
  19. return lastUpdates, fmt.Errorf("while opening updates file: %w", err)
  20. }
  21. defer updatesFile.Close()
  22. var lastUpdated map[string]string
  23. err = bare.UnmarshalReader(updatesFile, &lastUpdated)
  24. if err != nil {
  25. return lastUpdates, fmt.Errorf("while unmarshaling updates file: %w", err)
  26. }
  27. for feedID, st := range lastUpdated {
  28. t, err := time.Parse(time.RFC3339, st)
  29. if err != nil {
  30. return lastUpdates, fmt.Errorf("while parsing update time for %s: %w", feedID, err)
  31. }
  32. lastUpdates[feedID] = t
  33. }
  34. return lastUpdates, nil
  35. }
  36. func GetFeedInfos(t *Traffic, cfg config.Config) (map[string]FeedInfo, map[string]time.Time, error) {
  37. feedInfos := map[string]FeedInfo{}
  38. lastUpdates, err := getLastFeedsUpdated(cfg.FeedsPath)
  39. if err != nil {
  40. return feedInfos, lastUpdates, fmt.Errorf("while getting last update dates: %w", err)
  41. }
  42. for id := range t.Feeds {
  43. versionCode, _, err := ParseDate("", id, t)
  44. if err != nil {
  45. log.Printf("while parsing date for %s: %v", id, err)
  46. continue
  47. }
  48. feedInfo, err := getFeedInfo(cfg.FeedsPath, id, versionCode)
  49. if err != nil {
  50. log.Printf("while getting feed info for %s: %v", id, err)
  51. continue
  52. }
  53. feedInfos[id] = feedInfo
  54. }
  55. return feedInfos, lastUpdates, nil
  56. }