This repository has been archived by the owner on Aug 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
103 lines (84 loc) · 1.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"sync"
)
// Doc defines a Yumpu document
type Doc struct {
Document struct {
url string
dir string
Title string `json:"title"`
BasePath string `json:"base_path"`
Pages []Page `json:"pages"`
}
}
// Page defines a single page within a Yumpu document
type Page struct {
Number int `json:"nr"`
Images map[string]string `json:"images"`
Qss map[string]string `json:"qss"`
}
func main() {
var d Doc
flagID := flag.String("id", "", "Yumpu document ID")
flagOut := flag.String("out", ".", "directory where to download jpeg files")
flag.Parse()
if *flagID == "" { // flag not set
log.Fatal("Yumpu document ID not set")
}
u := &url.URL{
Scheme: "https",
Host: "www.yumpu.com",
Path: path.Join("document/json2/", *flagID),
}
d.Document.url = u.String()
d.Document.dir = *flagOut
log.Printf("downloading JSON response from '%s'", d.Document.url)
res, err := http.Get(d.Document.url)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(b, &d)
var wg sync.WaitGroup
for _, p := range d.Document.Pages {
pURL := fmt.Sprintf("%s%s?%s", d.Document.BasePath, p.Images["large"], p.Qss["large"])
wg.Add(1)
go func(p Page) {
// log errors and carry on
res, err := http.Get(pURL)
if err != nil {
log.Println(err)
}
defer res.Body.Close()
pth := path.Join(d.Document.dir, fmt.Sprintf("%d.jpg", p.Number))
f, err := os.Create(pth)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// dump the response body to the file
_, err = io.Copy(f, res.Body)
if err != nil {
log.Fatal(err)
}
log.Printf("downloaded page %d", p.Number)
wg.Done()
}(p)
}
wg.Wait()
}