forked from bolknote/NarchTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgodownloader.go
120 lines (92 loc) · 2.82 KB
/
cgodownloader.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
r "regexp"
"fmt"
"flag"
"io"
"io/ioutil"
"strings"
"net/http"
"os"
"runtime"
"path"
"time"
)
const url = `http://%s/Pages/ImageFile.ashx?level=10&x=0&y=0&tileOverlap=1024&id=%s&page=0&XHDOC=&archiveId=1`
const defaulthost="cgaso.regsamarh.ru"
func copyUrlToFile(url, filename string) bool {
if resp, err := http.Get(url); err == nil {
defer resp.Body.Close()
if w, err := os.OpenFile(filename, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666); err == nil {
defer w.Close()
if _, err := io.Copy(w, resp.Body); err == nil {
return true
}
}
}
return false
}
func readFile(name string) []byte {
if bytes, err := ioutil.ReadFile(name); err == nil {
return bytes
}
return nil
}
func getArray(content []byte) (array []string) {
re, _ := r.Compile(`(?m)^dxo\.itemsValue=\[('[\w-]{3,}.*?')\];$`)
matches := re.FindSubmatch(content)
if len(matches) > 0 {
array = strings.Split(string(matches[1]), ",")
for index, value := range array {
array[index] = strings.Trim(value, "'")
}
}
return
}
func main() {
N := runtime.NumCPU() * 2
dir := flag.String("dir", ".", "directory to output.")
host := flag.String("host", defaulthost, "address of e-archive")
from := flag.Int("from", 0, "number of start page")
to := flag.Int("to", 0, "number of end page (default - until the end)")
flag.Parse()
if flag.NArg() != 1 {
selfname := path.Base(os.Args[0])
fmt.Println("Usage: " + selfname +
" [-dir=<output dir>] [-host=<e-archive address>] [-from=<start page>] [-to=<end page>] <filename>")
flag.PrintDefaults()
os.Exit(0)
}
name := flag.Arg(0)
os.MkdirAll(*dir, 0777)
runtime.GOMAXPROCS(N + 1)
if content := readFile(name); content != nil {
ch := make(chan byte, N)
documents := getArray(content)
length := len(documents)
if *to == 0 || *to >= length {
*to = length - 1
}
if *to < *from {
fmt.Println("Error: <to> cannot be less than <from>")
os.Exit(1)
}
if *to != 0 || *from != 0 {
fmt.Printf("Found %d documents (%d to go).\n", length, *to-*from)
} else {
fmt.Printf("Found %d documents.\n", length)
}
for i := *from; i <= *to; i++ {
ch <- 1
go func(id string, index int) {
name := fmt.Sprintf("%05d.jpg", index)
copyUrlToFile(fmt.Sprintf(url, *host, id), *dir + "/" + name)
fmt.Println(name)
<-ch
}(documents[i], i)
}
for len(ch) > 0 {
time.Sleep(100 * time.Millisecond)
}
}
}