Improve the url parsing
This commit is contained in:
parent
c670645300
commit
7d5ca83a36
1 changed files with 47 additions and 14 deletions
|
@ -21,7 +21,36 @@ const (
|
||||||
RESIZE_THUMB = "/usr/bin/convert -resize 60 -quality 60 "
|
RESIZE_THUMB = "/usr/bin/convert -resize 60 -quality 60 "
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func resize(folder, name, extension string) (string, string) {
|
||||||
|
imgPath := folder + name + extension
|
||||||
|
resize := append(strings.Split(RESIZE, " "), imgPath, imgPath)
|
||||||
|
cmd := exec.Command(resize[0], resize[1:]...)
|
||||||
|
cmd.Run()
|
||||||
|
imgPathSmall := folder + name + "_small" + extension
|
||||||
|
resize = append(strings.Split(RESIZE_THUMB, " "), imgPath, imgPathSmall)
|
||||||
|
cmd = exec.Command(resize[0], resize[1:]...)
|
||||||
|
cmd.Run()
|
||||||
|
return "/" + imgPath, "/" + imgPathSmall
|
||||||
|
}
|
||||||
|
|
||||||
func getCover(e *epub.Epub, path string) (string, string) {
|
func getCover(e *epub.Epub, path string) (string, string) {
|
||||||
|
folder := COVER_PATH + path[:1] + "/"
|
||||||
|
os.Mkdir(folder, os.ModePerm)
|
||||||
|
|
||||||
|
/* Try first common names */
|
||||||
|
imgPath := folder + path + ".jpg"
|
||||||
|
file, _ := os.Create(imgPath)
|
||||||
|
defer file.Close()
|
||||||
|
n, _ := file.Write(e.Data("cover.jpg"))
|
||||||
|
if n != 0 {
|
||||||
|
return resize(folder, path, ".jpg")
|
||||||
|
}
|
||||||
|
n, _ = file.Write(e.Data("cover.jpeg"))
|
||||||
|
if n != 0 {
|
||||||
|
return resize(folder, path, ".jpg")
|
||||||
|
}
|
||||||
|
defer os.Remove(imgPath)
|
||||||
|
|
||||||
exp, _ := regexp.Compile("<img.*src=[\"']([^\"']*(\\.[^\\.\"']*))[\"']")
|
exp, _ := regexp.Compile("<img.*src=[\"']([^\"']*(\\.[^\\.\"']*))[\"']")
|
||||||
it := e.Iterator(epub.EITERATOR_SPINE)
|
it := e.Iterator(epub.EITERATOR_SPINE)
|
||||||
defer it.Close()
|
defer it.Close()
|
||||||
|
@ -31,19 +60,23 @@ func getCover(e *epub.Epub, path string) (string, string) {
|
||||||
for err == nil {
|
for err == nil {
|
||||||
res := exp.FindStringSubmatch(txt)
|
res := exp.FindStringSubmatch(txt)
|
||||||
if res != nil {
|
if res != nil {
|
||||||
folder := COVER_PATH + path[:1] + "/"
|
urlPart := strings.Split(it.CurrUrl(), "/")
|
||||||
os.Mkdir(folder, os.ModePerm)
|
url := strings.Join(urlPart[:len(urlPart)-1], "/")
|
||||||
imgPath := folder + path + res[2]
|
if url == "" {
|
||||||
|
url = res[1]
|
||||||
|
} else {
|
||||||
|
url = url + "/" + res[1]
|
||||||
|
}
|
||||||
|
imgPath = folder + path + res[2]
|
||||||
f, _ := os.Create(imgPath)
|
f, _ := os.Create(imgPath)
|
||||||
f.Write(e.Data(res[1]))
|
defer f.Close()
|
||||||
resize := append(strings.Split(RESIZE, " "), imgPath, imgPath)
|
/* try to write it, if there is nothing search for other img */
|
||||||
cmd := exec.Command(resize[0], resize[1:]...)
|
n, _ = f.Write(e.Data(url))
|
||||||
cmd.Run()
|
if n != 0 {
|
||||||
imgPathSmall := folder + path + "_small" + res[2]
|
return resize(folder, path, res[2])
|
||||||
resize = append(strings.Split(RESIZE_THUMB, " "), imgPath, imgPathSmall)
|
}
|
||||||
cmd = exec.Command(resize[0], resize[1:]...)
|
panic(url) // FIXME
|
||||||
cmd.Run()
|
defer os.Remove(imgPath)
|
||||||
return "/" + imgPath, "/" + imgPathSmall
|
|
||||||
}
|
}
|
||||||
txt, err = it.Next()
|
txt, err = it.Next()
|
||||||
}
|
}
|
||||||
|
@ -73,7 +106,7 @@ func parseAuthr(creator []string) []string {
|
||||||
func parseSubject(subject []string) []string {
|
func parseSubject(subject []string) []string {
|
||||||
var res []string
|
var res []string
|
||||||
for _, s := range subject {
|
for _, s := range subject {
|
||||||
res = append(res, strings.Split(s, " / ")...) // FIXME
|
res = append(res, strings.Split(s, " / ")...)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -91,9 +124,9 @@ func keywords(b Book) (k []string) {
|
||||||
func store(coll *mgo.Collection, path string) {
|
func store(coll *mgo.Collection, path string) {
|
||||||
var book Book
|
var book Book
|
||||||
|
|
||||||
|
fmt.Println(path)
|
||||||
e, err := epub.Open(NEW_PATH+path, 0)
|
e, err := epub.Open(NEW_PATH+path, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(path)
|
|
||||||
panic(err) // TODO: do something
|
panic(err) // TODO: do something
|
||||||
}
|
}
|
||||||
defer e.Close()
|
defer e.Close()
|
||||||
|
|
Reference in a new issue