WIP disclaimer
[yt-mango.git] / apiclassic / parsedescription.go
blob43bd4c84aa4003546eda921338a32c2b7ff5b37b
1 package apiclassic
3 import (
4 "errors"
5 "golang.org/x/net/html"
6 "bytes"
7 "strings"
8 "github.com/terorie/yt-mango/util"
11 const descriptionSelector = "#eow-description"
13 func (p *parseVideoInfo) parseDescription() error {
14 // Find description root
15 descNode := p.doc.Find(descriptionSelector).First()
16 if len(descNode.Nodes) == 0 { return errors.New("could not find description") }
18 // Markdown text
19 var buffer bytes.Buffer
21 // Enumerate nodes
22 for c := descNode.Nodes[0].FirstChild; c != nil; c = c.NextSibling {
23 switch c.Type {
24 case html.TextNode:
25 // FIXME: "&amp;lt;" gets parsed to => "<"
26 // Write text to buffer, escaping markdown
27 err := util.MarkdownTextEscape.ToBuffer(c.Data, &buffer)
28 if err != nil { return err }
29 case html.ElementNode:
30 switch c.Data {
31 // Newline
32 case "br":
33 err := buffer.WriteByte(0x0a)
34 if err != nil { return err }
35 // Link
36 case "a":
37 err := parseLink(c, &buffer)
38 if err != nil { return err }
43 // Save description
44 p.v.Description = buffer.String()
46 return nil
49 func parseLink(c *html.Node, dest *bytes.Buffer) error {
50 // Find text
51 if c.FirstChild == nil { return nil } // Empty link
52 if c.FirstChild.Type != html.TextNode {
53 return errors.New("unexpected non-text node")
55 text := c.FirstChild.Data
57 // Find href
58 for _, attr := range c.Attr {
59 if attr.Key == "href" {
60 switch {
61 // hashtag
62 case strings.HasPrefix(attr.Val, "/results"):
63 dest.WriteString(text)
65 // real link
66 case strings.HasPrefix(attr.Val, "/redirect"):
68 Not needed:
69 // Decode link from href
70 link, err := decodeLink(attr.Val)
71 if err != nil { return err }
72 // Escape to markdown
73 link, err = net.MarkdownLinkEscape.ToString(link)
74 if err != nil { return err }
75 // Write to buffer
76 dest.WriteString(fmt.Sprintf("[%s](%s)\n", text, link))
78 dest.WriteString(text)
80 default:
81 return errors.New("unknown link")
83 break
86 return nil
89 /* Not needed
91 func decodeLink(href string) (string, error) {
92 url, err := url2.Parse(href)
93 if err != nil { return "", err }
95 query := url.Query()
96 link := query.Get("q")
97 if link == "" { return "", errors.New("empty link") }
99 return link, nil