-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (78 loc) · 1.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
var (
dir string // osu! songs folder
modes = make([]int, 0, 4)
stats = make([]int, 0, 5)
search string // search keywords
exist map[int]bool
ban map[int]bool
)
func main() {
loadConfig()
exist = loadExist(dir)
ban = loadBan()
page := 1
fmt.Printf("osu! Songs folder: %s\n", dir)
fmt.Printf("Search keywords: %s\n", search)
fmt.Printf("Modes: %+v (0: Standard 1: Taiko 2: Catch 3: Mania)\n", modes)
fmt.Printf("Status: %+v (0: unranked 1: ranked 2: approved 3: qualified 4: loved)\n", stats)
time.Sleep(time.Second)
fmt.Print("\nStart Download? (y/n) ")
var yes string
_, err := fmt.Scan(&yes)
check(err)
yes = strings.TrimSpace(yes)
yes = strings.ToLower(yes)
if yes != "y" {
os.Exit(1)
}
u, err := url.Parse("https://bloodcat.com/osu/")
check(err)
params := url.Values{}
params.Add("mod", "json")
params.Add("q", search)
params.Add("m", joinInts(modes))
params.Add("s", joinInts(stats))
params.Add("p", strconv.Itoa(page))
u.RawQuery = params.Encode()
for {
// request map list
fmt.Printf("Trawling page %d...\n", page)
tasks := make([]map[string]interface{}, 0, 100)
resp, err := http.Get(u.String())
check(err)
js, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
check(err)
err = json.Unmarshal(js, &tasks)
check(err)
if len(tasks) == 0 {
break
}
// do the task
for i := range tasks {
res := doTask(tasks, i)
printResult(res)
}
// ready for next request
page++
params.Set("p", strconv.Itoa(page))
u.RawQuery = params.Encode()
}
buf := bufio.NewReader(os.Stdin)
buf.Discard(1) // I guess it's due to variable yes
fmt.Print("\nFinish! Press the Enter to terminate the console.")
buf.ReadBytes('\n') // wait for Enter Key
}