2020-10-15 14:39:54 +00:00
|
|
|
package osuapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2021-07-20 19:37:16 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-10-15 14:39:54 +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 {
|
2021-07-20 19:40:30 +00:00
|
|
|
errors.Wrap(err, "failed to search beatmaps")
|
|
|
|
return
|
2020-10-15 14:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-07-22 06:34:33 +00:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "could not fetch beatmap")
|
|
|
|
return
|
|
|
|
}
|
2020-10-15 14:39:54 +00:00
|
|
|
|
|
|
|
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
2021-07-22 06:34:33 +00:00
|
|
|
err = errors.Wrap(err, "could not open file for downloading beatmap")
|
2020-10-15 14:39:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(file, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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-15 17:37:21 +00:00
|
|
|
|
|
|
|
type GetBeatmapsetEventsOptions struct {
|
|
|
|
User string
|
|
|
|
Types []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *Osuapi) GetBeatmapsetEvents(opts *GetBeatmapsetEventsOptions) (events []BeatmapsetEvent, err error) {
|
|
|
|
values := url.Values{}
|
|
|
|
values.Set("user", opts.User)
|
|
|
|
query := values.Encode()
|
|
|
|
for _, t := range opts.Types {
|
|
|
|
query += "&types[]=" + t
|
|
|
|
}
|
|
|
|
url := "/beatmapsets/events?" + query
|
|
|
|
fmt.Println("URL IS", url)
|
|
|
|
|
|
|
|
var reply BeatmapsetEvents
|
|
|
|
err = api.Request("GET", url, &reply)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
events = reply.Events
|
|
|
|
return
|
|
|
|
}
|