feature: switch dealsbot to sqlite kv and remove defunct ubisoft api
This commit is contained in:
parent
759aae3b54
commit
50cd000ee5
4 changed files with 35 additions and 293 deletions
|
@ -23,7 +23,7 @@ type SteamStruct struct {
|
|||
|
||||
func NewSteamApi(logger *slog.Logger) SteamStruct {
|
||||
steam := SteamStruct{
|
||||
url: "https://store.steampowered.com/search/results?force_infinite=1&maxprice=free&specials=1",
|
||||
url: "https://store.steampowered.com/search/results?force_infinite=1&maxprice=free&specials=1&category1=998",
|
||||
baseUrl: "https://store.steampowered.com/app/",
|
||||
apiUrl: "https://store.steampowered.com/api/appdetails?appids=",
|
||||
idPrefix: "steam-",
|
||||
|
|
|
@ -1,151 +0,0 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UbisoftStruct struct {
|
||||
url string
|
||||
idPrefix string
|
||||
headers map[string]string
|
||||
deals DealsMap
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewUbsioftApi(logger *slog.Logger) UbisoftStruct {
|
||||
ubisoft := UbisoftStruct{
|
||||
url: "https://free.ubisoft.com/configuration.js",
|
||||
idPrefix: "ubisoft-",
|
||||
headers: make(map[string]string),
|
||||
deals: make(map[string]Deal),
|
||||
logger: logger,
|
||||
}
|
||||
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"`
|
||||
MediaUrl string `json:"mediaURL"`
|
||||
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 := strings.TrimSpace(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
|
||||
image := news.MediaUrl
|
||||
|
||||
e.deals[id] = Deal{
|
||||
Id: id,
|
||||
Title: title,
|
||||
Url: url,
|
||||
Image: image,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e UbisoftStruct) Get() []Deal {
|
||||
var deals []Deal
|
||||
for _, deal := range e.deals {
|
||||
deals = append(deals, deal)
|
||||
}
|
||||
return deals
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue