added timeout for authenticating

pull/2/merge
mpl 5 years ago
parent 6fd468be34
commit 07e75c0815

@ -38,25 +38,29 @@ func main() {
return return
} }
if !*devFlag && *startFlag != "" { if !*devFlag && *startFlag != "" {
log.Fatal("-start only allowed in dev mode") log.Print("-start only allowed in dev mode")
return
} }
s, err := NewSession() s, err := NewSession()
if err != nil { if err != nil {
log.Fatal(err) log.Print(err)
return
} }
defer s.Shutdown() defer s.Shutdown()
log.Printf("Session Dir: %v", s.profileDir) log.Printf("Session Dir: %v", s.profileDir)
if err := s.cleanDlDir(); err != nil { if err := s.cleanDlDir(); err != nil {
log.Fatal(err) log.Print(err)
return
} }
ctx, cancel := s.NewContext() ctx, cancel := s.NewContext()
defer cancel() defer cancel()
if err := login(ctx); err != nil { if err := login(ctx); err != nil {
log.Fatal(err) log.Print(err)
return
} }
if err := chromedp.Run(ctx, if err := chromedp.Run(ctx,
@ -73,7 +77,8 @@ func main() {
chromedp.ActionFunc(s.firstNav), chromedp.ActionFunc(s.firstNav),
chromedp.ActionFunc(s.navN(*nItemsFlag)), chromedp.ActionFunc(s.navN(*nItemsFlag)),
); err != nil { ); err != nil {
log.Fatal(err) log.Print(err)
return
} }
fmt.Println("OK") fmt.Println("OK")
} }
@ -201,11 +206,14 @@ func login(ctx context.Context) error {
// when we're not authenticated, the URL is actually // when we're not authenticated, the URL is actually
// https://www.google.com/photos/about/ , so we rely on that to detect when we have // https://www.google.com/photos/about/ , so we rely on that to detect when we have
// authenticated. // authenticated.
// TODO(mpl): add timeout
chromedp.ActionFunc(func(ctx context.Context) error { chromedp.ActionFunc(func(ctx context.Context) error {
time.Sleep(time.Second) time.Sleep(time.Second)
timeout := time.Now().Add(2 * time.Minute)
var location string var location string
for { for {
if time.Now().After(timeout) {
return errors.New("timeout waiting for authentication")
}
if err := chromedp.Location(&location).Do(ctx); err != nil { if err := chromedp.Location(&location).Do(ctx); err != nil {
return err return err
} }
@ -341,16 +349,16 @@ func (s Session) download(ctx context.Context, location string) (string, error)
started := false started := false
tick := 500 * time.Millisecond tick := 500 * time.Millisecond
var fileSize int64 var fileSize int64
timeout := time.Now().Add(time.Minute) deadline := time.Now().Add(time.Minute)
for { for {
time.Sleep(tick) time.Sleep(tick)
// TODO(mpl): download starts late if it's a video. figure out if dl can only // TODO(mpl): download starts late if it's a video. figure out if dl can only
// start after video has started playing or something like that? // start after video has started playing or something like that?
if !started && time.Now().After(timeout) { if !started && time.Now().After(deadline) {
return "", fmt.Errorf("downloading in %q took too long to start", s.dlDir) return "", fmt.Errorf("downloading in %q took too long to start", s.dlDir)
} }
if started && time.Now().After(timeout) { if started && time.Now().After(deadline) {
return "", fmt.Errorf("timeout while downloading in %q", s.dlDir) return "", fmt.Errorf("hit deadline while downloading in %q", s.dlDir)
} }
entries, err := ioutil.ReadDir(s.dlDir) entries, err := ioutil.ReadDir(s.dlDir)
@ -376,13 +384,13 @@ func (s Session) download(ctx context.Context, location string) (string, error)
if !started { if !started {
if len(fileEntries) > 0 { if len(fileEntries) > 0 {
started = true started = true
timeout = time.Now().Add(time.Minute) deadline = time.Now().Add(time.Minute)
} }
} }
newFileSize := fileEntries[0].Size() newFileSize := fileEntries[0].Size()
if newFileSize > fileSize { if newFileSize > fileSize {
// push back the timeout as long as we make progress // push back the timeout as long as we make progress
timeout = time.Now().Add(time.Minute) deadline = time.Now().Add(time.Minute)
fileSize = newFileSize fileSize = newFileSize
} }
if !strings.HasSuffix(fileEntries[0].Name(), ".crdownload") { if !strings.HasSuffix(fileEntries[0].Name(), ".crdownload") {

Loading…
Cancel
Save