wip tracking most recent one we downloaded

and using that to skip all the ones we already have
pull/2/merge
mpl 5 years ago
parent c8ef120ae4
commit eb88c894e5

@ -99,7 +99,16 @@ func main() {
return nil return nil
} }
download := func(ctx context.Context) (string, error) { markDone := func(dldir, location string) error {
println("LOCATION: ", location)
// TODO(mpl): back up .lastdone before overwriting it, in case writing it fails.
if err := ioutil.WriteFile(filepath.Join(dldir, ".lastdone"), []byte(location), 0600); err != nil {
return err
}
return nil
}
download := func(ctx context.Context, location string) (string, error) {
dir := s.dlDir dir := s.dlDir
keyD, ok := kb.Keys['D'] keyD, ok := kb.Keys['D']
if !ok { if !ok {
@ -128,6 +137,7 @@ func main() {
} }
} }
var filename string
started := false started := false
endTimeout := time.Now().Add(30 * time.Second) endTimeout := time.Now().Add(30 * time.Second)
startTimeout := time.Now().Add(5 * time.Second) startTimeout := time.Now().Add(5 * time.Second)
@ -166,9 +176,16 @@ func main() {
} }
if !strings.HasSuffix(fileEntries[0], ".crdownload") { if !strings.HasSuffix(fileEntries[0], ".crdownload") {
// download is over // download is over
return fileEntries[0], nil filename = fileEntries[0]
break
} }
} }
if err := markDone(dir, location); err != nil {
return "", err
}
return filename, nil
} }
mvDl := func(dlFile string) func(ctx context.Context) error { mvDl := func(dlFile string) func(ctx context.Context) error {
@ -184,9 +201,9 @@ func main() {
} }
} }
dlAndMove := func(ctx context.Context) error { dlAndMove := func(ctx context.Context, location string) error {
var err error var err error
dlFile, err := download(ctx) dlFile, err := download(ctx, location)
if err != nil { if err != nil {
return err return err
} }
@ -227,6 +244,11 @@ func main() {
if N == 0 { if N == 0 {
return nil return nil
} }
var location string
lookingForLastDone := true
if s.lastDone == "" {
lookingForLastDone = false
}
for { for {
if N > 0 && n >= N { if N > 0 && n >= N {
break break
@ -240,8 +262,19 @@ func main() {
return err return err
} }
} }
if err := chromedp.Location(&location).Do(ctx); err != nil {
return err
}
if lookingForLastDone {
if location == s.lastDone {
lookingForLastDone = false
}
println(location, " ALREADY DONE")
continue
}
// TODO(mpl): deal with getting the very last photo to properly exit that loop when N < 0. // TODO(mpl): deal with getting the very last photo to properly exit that loop when N < 0.
if err := dlAndMove(ctx); err != nil { if err := dlAndMove(ctx, location); err != nil {
return err return err
} }
n++ n++
@ -304,6 +337,18 @@ type Session struct {
parentCancel context.CancelFunc parentCancel context.CancelFunc
dlDir string dlDir string
profileDir string profileDir string
lastDone string
}
func getLastDone(dlDir string) (string, error) {
data, err := ioutil.ReadFile(filepath.Join(dlDir, ".lastdone"))
if err != nil {
if !os.IsNotExist(err) {
return "", err
}
return "", nil
}
return string(data), nil
} }
func NewSession() (*Session, error) { func NewSession() (*Session, error) {
@ -327,9 +372,14 @@ func NewSession() (*Session, error) {
if err := os.MkdirAll(dlDir, 0700); err != nil { if err := os.MkdirAll(dlDir, 0700); err != nil {
return nil, err return nil, err
} }
lastDone, err := getLastDone(dlDir)
if err != nil {
return nil, err
}
s := &Session{ s := &Session{
profileDir: dir, profileDir: dir,
dlDir: dlDir, dlDir: dlDir,
lastDone: lastDone,
} }
return s, nil return s, nil
} }

Loading…
Cancel
Save