2020-10-12 13:47:28 +00:00
|
|
|
package osuapi
|
2020-10-11 19:32:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-10-12 04:22:47 +00:00
|
|
|
"io"
|
2020-10-11 19:32:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-10-12 12:23:32 +00:00
|
|
|
"net/url"
|
2020-10-12 04:22:47 +00:00
|
|
|
"os"
|
2020-10-11 19:32:58 +00:00
|
|
|
"strings"
|
2020-10-14 20:19:06 +00:00
|
|
|
"sync"
|
2020-10-11 19:32:58 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/sync/semaphore"
|
2020-10-12 13:47:28 +00:00
|
|
|
|
|
|
|
"subscribe-bot/config"
|
2020-10-11 19:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const BASE_URL = "https://osu.ppy.sh/api/v2"
|
|
|
|
|
|
|
|
type Osuapi struct {
|
2020-10-12 14:25:50 +00:00
|
|
|
httpClient *http.Client
|
|
|
|
lock *semaphore.Weighted
|
|
|
|
token string
|
|
|
|
expires time.Time
|
|
|
|
config *config.Config
|
2020-10-14 20:19:06 +00:00
|
|
|
|
|
|
|
tokenLock sync.RWMutex
|
|
|
|
isFetchingToken bool
|
2020-10-11 19:32:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 13:47:28 +00:00
|
|
|
func New(config *config.Config) *Osuapi {
|
2020-10-12 06:33:45 +00:00
|
|
|
client := &http.Client{
|
2020-10-12 12:23:32 +00:00
|
|
|
Timeout: 9 * time.Second,
|
2020-10-12 06:33:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 19:32:58 +00:00
|
|
|
// want to cap at around 1000 requests a minute, OSU cap is 1200
|
|
|
|
lock := semaphore.NewWeighted(1000)
|
2020-10-14 20:19:06 +00:00
|
|
|
|
|
|
|
return &Osuapi{
|
|
|
|
httpClient: client,
|
|
|
|
lock: lock,
|
|
|
|
expires: time.Now(),
|
|
|
|
config: config,
|
|
|
|
}
|
2020-10-11 19:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *Osuapi) Token() (token string, err error) {
|
|
|
|
if time.Now().Before(api.expires) {
|
|
|
|
token = api.token
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-14 20:19:06 +00:00
|
|
|
if api.isFetchingToken {
|
|
|
|
api.tokenLock.RLock()
|
|
|
|
token = api.token
|
|
|
|
api.tokenLock.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
api.tokenLock.Lock()
|
|
|
|
api.isFetchingToken = true
|
|
|
|
|
2020-10-11 19:32:58 +00:00
|
|
|
data := fmt.Sprintf(
|
2020-10-14 19:08:38 +00:00
|
|
|
"client_id=%s&client_secret=%s&grant_type=client_credentials&scope=public",
|
2020-10-12 14:25:50 +00:00
|
|
|
api.config.Oauth.ClientId,
|
|
|
|
api.config.Oauth.ClientSecret,
|
2020-10-11 19:32:58 +00:00
|
|
|
)
|
|
|
|
|
2020-10-12 09:52:19 +00:00
|
|
|
resp, err := http.Post(
|
|
|
|
"https://osu.ppy.sh/oauth/token",
|
|
|
|
"application/x-www-form-urlencoded",
|
|
|
|
strings.NewReader(data),
|
|
|
|
)
|
2020-10-11 19:32:58 +00:00
|
|
|
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)
|
2020-10-14 20:19:06 +00:00
|
|
|
|
2020-10-11 19:32:58 +00:00
|
|
|
token = api.token
|
2020-10-14 20:19:06 +00:00
|
|
|
api.tokenLock.Unlock()
|
2020-10-11 19:32:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-12 04:22:47 +00:00
|
|
|
func (api *Osuapi) Request0(action string, url string) (resp *http.Response, err error) {
|
2020-10-11 19:32:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-12 06:33:45 +00:00
|
|
|
resp, err = api.httpClient.Do(req)
|
2020-10-11 19:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-12 04:22:47 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
var respBody []byte
|
|
|
|
respBody, err = ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fmt.Errorf("not 200: %s", string(respBody))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// release the lock after 1 minute
|
|
|
|
go func() {
|
|
|
|
time.Sleep(time.Minute)
|
|
|
|
api.lock.Release(1)
|
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *Osuapi) Request(action string, url string, result interface{}) (err error) {
|
|
|
|
resp, err := api.Request0(action, url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-11 19:32:58 +00:00
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, result)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-12 04:22:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:50:18 +00:00
|
|
|
func (api *Osuapi) DownloadSingleBeatmap(beatmapId int, path string) (err error) {
|
|
|
|
url := fmt.Sprintf("https://osu.ppy.sh/osu/%d", beatmapId)
|
|
|
|
resp, err := api.httpClient.Get(url)
|
|
|
|
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(file, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-12 04:22:47 +00:00
|
|
|
func (api *Osuapi) GetBeatmapSet(beatmapSetId int) (beatmapSet Beatmapset, err error) {
|
|
|
|
url := fmt.Sprintf("/beatmapsets/%d", beatmapSetId)
|
|
|
|
err = api.Request("GET", url, &beatmapSet)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *Osuapi) BeatmapsetDownload(beatmapSetId int) (path string, err error) {
|
|
|
|
url := fmt.Sprintf("/beatmapsets/%d/download", beatmapSetId)
|
|
|
|
resp, err := api.Request0("GET", url)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := ioutil.TempFile(os.TempDir(), "beatmapsetDownload")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(file, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
path = file.Name()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-12 04:30:04 +00:00
|
|
|
func (api *Osuapi) GetUser(userId string) (user User, err error) {
|
|
|
|
url := fmt.Sprintf("/users/%s", userId)
|
2020-10-12 04:22:47 +00:00
|
|
|
err = api.Request("GET", url, &user)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-11 19:32:58 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:23:32 +00:00
|
|
|
func (api *Osuapi) SearchBeatmaps(rankStatus string) (beatmapSearch BeatmapSearch, err error) {
|
|
|
|
values := url.Values{}
|
|
|
|
values.Set("s", rankStatus)
|
|
|
|
query := values.Encode()
|
|
|
|
url := "/beatmapsets/search?" + query
|
|
|
|
err = api.Request("GET", url, &beatmapSearch)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-11 19:32:58 +00:00
|
|
|
type OsuToken struct {
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
}
|