-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (90 loc) · 2.62 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"log"
"time"
"github.com/gocolly/colly"
"github.com/spf13/cobra"
)
var (
password string
username string
url string
cmd *cobra.Command
)
func init() {
cmd = &cobra.Command{
Use: "itch-claim",
Short: "Add all items from the #BLM itch.io bundle to your account",
RunE: LoginAndAddItems,
}
pf := cmd.PersistentFlags()
pf.StringVarP(&username, "username", "", "", "itch.io username")
pf.StringVarP(&password, "password", "", "", "itch.io password")
pf.StringVarP(&url, "url", "", "", "your unique bundle url")
cobra.MarkFlagRequired(pf,"username")
cobra.MarkFlagRequired(pf,"password")
cobra.MarkFlagRequired(pf,"url")
}
func LoginAndAddItems(cmd *cobra.Command, args []string) error {
c := colly.NewCollector()
var collyErr error
if err := c.Limit(&colly.LimitRule{DomainGlob: "itch.io", Delay: 5 * time.Second}); err != nil {
return err
}
log.Println("logging in")
c.OnHTML(`.user_login_page`, func(e *colly.HTMLElement) {
// We have errors in the login submission. Need to exit
if e.ChildText(".form_errors") != "" {
collyErr = fmt.Errorf("could not login with provided credentials")
return
}
// No form errors so we can assume we are trying to log in
data := map[string]string{
"username": username,
"password": password,
"csrf_token": e.ChildAttr(`input[name="csrf_token"]`, "value"),
}
if err := c.Post("https://itch.io/login", data); err != nil {
collyErr = err
}
})
if err := c.Visit("https://itch.io/login"); err != nil {
return err
}
if collyErr != nil {
return collyErr
}
log.Println("log in successful. commencing download")
// We have logged in. Now parse the game rows from the download page
c.OnHTML("div.game_row", func(e *colly.HTMLElement) {
// If we have already claimed the game the row will contain a link instead of a form
if e.ChildAttr(".game_download_btn", "href") == "" {
log.Println("adding", e.ChildText(".game_title"))
formData := map[string]string{
"csrf_token": e.ChildAttr(`input[name="csrf_token"]`, "value"),
"game_id": e.ChildAttr(`input[name="game_id"]`, "value"),
"action": "claim",
}
if err := c.Post(url, formData); err != nil {
log.Println(err.Error())
}
}
})
// Look for the next page button and visit it
c.OnHTML("div.pager", func(e *colly.HTMLElement) {
nextPage := e.ChildAttr("a.next_page", "href")
if err := c.Visit(url + nextPage); err != nil {
fmt.Println(err.Error())
}
})
if err := c.Visit(url); err != nil {
log.Print(err.Error())
}
return nil
}
func main() {
if err := cmd.Execute(); err != nil {
log.Fatalln(err.Error())
}
}