replace zxq.co/ripple/hanayo

This commit is contained in:
Alicia
2019-02-23 13:29:15 +00:00
commit c3d206c173
5871 changed files with 1353715 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
// Package downloader implements downloading from the osu! website, through,
// well, mostly scraping and dirty hacks.
package downloader
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
)
// LogIn logs in into an osu! account and returns a Client.
func LogIn(username, password string) (*Client, error) {
j, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
return nil, err
}
c := &http.Client{
Jar: j,
}
vals := url.Values{}
vals.Add("redirect", "/")
vals.Add("sid", "")
vals.Add("username", username)
vals.Add("password", password)
vals.Add("autologin", "on")
vals.Add("login", "login")
loginResp, err := c.PostForm("https://osu.ppy.sh/forum/ucp.php?mode=login", vals)
if err != nil {
return nil, err
}
if loginResp.Request.URL.Path != "/" {
return nil, errors.New("downloader: Login: could not log in (was not redirected to index)")
}
return (*Client)(c), nil
}
// Client is a wrapper around an http.Client which can fetch beatmaps from the
// osu! website.
type Client http.Client
// HasVideo checks whether a beatmap has a video.
func (c *Client) HasVideo(setID int) (bool, error) {
h := (*http.Client)(c)
page, err := h.Get(fmt.Sprintf("https://osu.ppy.sh/s/%d", setID))
if err != nil {
return false, err
}
defer page.Body.Close()
body, err := ioutil.ReadAll(page.Body)
if err != nil {
return false, err
}
return bytes.Contains(body, []byte(fmt.Sprintf(`href="/d/%dn"`, setID))), nil
}
// Download downloads a beatmap from the osu! website. noVideo specifies whether
// we should request the beatmap to not have the video.
func (c *Client) Download(setID int, noVideo bool) (io.ReadCloser, error) {
suffix := ""
if noVideo {
suffix = "n"
}
return c.getReader(strconv.Itoa(setID) + suffix)
}
// ErrNoRedirect is returned from Download when we were not redirect, thus
// indicating that the beatmap is unavailable.
var ErrNoRedirect = errors.New("no redirect happened, beatmap could not be downloaded")
func (c *Client) getReader(str string) (io.ReadCloser, error) {
h := (*http.Client)(c)
resp, err := h.Get("https://osu.ppy.sh/d/" + str)
if err != nil {
return nil, err
}
if resp.Request.URL.Host == "osu.ppy.sh" {
resp.Body.Close()
return nil, ErrNoRedirect
}
return resp.Body, nil
}

View File

@@ -0,0 +1,76 @@
package downloader
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"os"
"testing"
)
var c *Client
var (
username = os.Getenv("OSU_USERNAME")
password = os.Getenv("OSU_PASSWORD")
)
func TestLogIn(t *testing.T) {
var err error
c, err = LogIn(username, password)
if err != nil {
t.Fatal(err)
}
}
func TestLogInWrongDetails(t *testing.T) {
_, err := LogIn("a", "i")
if err == nil {
t.Fatal("Unexpected non-error when trying to log in with user 'a' and password 'i'")
}
}
func TestDownload(t *testing.T) {
if c == nil {
t.Skip("c is nil")
}
{
vid, novid, err := c.Download(1)
if err != nil {
t.Fatal(err)
}
if novid != nil {
t.Fatal("Returning a video when there's supposed to be no video")
}
md5Test(t, vid, "f40fae62893087e72672b3e6d1468a70")
}
{
vid, novid, err := c.Download(100517)
if err != nil {
t.Fatal(err)
}
if novid == nil {
t.Fatal("Returning no video when there's supposed to be one.")
}
md5Test(t, vid, "500b361f47ff99551dbb9931cdf39ace")
md5Test(t, novid, "3de1e07850e2fe1f21333e4d5b01a350")
}
}
func cleanUp(files ...string) {
for _, f := range files {
os.Remove(f)
}
}
func md5Test(t *testing.T, f io.Reader, expect string) {
data, err := ioutil.ReadAll(f)
if err != nil {
t.Fatal(err)
}
sum := fmt.Sprintf("%x", md5.Sum(data))
if sum != expect {
t.Fatal("expecting md5 sum to be", expect, "got", sum)
}
}