90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package tasks
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type EpicLinkTask struct{}
|
|
|
|
func (t *EpicLinkTask) Run() error {
|
|
jsonContent, err := http.Get("https://api.egdata.app/free-games?country=US")
|
|
if err != nil {
|
|
return fmt.Errorf("failed fetching link")
|
|
}
|
|
defer jsonContent.Body.Close()
|
|
|
|
var parsedJson []struct {
|
|
ID string `json:"id"`
|
|
Namespace string `json:"namespace"`
|
|
Title string `json:"title"`
|
|
Tags []struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
} `json:"tags"`
|
|
Giveaway struct {
|
|
ID string `json:"id"`
|
|
Namespace string `json:"namespace"`
|
|
StartDate time.Time `json:"startDate"`
|
|
EndDate time.Time `json:"endDate"`
|
|
Title string `json:"title"`
|
|
} `json:"giveaway"`
|
|
}
|
|
decoder := json.NewDecoder(jsonContent.Body)
|
|
if err := decoder.Decode(&parsedJson); err != nil {
|
|
return fmt.Errorf("failed decoding JSON: %w", err)
|
|
}
|
|
|
|
offers := []*EpicOffer{}
|
|
for _, item := range parsedJson {
|
|
if item.Giveaway.StartDate.Before(time.Now()) {
|
|
offer := &EpicOffer{
|
|
Title: item.Title,
|
|
Namespace: item.Namespace,
|
|
OfferId: item.ID,
|
|
}
|
|
for _, tag := range item.Tags {
|
|
switch tag.Name {
|
|
case "Windows":
|
|
offer.System = "PC"
|
|
offers = append(offers, offer)
|
|
case "iOS":
|
|
offer.System = "iOS"
|
|
offers = append(offers, offer)
|
|
case "Android":
|
|
offer.System = "Android"
|
|
offers = append(offers, offer)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
allOffers := ""
|
|
for _, offer := range offers {
|
|
fmt.Printf("%s (%s):\n%s\n\n", offer.Title, offer.System, t.toOfferUrl(offer.ToOffer()))
|
|
allOffers += offer.ToOffer() + "&"
|
|
}
|
|
allOffers = strings.TrimSuffix(allOffers, "&")
|
|
fmt.Printf("All Offers:\n%s\n", t.toOfferUrl(allOffers))
|
|
|
|
return nil
|
|
}
|
|
|
|
func (t *EpicLinkTask) toOfferUrl(offerString string) string {
|
|
return fmt.Sprintf("https://store.epicgames.com/purchase?%s#/purchase/payment-methods", offerString)
|
|
}
|
|
|
|
type EpicOffer struct {
|
|
Title string
|
|
System string
|
|
Namespace string
|
|
OfferId string
|
|
}
|
|
|
|
func (o *EpicOffer) ToOffer() string {
|
|
return fmt.Sprintf("offers=1-%s-%s", o.Namespace, o.OfferId)
|
|
}
|