Created
April 5, 2020 22:49
-
-
Save sug0/214927847bb27e62cc2b3ae6fb1254d2 to your computer and use it in GitHub Desktop.
Downloader for Adobe InDesign's online reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is targeting windows specifically, because I made this for my brother. | |
| // If you need to use this abomination yourself, be sure to edit the code accordingly. | |
| package main | |
| import ( | |
| "os" | |
| "log" | |
| "fmt" | |
| "sync" | |
| "bufio" | |
| "errors" | |
| "regexp" | |
| "os/exec" | |
| "net/http" | |
| ) | |
| var R = regexp.MustCompile(`https://indd.adobe.com/view/(.*)`) | |
| const ( | |
| wkhtmltopdf = "./wkhtmltopdf.exe" | |
| urlFmt0 = "https://adobeindd.com/view/publications/%s/1/publication.html" | |
| urlFmt1 = "https://adobeindd.com/view/publications/%s/1/publication-%d.html" | |
| ) | |
| func main() { | |
| err := os.Mkdir("out", 0777) | |
| if err != nil && !errors.Is(err, os.ErrExist) { | |
| panic(err) | |
| } | |
| f, err := os.Open(os.Args[1]) | |
| if err != nil { | |
| panic(err) | |
| } | |
| defer f.Close() | |
| var wg sync.WaitGroup | |
| sem := make(chan struct{}, 4) | |
| for url := range readLines(f) { | |
| matches := R.FindStringSubmatch(url) | |
| if matches == nil { | |
| continue | |
| } | |
| id := matches[1] | |
| log.Printf("%s: Collecting HTML pages.\n", id) | |
| argList := []string{"-s", "A5"} | |
| var page int | |
| for { | |
| if page == 0 { | |
| url = fmt.Sprintf(urlFmt0, id) | |
| } else { | |
| url = fmt.Sprintf(urlFmt1, id, page) | |
| } | |
| rsp, err := http.Head(url) | |
| if err != nil || rsp.StatusCode != 200 { | |
| log.Printf("%s: Last page found.\n", id) | |
| break | |
| } | |
| log.Printf("%s: Including page %d.\n", id, page) | |
| argList = append(argList, url) | |
| page++ | |
| } | |
| if len(argList) == 2 { | |
| log.Printf("%s: Nothing to download.\n", id) | |
| continue | |
| } | |
| argList = append(argList, fmt.Sprintf(`out\%s.pdf`, id)) | |
| sem <- struct{}{} | |
| wg.Add(1) | |
| log.Printf("%s: Downloading pages and converting to PDF.\n", id) | |
| go func(id string, argList []string) { | |
| err := exec.Command(wkhtmltopdf, argList...).Run() | |
| if err != nil { | |
| log.Printf("%s: PDF conversion failed. Reason: %s\n", id, err) | |
| } | |
| wg.Done() | |
| <-sem | |
| }(id, argList) | |
| } | |
| wg.Wait() | |
| } | |
| func readLines(f *os.File) <-chan string { | |
| ch := make(chan string, 16) | |
| go func() { | |
| r := bufio.NewReader(f) | |
| for { | |
| url, err := r.ReadString('\n') | |
| if err != nil { | |
| close(ch) | |
| return | |
| } | |
| ch <- url[:len(url)-2] | |
| } | |
| }() | |
| return ch | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how it work