moved to pkgs

This commit is contained in:
Seraphim Strub 2023-03-06 20:53:07 +01:00
parent 7ea6ba6809
commit 843a357428
8 changed files with 44 additions and 41 deletions

14
cmd/dealsbot/api/api.go Normal file
View file

@ -0,0 +1,14 @@
package api
type Api interface {
Load() error
Get() []Deal
}
type DealsMap map[string]Deal
type Deal struct {
Id string
Title string
Url string
}

140
cmd/dealsbot/api/epic.go Normal file
View file

@ -0,0 +1,140 @@
package api
import (
"encoding/json"
"fmt"
"github.com/disgoorg/log"
"io"
"net/http"
"time"
)
type EpicStruct struct {
url string
baseUrl string
idPrefix string
headers map[string]string
deals DealsMap
}
func NewEpicApi() EpicStruct {
epic := EpicStruct{
url: "https://store-site-backend-static-ipv4.ak.epicgames.com/freeGamesPromotions",
baseUrl: "https://store.epicgames.com/p/",
idPrefix: "epic-",
headers: make(map[string]string),
deals: make(map[string]Deal),
}
epic.headers["Accept-Language"] = "en"
epic.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"
return epic
}
type epicApiBody struct {
Data struct {
Catalog struct {
SearchStore struct {
Elements []struct {
Title string `json:"title"`
Id string `json:"id"`
Description string `json:"description"`
OfferType string `json:"offerType"`
IsCodeRedemptionOnly bool `json:"isCodeRedemptionOnly"`
ProductSlug string `json:"productSlug"`
OfferMappings []struct {
PageSlug string `json:"pageSlug"`
PageType string `json:"pageType"`
} `json:"offerMappings"`
Price struct {
TotalPrice struct {
DiscountPrice int `json:"discountPrice"`
OriginalPrice int `json:"originalPrice"`
Discount int `json:"discount"`
CurrencyCode string `json:"currencyCode"`
} `json:"totalPrice"`
} `json:"price"`
Promotions *struct {
PromotionalOffers []struct {
PromotionalOffers []struct {
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
DiscountSetting struct {
DiscountType string `json:"discountType"`
DiscountPercentage int `json:"discountPercentage"`
} `json:"discountSetting"`
} `json:"promotionalOffers"`
} `json:"promotionalOffers"`
} `json:"promotions"`
} `json:"elements"`
Paging struct {
Count int `json:"count"`
Total int `json:"total"`
} `json:"paging"`
} `json:"searchStore"`
} `json:"Catalog"`
} `json:"data"`
}
func (e EpicStruct) Load() error {
client := &http.Client{}
req, err := http.NewRequest("GET", e.url, nil)
if err != nil {
return err
}
for key, value := range e.headers {
req.Header.Set(key, value)
}
res, err := client.Do(req)
if err != nil {
return err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
var data epicApiBody
err = json.Unmarshal(body, &data)
if err != nil {
return err
}
for _, element := range data.Data.Catalog.SearchStore.Elements {
if element.Promotions == nil ||
len(element.Promotions.PromotionalOffers) == 0 ||
len(element.Promotions.PromotionalOffers[0].PromotionalOffers) == 0 ||
element.Promotions.PromotionalOffers[0].PromotionalOffers[0].DiscountSetting.DiscountPercentage != 0 {
// no deal
continue
}
productSlug := element.ProductSlug
if len(element.OfferMappings) != 0 && productSlug == "" {
productSlug = element.OfferMappings[0].PageSlug
}
if productSlug == "" {
log.Errorf("product slug not found for: %v", element.Title)
continue
}
id := fmt.Sprintf("%v%v", e.idPrefix, element.Id)
title := element.Title
url := fmt.Sprintf("%v%v", e.baseUrl, productSlug)
e.deals[id] = Deal{
Id: id,
Title: title,
Url: url,
}
}
return nil
}
func (e EpicStruct) Get() []Deal {
var deals []Deal
for _, deal := range e.deals {
deals = append(deals, deal)
}
return deals
}

148
cmd/dealsbot/api/gog.go Normal file
View file

@ -0,0 +1,148 @@
package api
import (
"fmt"
"golang.org/x/net/html"
"net/http"
"regexp"
)
type GogStruct struct {
url string
baseUrl string
idPrefix string
headers map[string]string
deals DealsMap
}
func NewGogApi() GogStruct {
gog := GogStruct{
url: "https://www.gog.com/",
baseUrl: "https://www.gog.com/game/",
idPrefix: "gog-",
headers: make(map[string]string),
deals: make(map[string]Deal),
}
gog.headers["Accept-Language"] = "en"
gog.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"
return gog
}
func (e GogStruct) Load() error {
client := &http.Client{}
// might have to add a cookie at a later time but currently works without
// "Cookie", "gog_lc=GB_GBP_en-US" or "Accept-Language", "en"
reqStore, err := http.NewRequest("GET", e.url, nil)
if err != nil {
return err
}
for key, value := range e.headers {
reqStore.Header.Set(key, value)
}
resStore, err := client.Do(reqStore)
if err != nil {
return err
}
bodyStore := html.NewTokenizer(resStore.Body)
regexAppid, err := regexp.Compile(`/\w{2}/game/([-\w]+)`)
if err != nil {
return err
}
var appIDs []string
func() {
for {
tt := bodyStore.Next()
switch {
case tt == html.ErrorToken:
// file end or error
return
case tt == html.StartTagToken:
t := bodyStore.Token()
if t.Data != "a" {
continue
}
for _, a := range t.Attr {
if !(a.Key == "id" && a.Val == "giveaway") {
continue
}
for _, attr := range t.Attr {
if attr.Key != "ng-href" {
continue
}
appID := regexAppid.FindStringSubmatch(attr.Val)
if len(appID) < 1 {
continue
}
appIDs = append(appIDs, appID[1])
}
}
}
}
}()
for _, appID := range appIDs {
reqGame, err := http.NewRequest("GET", fmt.Sprintf("%v%v", e.baseUrl, appID), nil)
if err != nil {
return err
}
for key, value := range e.headers {
reqGame.Header.Set(key, value)
}
resGame, err := client.Do(reqGame)
if err != nil {
return err
}
bodyGame := html.NewTokenizer(resGame.Body)
func() {
for {
tt := bodyGame.Next()
switch {
case tt == html.ErrorToken:
// file end or error
return
case tt == html.StartTagToken:
t := bodyGame.Token()
if t.Data != "h1" {
continue
}
for _, a := range t.Attr {
if !(a.Key == "class" && a.Val == "productcard-basics__title") {
}
if tt = bodyGame.Next(); tt != html.TextToken {
continue
}
id := fmt.Sprintf("%v%v", e.idPrefix, appID)
title := bodyGame.Token().Data
url := fmt.Sprintf("%v%v", e.baseUrl, appID)
e.deals[id] = Deal{
Id: id,
Title: title,
Url: url,
}
}
}
}
}()
}
return nil
}
func (e GogStruct) Get() []Deal {
var deals []Deal
for _, deal := range e.deals {
deals = append(deals, deal)
}
return deals
}

View file

@ -0,0 +1,147 @@
package api
import (
"encoding/json"
"fmt"
"golang.org/x/net/html"
"net/http"
"regexp"
)
type HumbleBundleStruct struct {
url string
baseUrl string
idPrefix string
headers map[string]string
deals DealsMap
}
func NewHumbleBundleApi() HumbleBundleStruct {
humbleBundle := HumbleBundleStruct{
url: "https://www.humblebundle.com/",
baseUrl: "https://www.humblebundle.com/store/",
idPrefix: "humblebundle-",
headers: make(map[string]string),
deals: make(map[string]Deal),
}
humbleBundle.headers["Accept-Language"] = "en"
humbleBundle.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"
return humbleBundle
}
type humblebundleJsonBody struct {
Mosaic []struct {
Products []struct {
ProductUrl string `json:"product_url,omitempty"`
Highlights []string `json:"highlights,omitempty"`
TileName string `json:"tile_name,omitempty"`
ProductTitle *string `json:"product_title,omitempty"`
Type string `json:"type"`
Category string `json:"category,omitempty"`
EndDateDatetime string `json:"end_date|datetime,omitempty"`
OperatingSystems []string `json:"operating_systems,omitempty"`
Platforms []string `json:"platforms,omitempty"`
} `json:"products"`
} `json:"mosaic"`
}
func (e HumbleBundleStruct) Load() error {
client := &http.Client{}
// might have to add a cookie at a later time but currently works without
// "Cookie", "gog_lc=GB_GBP_en-US" or "Accept-Language", "en"
reqStore, err := http.NewRequest("GET", e.url, nil)
if err != nil {
return err
}
for key, value := range e.headers {
reqStore.Header.Set(key, value)
}
resStore, err := client.Do(reqStore)
if err != nil {
return err
}
bodyStore := html.NewTokenizer(resStore.Body)
var data humblebundleJsonBody
err = func() error {
for {
tt := bodyStore.Next()
switch {
case tt == html.ErrorToken:
// file end or error
return nil
case tt == html.StartTagToken:
t := bodyStore.Token()
if t.Data != "script" {
continue
}
for _, a := range t.Attr {
if !(a.Key == "id" && a.Val == "webpack-json-data") {
continue
}
if tt = bodyStore.Next(); tt != html.TextToken {
continue
}
err = json.Unmarshal([]byte(bodyStore.Token().Data), &data)
if err != nil {
return err
}
}
}
}
}()
if err != nil {
return err
}
regexAppid, err := regexp.Compile(`/store/([-\w]+)`)
if err != nil {
return err
}
for _, products := range data.Mosaic {
for _, product := range products.Products {
if !contains(product.Highlights, "FREE WHILE SUPPLIES LAST") {
continue
}
appID := regexAppid.FindStringSubmatch(product.ProductUrl)
if len(appID) < 1 {
continue
}
id := fmt.Sprintf("%v%v", e.idPrefix, appID[1])
title := product.TileName
url := fmt.Sprintf("%v%v", e.baseUrl, appID[1])
e.deals[id] = Deal{
Id: id,
Title: title,
Url: url,
}
}
}
return nil
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func (e HumbleBundleStruct) Get() []Deal {
var deals []Deal
for _, deal := range e.deals {
deals = append(deals, deal)
}
return deals
}

159
cmd/dealsbot/api/steam.go Normal file
View file

@ -0,0 +1,159 @@
package api
import (
"encoding/json"
"fmt"
"golang.org/x/net/html"
"io"
"net/http"
"regexp"
)
type SteamStruct struct {
url string
baseUrl string
apiUrl string
idPrefix string
headers map[string]string
deals DealsMap
}
func NewSteamApi() SteamStruct {
steam := SteamStruct{
url: "https://store.steampowered.com/search/results?force_infinite=1&maxprice=free&specials=1",
baseUrl: "https://store.steampowered.com/app/",
apiUrl: "https://store.steampowered.com/api/appdetails?appids=",
idPrefix: "steam-",
headers: make(map[string]string),
deals: make(map[string]Deal),
}
steam.headers["Accept-Language"] = "en"
steam.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"
return steam
}
type steamApiBodyGame struct {
Success bool `json:"success"`
Data struct {
Type string `json:"type"`
Name string `json:"name"`
IsFree bool `json:"is_free"`
PriceOverview struct {
Currency string `json:"currency"`
Initial int `json:"initial"`
Final int `json:"final"`
DiscountPercent int `json:"discount_percent"`
InitialFormatted string `json:"initial_formatted"`
FinalFormatted string `json:"final_formatted"`
} `json:"price_overview"`
Platforms struct {
Windows bool `json:"windows"`
Mac bool `json:"mac"`
Linux bool `json:"linux"`
} `json:"platforms"`
} `json:"data"`
}
type steamApiBody map[string]steamApiBodyGame
func (e SteamStruct) Load() error {
client := &http.Client{}
reqStore, err := http.NewRequest("GET", e.url, nil)
if err != nil {
return err
}
for key, value := range e.headers {
reqStore.Header.Set(key, value)
}
resStore, err := client.Do(reqStore)
if err != nil {
return err
}
bodyStore := html.NewTokenizer(resStore.Body)
// could also search over each #search_resultsRows element instead of regex
regexAppid, err := regexp.Compile(`https://store\.steampowered\.com/app/(\d+)`)
if err != nil {
return err
}
var appIDs []string
func() {
for {
tt := bodyStore.Next()
switch {
case tt == html.ErrorToken:
// file end or error
return
case tt == html.StartTagToken:
t := bodyStore.Token()
if t.Data != "a" {
continue
}
for _, a := range t.Attr {
if a.Key != "href" {
continue
}
appID := regexAppid.FindStringSubmatch(a.Val)
if len(appID) < 1 {
continue
}
appIDs = append(appIDs, appID[1])
}
}
}
}()
for _, appID := range appIDs {
reqApi, err := http.NewRequest("GET", fmt.Sprintf("%v%v", e.apiUrl, appID), nil)
if err != nil {
return err
}
for key, value := range e.headers {
reqApi.Header.Set(key, value)
}
resApi, err := client.Do(reqApi)
if err != nil {
return err
}
bodyApi, err := io.ReadAll(resApi.Body)
if err != nil {
return err
}
var data steamApiBody
err = json.Unmarshal(bodyApi, &data)
if err != nil {
return err
}
if game, ok := data[appID]; ok {
if game.Data.Type != "game" {
continue
}
id := fmt.Sprintf("%v%v", e.idPrefix, appID)
title := game.Data.Name
url := fmt.Sprintf("%v%v", e.baseUrl, appID)
e.deals[id] = Deal{
Id: id,
Title: title,
Url: url,
}
}
}
return nil
}
func (e SteamStruct) Get() []Deal {
var deals []Deal
for _, deal := range e.deals {
deals = append(deals, deal)
}
return deals
}

144
cmd/dealsbot/api/ubisoft.go Normal file
View file

@ -0,0 +1,144 @@
package api
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
)
type UbisoftStruct struct {
url string
idPrefix string
headers map[string]string
deals DealsMap
}
func NewUbsioftApi() UbisoftStruct {
ubisoft := UbisoftStruct{
url: "https://free.ubisoft.com/configuration.js",
idPrefix: "ubisoft-",
headers: make(map[string]string),
deals: make(map[string]Deal),
}
ubisoft.headers["referer"] = "https://free.ubisoft.com/"
ubisoft.headers["origin"] = "https://free.ubisoft.com"
ubisoft.headers["ubi-localecode"] = "en-US"
ubisoft.headers["Accept-Language"] = "en"
ubisoft.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0"
return ubisoft
}
type ubisoftApiBody struct {
News []struct {
Type string `json:"type"`
Title string `json:"title"`
Links []struct {
Param string `json:"param"`
} `json:"links"`
} `json:"news"`
}
func (e UbisoftStruct) Load() error {
appId, prodUrl, err := func() (string, string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", e.url, nil)
if err != nil {
return "", "", err
}
for key, value := range e.headers {
req.Header.Set(key, value)
}
res, err := client.Do(req)
if err != nil {
return "", "", err
}
body, err := io.ReadAll(res.Body)
regexAppId, err := regexp.Compile(`appId:\s*'(.+)'`)
if err != nil {
return "", "", err
}
regexProd, err := regexp.Compile(`prod:\s*'(.+)'`)
if err != nil {
return "", "", err
}
appId := regexAppId.FindSubmatch(body)
prodUrl := regexProd.FindSubmatch(body)
if len(appId) < 1 || len(prodUrl) < 1 {
return "", "", errors.New("appid or prod url not found")
}
return string(appId[1]), string(prodUrl[1]), nil
}()
if err != nil {
return err
}
client := &http.Client{}
req, err := http.NewRequest("GET", prodUrl, nil)
if err != nil {
return err
}
for key, value := range e.headers {
req.Header.Set(key, value)
}
req.Header.Set("ubi-appid", appId)
res, err := client.Do(req)
if err != nil {
return err
}
body, err := io.ReadAll(res.Body)
var data ubisoftApiBody
err = json.Unmarshal(body, &data)
if err != nil {
return err
}
for _, news := range data.News {
if news.Type != "freegame" {
continue
}
title := news.Title
if len(news.Links) != 1 {
return errors.New(fmt.Sprintf("lenght of links for %v is more or less then 1", news.Title))
}
regexName, err := regexp.Compile(`https://register.ubisoft.com/([^/]*)/?`)
idFromUrl := regexName.FindStringSubmatch(news.Links[0].Param)
if err != nil {
return err
}
if len(idFromUrl) < 1 {
return errors.New("could not parse url")
}
id := fmt.Sprintf("%v%v", e.idPrefix, idFromUrl[1])
url := news.Links[0].Param
e.deals[id] = Deal{
Id: id,
Title: title,
Url: url,
}
}
return nil
}
func (e UbisoftStruct) Get() []Deal {
var deals []Deal
for _, deal := range e.deals {
deals = append(deals, deal)
}
return deals
}