-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
95 lines (75 loc) · 1.89 KB
/
helpers.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
package main
import (
"bytes"
"io"
"io/ioutil"
//"fmt"
"net/http"
"os"
"text/template"
"github.com/PuerkitoBio/goquery"
)
func GetResponse(url string) *http.Response {
resp, err := http.Get(url)
if err != nil { panic(err) }
// TODO: check response status
return resp
}
func GetHTML(url string)[]byte{
resp := GetResponse(url)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}
func GetDocument(url string) (*goquery.Document, error){
resp := GetResponse(url)
doc, err := goquery.NewDocumentFromReader(resp.Body)
return doc, err
}
func GetImagesSrcList(url string)[]string{
doc, err := GetDocument(url)
if err != nil { panic(err) }
slice := make([]string, 0)
doc.Find("img").Each(func(i int, s *goquery.Selection){
imgUrl, _ := s.Attr("src")
slice = append(slice, imgUrl)
})
return slice
}
func CopyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil { return err }
out, err := os.Create(dst)
if err != nil { return err }
_, err = io.Copy(out, in)
if err != nil { return err }
in.Close()
out.Close()
return nil
}
func renderStandardTemplate(ctx StdComicTemplateCtx)string{
templ, err := template.New("segment").Parse(SEGMENT_TEMPLATE)
if err != nil { panic(err) }
var rendered bytes.Buffer
err = templ.Execute(&rendered, ctx)
if err != nil { panic(err) }
return rendered.String()
}
func renderStd(comic Comic, imgSrc string, title string, errMsg string)(string){
ctx := StdComicTemplateCtx{comic, imgSrc, title, errMsg}
return renderStandardTemplate(ctx)
}
func writeCssFile(targetDirName string){
targetFilePath := targetDirName + "comics.css"
// check if file already exists
if _, err := os.Stat(targetFilePath); err == nil {
return
}
// creata a file
outFile, err := os.Create(targetFilePath)
if err != nil { panic(err) }
defer outFile.Close()
// write
outFile.WriteString(CSS)
outFile.Sync()
}