main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Command cheapo lists and watches free game offers (currently: Epic Games Store).
  2. //
  3. // Data is read from the public storefront backend, no browser and no login
  4. // needed. Reference implementation: vogler/free-games-claimer.
  5. package main
  6. import (
  7. "encoding/json"
  8. "fmt"
  9. "os"
  10. "os/signal"
  11. "syscall"
  12. "time"
  13. "github.com/spf13/cobra"
  14. "gogs.netdome.biz/paul/cheapo/internal/epic"
  15. "gogs.netdome.biz/paul/cheapo/internal/watch"
  16. )
  17. var (
  18. flagCountry string
  19. flagLocale string
  20. flagJSON bool
  21. )
  22. func main() {
  23. root := &cobra.Command{
  24. Use: "cheapo",
  25. Short: "cheapo — free games lister (Go edition)",
  26. Long: `cheapo lists and watches free game offers (currently: Epic Games Store).
  27. Data is read from the public storefront backend, no browser and no login needed.
  28. Reference: https://github.com/vogler/free-games-claimer`,
  29. SilenceUsage: true,
  30. }
  31. root.PersistentFlags().StringVar(&flagCountry, "country", "DE", "ISO country code for store region")
  32. root.PersistentFlags().StringVar(&flagLocale, "locale", "de", "store locale (e.g. de, en-US)")
  33. root.PersistentFlags().BoolVar(&flagJSON, "json", false, "output as JSON")
  34. root.AddCommand(listCmd(), watchCmd())
  35. if err := root.Execute(); err != nil {
  36. os.Exit(1)
  37. }
  38. }
  39. func newClient() *epic.Client {
  40. c := epic.NewClient()
  41. c.Country = flagCountry
  42. c.Locale = flagLocale
  43. return c
  44. }
  45. func printGames(cmd *cobra.Command, games []epic.Game) error {
  46. if flagJSON {
  47. enc := json.NewEncoder(os.Stdout)
  48. enc.SetIndent("", " ")
  49. return enc.Encode(games)
  50. }
  51. if len(games) == 0 {
  52. fmt.Fprintln(cmd.OutOrStdout(), "No free games on offer right now.")
  53. return nil
  54. }
  55. out := cmd.OutOrStdout()
  56. for _, g := range games {
  57. fmt.Fprintf(out, "🎮 %s\n", g.Title)
  58. if g.Seller != "" {
  59. fmt.Fprintf(out, " By: %s\n", g.Seller)
  60. }
  61. if g.PriceFmt != "" {
  62. fmt.Fprintf(out, " Instead of: %s\n", g.PriceFmt)
  63. }
  64. fmt.Fprintf(out, " Free until: %s\n", g.EndDate.Local().Format("02.01.2006 15:04 (MST)"))
  65. fmt.Fprintf(out, " %s\n", g.URL)
  66. fmt.Fprintln(out)
  67. }
  68. return nil
  69. }
  70. func listCmd() *cobra.Command {
  71. return &cobra.Command{
  72. Use: "list",
  73. Aliases: []string{"ls"},
  74. Short: "List games currently free",
  75. RunE: func(cmd *cobra.Command, args []string) error {
  76. games, err := newClient().GetFreeGames(cmd.Context())
  77. if err != nil {
  78. return err
  79. }
  80. return printGames(cmd, games)
  81. },
  82. }
  83. }
  84. func watchCmd() *cobra.Command {
  85. var (
  86. interval time.Duration
  87. once bool
  88. )
  89. cmd := &cobra.Command{
  90. Use: "watch",
  91. Short: "Wait for new free games (polling)",
  92. Long: `Polls the store and reports every game that becomes newly free.
  93. The first pass records the current state as the baseline (no reports).
  94. Exit with Ctrl+C.`,
  95. RunE: func(cmd *cobra.Command, args []string) error {
  96. ctx, stop := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
  97. defer stop()
  98. c := newClient()
  99. if once {
  100. games, err := c.GetFreeGames(ctx)
  101. if err != nil {
  102. return err
  103. }
  104. return printGames(cmd, games)
  105. }
  106. w := watch.NewWatcher(c, interval)
  107. return w.Run(ctx, false, watch.NotifierFunc(func(games []epic.Game) {
  108. fmt.Fprintf(os.Stderr, "\n🔔 New free games (%s):\n", time.Now().Format("15:04:05"))
  109. for _, g := range games {
  110. fmt.Fprintf(os.Stderr, " 🎮 %s — until %s — %s\n",
  111. g.Title, g.EndDate.Local().Format("02.01."), g.URL)
  112. }
  113. }))
  114. },
  115. }
  116. cmd.Flags().DurationVar(&interval, "interval", 15*time.Minute, "Polling interval")
  117. cmd.Flags().BoolVar(&once, "once", false, "Check once and exit")
  118. return cmd
  119. }