works in one run

But only with first nav, and next nav. need to generalize now.
pull/2/merge
mpl 5 years ago
parent 9f9c1f4748
commit 4f5c84331a

@ -82,7 +82,9 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
download := func(ctx context.Context, dir string) (string, error) { // download := func(ctx context.Context, dir string) (string, error) {
download := func(ctx context.Context) (string, error) {
dir := s.dlDir
keyD, ok := kb.Keys['D'] keyD, ok := kb.Keys['D']
if !ok { if !ok {
log.Fatal("NO D KEY") log.Fatal("NO D KEY")
@ -160,6 +162,29 @@ func main() {
} }
} }
mvDl := func(dlFile string) func(ctx context.Context) error {
return func(ctx context.Context) error {
dir, err := ioutil.TempDir(s.dlDir, "")
if err != nil {
return err
}
if err := os.Rename(filepath.Join(s.dlDir, dlFile), filepath.Join(dir, dlFile)); err != nil {
return err
}
println("NEW FILE: ", filepath.Join(dir, dlFile))
return nil
}
}
dlAndMove := func(ctx context.Context) error {
var err error
dlFile, err := download(ctx)
if err != nil {
return err
}
return mvDl(dlFile)(ctx)
}
// firstNav := func(ctx context.Context) chromedp.ActionFunc { // firstNav := func(ctx context.Context) chromedp.ActionFunc {
// return func(ctx context.Context) error { // return func(ctx context.Context) error {
firstNav := func(ctx context.Context) error { firstNav := func(ctx context.Context) error {
@ -172,97 +197,58 @@ func main() {
} }
// } // }
navRight := chromedp.ActionFunc(func(ctx context.Context) error { navRight := func(ctx context.Context) error {
chromedp.KeyEvent(kb.ArrowRight).Do(ctx) chromedp.KeyEvent(kb.ArrowRight).Do(ctx)
log.Printf("sent key") log.Printf("sent key")
chromedp.Sleep(500 * time.Millisecond).Do(ctx) chromedp.Sleep(5000 * time.Millisecond).Do(ctx)
chromedp.WaitReady("body", chromedp.ByQuery)
return nil return nil
}) }
navRightN := func(N int, ctx context.Context) chromedp.ActionFunc { /*
n := 0 navRightN := func(N int, ctx context.Context) chromedp.ActionFunc {
return func(ctx context.Context) error { n := 0
for { return func(ctx context.Context) error {
if n >= N { for {
break if n >= N {
} break
chromedp.KeyEvent(kb.ArrowRight).Do(ctx) }
chromedp.Sleep(500 * time.Millisecond).Do(ctx) chromedp.KeyEvent(kb.ArrowRight).Do(ctx)
/* chromedp.Sleep(500 * time.Millisecond).Do(ctx)
if err := navRight.Do(ctx); err != nil { if err := navRight(ctx); err != nil {
return err return err
} }
chromedp.Sleep(500 * time.Millisecond).Do(ctx) chromedp.Sleep(500 * time.Millisecond).Do(ctx)
if err := download.Do(ctx); err != nil { if err := download.Do(ctx); err != nil {
return err return err
} }
*/ n++
n++ }
return nil
} }
return nil
} }
} */
_, _, _, _ = download, navRight, navRightN, firstNav
photosList := []string{
"https://photos.google.com/photo/AF1QipPMVPPg5TI2-cnAj-gDXYZL_7fG95jqNDCNb6WP",
"https://photos.google.com/photo/AF1QipOnmwDjAWN2yN1hTlrD8vxdfCdbA0mcoF8CNFm0",
"https://photos.google.com/photo/AF1QipPNNMjO3KT58o52V2WVzATr0zMKbmTQ-I2PPGyf",
}
var currentFile string _, _, _ = download, navRight, firstNav
for _, v := range photosList {
if err := chromedp.Run(ctx,
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(s.dlDir),
// TODO(mpl): add policy func over photo URL, which decides what we do (with?)
/*
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(s.dlDir),
chromedp.Navigate("https://photos.google.com/"),
chromedp.Sleep(5000*time.Millisecond),
// the `ERROR: unhandled page event *page.EventDownloadWillBegin` error does show up, but it does not actually prevent the download, so who cares?
chromedp.WaitReady("body", chromedp.ByQuery), if err := chromedp.Run(ctx,
chromedp.ActionFunc(func(ctx context.Context) error { page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(s.dlDir),
log.Printf("body is ready") // TODO(mpl): add policy func over photo URL, which decides what we do (with?)
return nil chromedp.Navigate("https://photos.google.com/"),
}), chromedp.Sleep(5000*time.Millisecond),
chromedp.ActionFunc(func(ctx context.Context) error { chromedp.WaitReady("body", chromedp.ByQuery),
if err := firstNav(ctx); err != nil { chromedp.ActionFunc(func(ctx context.Context) error {
return err log.Printf("body is ready")
} return nil
return download(ctx, s.dlDir) }),
}), chromedp.ActionFunc(firstNav),
*/ chromedp.ActionFunc(dlAndMove),
chromedp.ActionFunc(navRight),
chromedp.Navigate(v), chromedp.ActionFunc(dlAndMove),
chromedp.Sleep(5000*time.Millisecond), ); err != nil {
chromedp.WaitReady("body", chromedp.ByQuery), log.Fatal(err)
chromedp.ActionFunc(func(ctx context.Context) error {
var err error
dlFile, err := download(ctx, s.dlDir)
if err != nil {
return err
}
currentFile = dlFile
return nil
}),
chromedp.ActionFunc(func(ctx context.Context) error {
dir, err := ioutil.TempDir(s.dlDir, "")
if err != nil {
return err
}
if err := os.Rename(filepath.Join(s.dlDir, currentFile), filepath.Join(dir, currentFile)); err != nil {
return err
}
println("NEW FILE: ", filepath.Join(dir, currentFile))
return nil
}),
); err != nil {
log.Fatal(err)
}
fmt.Println("OK")
} }
fmt.Println("OK")
// Next: keys // Next: keys
// https://github.com/chromedp/chromedp/issues/400 // https://github.com/chromedp/chromedp/issues/400

Loading…
Cancel
Save