-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture.go
59 lines (47 loc) · 1.19 KB
/
picture.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
package meta
import (
"github.com/readeck/readeck/pkg/extract"
)
// ExtractPicture is a processor that extracts the picture from the document
// metadata. It has to come after ExtractMeta.
func ExtractPicture(m *extract.ProcessMessage, next extract.Processor) extract.Processor {
if m.Step() != extract.StepDom || m.Position() > 0 {
return next
}
d := m.Extractor.Drop()
if d.Meta == nil {
return next
}
href := d.Meta.LookupGet(
"x.picture_url",
"graph.image",
"twitter.image",
"oembed.thumbnail_url",
)
if href == "" {
return next
}
size := uint(800)
if d.DocumentType == "photo" {
size = 1280
}
m.Log.WithField("href", href).Debug("loading picture")
picture, err := extract.NewPicture(href, d.URL)
if err != nil {
m.Log.WithError(err).Warn()
return next
}
if err = picture.Load(m.Extractor.Client(), size, ""); err != nil {
m.Log.WithError(err).WithField("url", href).Warn("cannot load picture")
return next
}
d.Pictures["image"] = picture
m.Log.WithField("size", picture.Size[:]).Debug("picture loaded")
thumbnail, err := picture.Copy(380, "")
if err != nil {
m.Log.WithError(err).Warn()
return next
}
d.Pictures["thumbnail"] = thumbnail
return next
}