-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
92 lines (85 loc) · 2.41 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
package main
import (
"context"
"io/ioutil"
"log"
"os"
"time"
"github.com/chromedp/chromedp"
img "cdp/imgur"
)
func callWithTimeout(c context.Context, a chromedp.QueryAction, seconds time.Duration) error {
ctx, cancel := context.WithTimeout(c, seconds)
defer cancel()
err := a.Do(ctx)
return err
}
func screenshot(ctx context.Context) string {
screenshotName := "screenshot.png"
var buf []byte
chromedp.CaptureScreenshot(&buf).Do(ctx)
if err := ioutil.WriteFile(screenshotName, buf, os.FileMode(0755)); err != nil {
log.Println("Screenshot failed.")
return ""
}
// cmd := exec.Command("/usr/bin/scrot", screenshotName)
// cmd.Env = append(os.Environ(),
// "DISPLAY=:99",
// )
// err := cmd.Run()
// if err != nil {
// log.Printf("Screenshotting failed: %s", err)
// }
url, err := img.Upload(screenshotName)
if err != nil {
log.Println(err)
return ""
}
return url
}
const shortTimeOut = time.Second * 3
const timeOut = time.Second * 10
const longTimeout = time.Second * 30
func main() {
config = readConfig()
dir, err := ioutil.TempDir("", "epic-games-yoinker")
if err != nil {
log.Fatalf("Could not create user data dir for Chrome in %s\n", dir)
}
// dir := "~/.config/google-chrome"
opts := []chromedp.ExecAllocatorOption{
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
chromedp.UserDataDir(dir),
chromedp.DisableGPU,
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-blink-features", "AutomationControlled"),
}
allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
taskCtx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()
if err := chromedp.Run(taskCtx); err != nil {
log.Fatalf("Could not start Chrome?\n%s\n", err)
}
// Authenticate for imgur to be able to create an image
if len(config.ImgurClientID) > 0 {
img.Authenticate(config.ImgurClientID, config.ImgurSecret, config.ImgurRefreshToken)
}
if err := chromedp.Run(taskCtx,
chromedp.ActionFunc(func(ctx context.Context) error {
// setupLogger(ctx)
for _, user := range config.Users {
log.Printf("Yoinking free games for user: %s\n...", user.Username)
getCookies(ctx, user)
setCookiesMisc(ctx)
handleFreeGames(ctx, getFreeGameURLs(ctx, user), user)
logoutEpicGames(ctx)
log.Printf("Done with yoinking for user: %s\n", user.Username)
}
return nil
}),
); err != nil {
log.Fatal(err)
}
}