N nav right working

pull/2/merge
mpl 5 years ago
parent 7be8a7b79e
commit 2c905fbc27

@ -14,7 +14,7 @@ import (
"time" "time"
"github.com/chromedp/cdproto/input" "github.com/chromedp/cdproto/input"
"github.com/chromedp/cdproto/page" // "github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
"github.com/chromedp/chromedp/kb" "github.com/chromedp/chromedp/kb"
"go4.org/lock" "go4.org/lock"
@ -61,11 +61,13 @@ func main() {
} }
download := chromedp.ActionFunc(func(ctx context.Context) error { download := chromedp.ActionFunc(func(ctx context.Context) error {
// TODO(mpl): instead of tempdir name, probably use dated name instead?
dir, err := ioutil.TempDir(s.dlDir, "") dir, err := ioutil.TempDir(s.dlDir, "")
if err != nil { if err != nil {
return err return err
} }
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(dir) // TODO(mpl): that does not seem to be working here. maybe we need to do it before navigating?
// page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(dir)
// TODO(mpl): cleanup dir // TODO(mpl): cleanup dir
keyD, ok := kb.Keys['D'] keyD, ok := kb.Keys['D']
@ -100,17 +102,30 @@ func main() {
return err return err
} }
defer dirfd.Close() defer dirfd.Close()
timeout := time.Now().Add(30 * time.Second) started := false
endTimeout := time.Now().Add(30 * time.Second)
startTimeout := time.Now().Add(5 * time.Second)
tick := 500 * time.Millisecond
for { for {
if time.Now().After(timeout) { time.Sleep(tick)
if time.Now().After(endTimeout) {
return fmt.Errorf("timeout while downloading in %q", dir) return fmt.Errorf("timeout while downloading in %q", dir)
} }
if !started && time.Now().After(startTimeout) {
return fmt.Errorf("downloading in %q took too long to start", dir)
}
entries, err := dirfd.Readdirnames(-1) entries, err := dirfd.Readdirnames(-1)
if err != nil { if err != nil {
return err return err
} }
if len(entries) != 1 { if len(entries) > 1 {
return fmt.Errorf("more or less than one file (%d) in download dir %q", len(entries), dir) return fmt.Errorf("more than one file (%d) in download dir %q", len(entries), dir)
}
if !started {
if len(entries) > 0 {
started = true
}
continue
} }
if !strings.HasSuffix(entries[0], ".crdownload") { if !strings.HasSuffix(entries[0], ".crdownload") {
// download is over // download is over
@ -120,15 +135,26 @@ func main() {
return nil return nil
}) })
firstNav := func(ctx context.Context) chromedp.ActionFunc {
return func(ctx context.Context) error {
chromedp.KeyEvent(kb.ArrowRight).Do(ctx)
log.Printf("sent key")
chromedp.Sleep(500 * time.Millisecond).Do(ctx)
chromedp.KeyEvent("\n").Do(ctx)
chromedp.Sleep(500 * time.Millisecond).Do(ctx)
return nil
}
}
navRight := chromedp.ActionFunc(func(ctx context.Context) error { navRight := chromedp.ActionFunc(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(500 * time.Millisecond).Do(ctx)
chromedp.KeyEvent("\n").Do(ctx)
chromedp.Sleep(500 * time.Millisecond).Do(ctx)
return nil return nil
}) })
_, _ = download, navRight
navRightN := func(N int, ctx context.Context) chromedp.ActionFunc { navRightN := func(N int, ctx context.Context) chromedp.ActionFunc {
n := 0 n := 0
return func(ctx context.Context) error { return func(ctx context.Context) error {
@ -136,14 +162,17 @@ func main() {
if n >= N { if n >= N {
break break
} }
if err := navRight.Do(ctx); err != nil { chromedp.KeyEvent(kb.ArrowRight).Do(ctx)
return err
}
chromedp.Sleep(500 * time.Millisecond).Do(ctx) chromedp.Sleep(500 * time.Millisecond).Do(ctx)
if err := download.Do(ctx); err != nil { /*
return err if err := navRight.Do(ctx); err != nil {
} return err
chromedp.Sleep(5 * time.Second) }
chromedp.Sleep(500 * time.Millisecond).Do(ctx)
if err := download.Do(ctx); err != nil {
return err
}
*/
n++ n++
} }
return nil return nil
@ -162,6 +191,7 @@ func main() {
log.Printf("body is ready") log.Printf("body is ready")
return nil return nil
}), }),
firstNav(ctx),
navRightN(5, ctx), navRightN(5, ctx),
); err != nil { ); err != nil {
log.Fatal(err) log.Fatal(err)

Loading…
Cancel
Save