subscribe-bot/osuapi.go

127 lines
2.4 KiB
Go
Raw Normal View History

2020-10-11 19:32:58 +00:00
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"golang.org/x/sync/semaphore"
)
const BASE_URL = "https://osu.ppy.sh/api/v2"
type Osuapi struct {
lock *semaphore.Weighted
token string
expires time.Time
clientId int
clientSecret string
}
func NewOsuapi(config *Config) *Osuapi {
// want to cap at around 1000 requests a minute, OSU cap is 1200
lock := semaphore.NewWeighted(1000)
return &Osuapi{lock, "", time.Now(), config.ClientId, config.ClientSecret}
}
func (api *Osuapi) Token() (token string, err error) {
if time.Now().Before(api.expires) {
token = api.token
return
}
data := fmt.Sprintf(
"client_id=%d&client_secret=%s&grant_type=client_credentials&scope=public",
api.clientId,
api.clientSecret,
)
resp, err := http.Post("https://osu.ppy.sh/oauth/token", "application/x-www-form-urlencoded", strings.NewReader(data))
if err != nil {
return
}
var osuToken OsuToken
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(respBody, &osuToken)
if err != nil {
return
}
log.Println("got new access token", osuToken.AccessToken[:12]+"...")
api.token = osuToken.AccessToken
api.expires = time.Now().Add(time.Duration(osuToken.ExpiresIn) * time.Second)
token = api.token
return
}
func (api *Osuapi) Request(action string, url string, result interface{}) (err error) {
err = api.lock.Acquire(context.TODO(), 1)
if err != nil {
return
}
apiUrl := BASE_URL + url
req, err := http.NewRequest(action, apiUrl, nil)
token, err := api.Token()
if err != nil {
return
}
req.Header.Add("Authorization", "Bearer "+token)
if err != nil {
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(data, result)
if err != nil {
return
}
// release the lock after 1 minute
go func() {
time.Sleep(time.Minute)
api.lock.Release(1)
}()
return
}
func (api *Osuapi) GetUserEvents(userId int, limit int, offset int) (events []Event, err error) {
url := fmt.Sprintf(
"/users/%d/recent_activity?limit=%d&offset=%d",
userId,
limit,
offset,
)
err = api.Request("GET", url, &events)
if err != nil {
return
}
return
}
type OsuToken struct {
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
AccessToken string `json:"access_token"`
}