From ff03dfba5e91626e1eb8f2adc27576e12a9d183f Mon Sep 17 00:00:00 2001 From: Henry Lonng Date: Sun, 2 Jun 2024 18:35:46 +0800 Subject: [PATCH] autogen: using the original URL when downloading PDF fails --- autogen/autogen.go | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/autogen/autogen.go b/autogen/autogen.go index 5f99f91..10e8c98 100644 --- a/autogen/autogen.go +++ b/autogen/autogen.go @@ -242,23 +242,32 @@ func convert(opt *options) error { // Download the paper is not exists if _, err := os.Stat(path); os.IsNotExist(err) { - paper, err := http.Get(r.url) - if err != nil { - return err + fetch := func() error { + paper, err := http.Get(r.url) + if err != nil { + return err + } + + fmt.Println("Download Paper", r.title) + fmt.Println(padding(2), "url", r.url) + fmt.Println(padding(2), "path", path) + + data, err := io.ReadAll(paper.Body) + if err != nil { + return err + } + + // Write to file + if err := os.WriteFile(path, data, 0644); err != nil { + return err + } + + return nil } - fmt.Println("Download Paper", r.title) - fmt.Println(padding(2), "url", r.url) - fmt.Println(padding(2), "path", path) - - data, err := io.ReadAll(paper.Body) - if err != nil { - return err - } - - // Write to file - if err := os.WriteFile(path, data, 0644); err != nil { - return err + if err := fetch(); err != nil { + fmt.Println(padding(2), "failed to download paper", err) + url = r.url } }