forked from mediahome/javscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.go
130 lines (119 loc) · 2.65 KB
/
output.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
121
122
123
124
125
126
127
128
129
130
package scrape
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/goextension/log"
)
// DefaultOutputPath ...
var DefaultOutputPath = "video"
func copyCache(cache *Cache, msg *Content, sample bool, output string) (e error) {
pid := filepath.Join(output, strings.ToUpper(msg.ID), "."+msg.From)
e = copyFile(cache, msg.Poster, filepath.Join(pid, "poster"))
if e != nil {
return e
}
e = copyFile(cache, msg.Thumb, filepath.Join(pid, "thumb"))
if e != nil {
return e
}
for _, act := range msg.Actors {
e = copyFile(cache, act.Image, filepath.Join(pid, ".actor", act.Name))
if e != nil {
return e
}
}
if sample {
for _, s := range msg.Sample {
e = copyFile(cache, s.Image, filepath.Join(pid, ".sample", "sample"+"@"+strconv.Itoa(s.Index)))
if e != nil {
return e
}
e = copyFile(cache, s.Thumb, filepath.Join(pid, ".thumb", "thumb"+"@"+strconv.Itoa(s.Index)))
if e != nil {
return e
}
}
}
return nil
}
func copyInfo(msg *Content, path string, name string) error {
pid := filepath.Join(path, strings.ToUpper(msg.ID))
inf := filepath.Join(pid, "."+msg.From+name)
_ = os.MkdirAll(filepath.Dir(inf), os.ModePerm)
info, e := os.Stat(inf)
if e != nil && !os.IsNotExist(e) {
return e
}
if e == nil && info.Size() != 0 {
return nil
}
bytes, e := json.MarshalIndent(msg, "", " ")
if e != nil {
return e
}
return ioutil.WriteFile(inf, bytes, 0755)
}
func copyFile(cache *Cache, source, path string) error {
if source == "" {
return nil
}
path = TrimEnd(path)
if debug {
log.Infow("copy", "dir", filepath.Dir(path), "path", path)
}
_ = os.MkdirAll(filepath.Dir(path), os.ModePerm)
info, e := os.Stat(path + Ext(source))
if e != nil && !os.IsNotExist(e) {
return e
}
if e == nil && info.Size() != 0 {
return nil
}
bys, e := cache.GetBytes(source)
if e != nil {
return e
}
return ioutil.WriteFile(path+Ext(source), bys, 0755)
}
func imageCache(cache *Cache, m Content, sample bool) (e error) {
path := make(chan string, 2)
go func(path chan<- string) {
defer close(path)
path <- m.Poster
path <- m.Thumb
for _, act := range m.Actors {
path <- act.Image
}
if sample {
for _, s := range m.Sample {
path <- s.Image
path <- s.Thumb
}
}
}(path)
for p := range path {
if p != "" {
_, err := cache.Get(p)
if err != nil && !os.IsExist(err) {
log.Error(err)
}
}
}
return nil
}
// TrimEnd ...
func TrimEnd(source string) string {
return strings.Split(source, "?")[0]
}
// Ext ...
func Ext(source string) string {
ext := filepath.Ext(TrimEnd(source))
if debug {
log.Infow("ext", "source", source, "ext", ext)
}
return ext
}