works with several runs

set same dl dir at each iteration,
and move file to other dir afterwards
pull/2/merge
mpl 5 years ago
parent ed55198ec4
commit 9f9c1f4748

@ -20,6 +20,25 @@ import (
var startFlag = flag.String("start", "", "skip all the photos more recent than the one at that URL") var startFlag = flag.String("start", "", "skip all the photos more recent than the one at that URL")
func (s *Session) cleanDlDir() error {
if s.dlDir == "" {
return nil
}
entries, err := ioutil.ReadDir(s.dlDir)
if err != nil {
return err
}
for _, v := range entries {
if v.IsDir() {
continue
}
if err := os.Remove(filepath.Join(s.dlDir, v.Name())); err != nil {
return err
}
}
return nil
}
func main() { func main() {
flag.Parse() flag.Parse()
s, err := NewSession() s, err := NewSession()
@ -32,6 +51,10 @@ func main() {
// s.fixPreferences() // s.fixPreferences()
if err := s.cleanDlDir(); err != nil {
log.Fatal(err)
}
ctx, cancel := s.NewContext() ctx, cancel := s.NewContext()
defer cancel() defer cancel()
@ -59,7 +82,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
download := func(ctx context.Context, dir string) error { download := func(ctx context.Context, dir string) (string, error) {
keyD, ok := kb.Keys['D'] keyD, ok := kb.Keys['D']
if !ok { if !ok {
log.Fatal("NO D KEY") log.Fatal("NO D KEY")
@ -83,7 +106,7 @@ func main() {
for _, ev := range []*input.DispatchKeyEventParams{&down, &up} { for _, ev := range []*input.DispatchKeyEventParams{&down, &up} {
log.Printf("Event: %+v", *ev) log.Printf("Event: %+v", *ev)
if err := ev.Do(ctx); err != nil { if err := ev.Do(ctx); err != nil {
return err return "", err
} }
} }
@ -94,55 +117,47 @@ func main() {
startTimeout := time.Now().Add(5 * time.Second) startTimeout := time.Now().Add(5 * time.Second)
tick := 500 * time.Millisecond tick := 500 * time.Millisecond
for { for {
foo := func() (bool, error) { time.Sleep(tick)
dirfd, err := os.Open(dir) println("TICK")
if err != nil { if time.Now().After(endTimeout) {
return false, err return "", fmt.Errorf("timeout while downloading in %q", dir)
} }
defer dirfd.Close()
time.Sleep(tick) if !started && time.Now().After(startTimeout) {
println("TICK") return "", fmt.Errorf("downloading in %q took too long to start", dir)
if time.Now().After(endTimeout) { }
return false, fmt.Errorf("timeout while downloading in %q", dir) entries, err := ioutil.ReadDir(dir)
} if err != nil {
if !started && time.Now().After(startTimeout) { return "", err
return false, fmt.Errorf("downloading in %q took too long to start", dir) }
} var fileEntries []string
entries, err := dirfd.Readdirnames(-1) for _, v := range entries {
if err != nil { if v.IsDir() {
return false, err continue
}
if len(entries) < 1 {
return false, nil
}
if len(entries) > 1 {
for _, v := range entries {
println(v)
}
return false, fmt.Errorf("more than one file (%d) in download dir %q", len(entries), dir)
}
if !started {
if len(entries) > 0 {
started = true
}
return false, nil
} }
println(entries[0]) fileEntries = append(fileEntries, v.Name())
if !strings.HasSuffix(entries[0], ".crdownload") { }
// download is over if len(fileEntries) < 1 {
return true, nil continue
}
if !started {
if len(fileEntries) > 0 {
started = true
} }
return false, nil continue
} }
done, err := foo() if len(fileEntries) > 1 {
if err != nil { for _, v := range entries {
return err println(v)
}
return "", fmt.Errorf("more than one file (%d) in download dir %q", len(fileEntries), dir)
} }
if done { println(fileEntries[0])
break if !strings.HasSuffix(fileEntries[0], ".crdownload") {
// download is over
return fileEntries[0], nil
} }
} }
return nil
} }
// firstNav := func(ctx context.Context) chromedp.ActionFunc { // firstNav := func(ctx context.Context) chromedp.ActionFunc {
@ -196,12 +211,10 @@ func main() {
"https://photos.google.com/photo/AF1QipPNNMjO3KT58o52V2WVzATr0zMKbmTQ-I2PPGyf", "https://photos.google.com/photo/AF1QipPNNMjO3KT58o52V2WVzATr0zMKbmTQ-I2PPGyf",
} }
var currentDir string var currentFile string
for _, v := range photosList { for _, v := range photosList {
if err := chromedp.Run(ctx, if err := chromedp.Run(ctx,
// This one here works, so why not below?? page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(s.dlDir),
// page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath("/Users/mpl/Downloads/pk-gphotos"),
// TODO(mpl): change dl dir for each photo, to detect it's finished downloading.
// TODO(mpl): add policy func over photo URL, which decides what we do (with?) // TODO(mpl): add policy func over photo URL, which decides what we do (with?)
/* /*
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(s.dlDir), page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(s.dlDir),
@ -222,47 +235,29 @@ func main() {
}), }),
*/ */
chromedp.Navigate(v),
chromedp.Sleep(5000*time.Millisecond),
chromedp.WaitReady("body", chromedp.ByQuery),
chromedp.ActionFunc(func(ctx context.Context) error { chromedp.ActionFunc(func(ctx context.Context) error {
dir, err := ioutil.TempDir(s.dlDir, "") var err error
dlFile, err := download(ctx, s.dlDir)
if err != nil { if err != nil {
return err return err
} }
currentDir = dir currentFile = dlFile
return nil return nil
}), }),
chromedp.ActionFunc(func(ctx context.Context) error { chromedp.ActionFunc(func(ctx context.Context) error {
println("CURRENTDIR: ", currentDir) dir, err := ioutil.TempDir(s.dlDir, "")
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(currentDir) 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 return nil
}), }),
// WTF IS 2019/08/18 00:49:30 downloadPath not provided (-32000)
// maybe wrong chmod on the dir?? nope.
// page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(foo),
chromedp.Navigate(v),
chromedp.Sleep(5000*time.Millisecond),
chromedp.WaitReady("body", chromedp.ByQuery),
chromedp.ActionFunc(func(ctx context.Context) error {
return download(ctx, currentDir)
}),
/*
chromedp.ActionFunc(func(ctx context.Context) error {
// TODO(mpl): instead of tempdir name, probably use dated name instead?
dir, err := ioutil.TempDir(s.dlDir, "")
if err != nil {
return err
}
// TODO(mpl): that does not seem to be working here. maybe we need to do it before navigating?
// Maybe if we put each nav+download into their own chromedp.Run ?
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(dir)
// TODO(mpl): cleanup dir
if err := firstNav(ctx); err != nil {
return err
}
return download(ctx, dir)
}),
navRightN(5, ctx),
*/
); err != nil { ); err != nil {
log.Fatal(err) log.Fatal(err)
} }

Loading…
Cancel
Save