1
0

embeds.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package clean
  2. import (
  3. "net/url"
  4. "regexp"
  5. "strings"
  6. "golang.org/x/net/html"
  7. "golang.org/x/net/html/atom"
  8. )
  9. // Prepare edits the downloaded page before the article is extracted:
  10. //
  11. // - It makes every link absolute. go-trafilatura resolves relative links
  12. // as if the page URL were a folder ("../faq" on /docs/config becomes
  13. // /docs/faq instead of /faq), so they must be absolute before it runs.
  14. // - It fixes images for go-trafilatura, which only looks at src; see
  15. // prepareImages.
  16. // - It replaces embedded YouTube and Vimeo players, which can't play on an
  17. // e-reader and would otherwise vanish without a trace, with a paragraph
  18. // linking to the video.
  19. func Prepare(doc *html.Node, base *url.URL) {
  20. resolveLinks(doc, base)
  21. prepareImages(doc, base)
  22. var iframes []*html.Node
  23. walk(doc, func(n *html.Node) {
  24. if n.DataAtom == atom.Iframe {
  25. iframes = append(iframes, n)
  26. }
  27. })
  28. for _, f := range iframes {
  29. link, ok := videoLink(attr(f, "src"), base)
  30. if !ok || f.Parent == nil {
  31. continue
  32. }
  33. label := strings.TrimSpace(attr(f, "title"))
  34. if label == "" {
  35. label = link
  36. }
  37. f.Parent.InsertBefore(videoParagraph(link, label), f)
  38. f.Parent.RemoveChild(f)
  39. }
  40. }
  41. var (
  42. youTubeEmbed = regexp.MustCompile(`^(?:www\.)?youtube(?:-nocookie)?\.com$`)
  43. youTubeID = regexp.MustCompile(`^/embed/([A-Za-z0-9_-]{6,})`)
  44. vimeoID = regexp.MustCompile(`^/video/([0-9]+)`)
  45. )
  46. // videoLink turns an embedded player's URL into the video's watch page.
  47. func videoLink(src string, base *url.URL) (string, bool) {
  48. u, err := base.Parse(src)
  49. if err != nil {
  50. return "", false
  51. }
  52. switch {
  53. case youTubeEmbed.MatchString(u.Host):
  54. if m := youTubeID.FindStringSubmatch(u.Path); m != nil {
  55. return "https://www.youtube.com/watch?v=" + m[1], true
  56. }
  57. case u.Host == "player.vimeo.com":
  58. if m := vimeoID.FindStringSubmatch(u.Path); m != nil {
  59. return "https://vimeo.com/" + m[1], true
  60. }
  61. }
  62. return "", false
  63. }
  64. // videoParagraph builds <p>▶ Video: <a href="link">label</a></p>.
  65. func videoParagraph(link, label string) *html.Node {
  66. a := &html.Node{Type: html.ElementNode, Data: "a", DataAtom: atom.A,
  67. Attr: []html.Attribute{{Key: "href", Val: link}}}
  68. a.AppendChild(&html.Node{Type: html.TextNode, Data: label})
  69. p := &html.Node{Type: html.ElementNode, Data: "p", DataAtom: atom.P}
  70. p.AppendChild(&html.Node{Type: html.TextNode, Data: "▶ Video: "})
  71. p.AppendChild(a)
  72. return p
  73. }
  74. func attr(n *html.Node, key string) string {
  75. for _, a := range n.Attr {
  76. if a.Key == key {
  77. return a.Val
  78. }
  79. }
  80. return ""
  81. }