aboutsummaryrefslogtreecommitdiff
path: root/parser.go
blob: 38802ed485c82d181251bb1e24d97483ccb4bb98 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main

import (
	"io"
	"net/url"
	"strconv"
	"strings"
	"time"

	"golang.org/x/net/html"
)

// parse takes an io.Reader which is supposed to be an entire web page (like http.Response.Body)
// and returns the separate posts found inside.
func parse(r io.Reader) (c *channel, err error) {
	c = &channel{}
	doc, err := html.Parse(r)
	if err != nil {
		return
	}

	var f func(*html.Node)
	f = func(n *html.Node) {
		if n.Type == html.ElementNode && n.Data == "meta" {
			var prop string
			for _, a := range n.Attr {
				if a.Key == "property" {
					prop = a.Val
				}
				if a.Key == "content" {
					switch prop {
					case "og:title":
						c.Title = a.Val
					case "og:description":
						c.Description = a.Val
					}
				}
			}

		}

		if n.Type == html.ElementNode && n.Data == "div" {
			for _, a := range n.Attr {
				if a.Key == "class" && strings.Contains(a.Val, "userContentWrapper") {
					var p post
					parseNode(n, &p)
					c.Items = append(c.Items, &p)
					break
				}
			}
		}

		// loop further down
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			f(c)
		}
	}
	f(doc)

	return
}

func parseNode(n *html.Node, p *post) {
	if n.Type == html.ElementNode {
		switch n.Data {
		// fetch time stamp from header
		case "abbr":
			for _, attr := range n.Attr {
				if attr.Key == "data-utime" {
					unix, _ := strconv.ParseInt(attr.Val, 10, 64)
					p.Time = time.Unix(unix, 0)
					return
				}
			}

		// skip post header with name of group and check-in
		case "h5":
			return

		// skip "show more", aria-hidden and accessible stuff
		case "span":
			fallthrough
		case "a":
			fallthrough
		case "div":
			for _, attr := range n.Attr {
				if attr.Key == "class" && attr.Val == "text_exposed_hide" {
					return
				}
				if attr.Key == "class" && strings.Contains(attr.Val, "accessible_elem") {
					return
				}

				if attr.Key == "aria-hidden" && attr.Val == "true" {
					return
				}
			}

		// parse paragraphs
		case "p":
			parseParagraph(n, p)
			p.Content += "\n\n"
			return

		// get images
		case "img":
			parseImage(n, p)
			return
		}
	}

	// fetch all pure text elements outside proper post paragraphs
	if n.Type == html.TextNode {
		p.Content += n.Data
		if n.Parent.Type == html.ElementNode && n.Parent.Data == "a" {
			parseLink(n.Parent, p)
		}
		p.Content += "\n"
	}

	// loop deeper
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		parseNode(c, p)
	}
}

func parseParagraph(n *html.Node, p *post) {
	// fetch all pure text elements
	if n.Type == html.TextNode {
		p.Content += n.Data
		if n.Parent.Type == html.ElementNode && n.Parent.Data == "a" {
			parseLink(n.Parent, p)
			return
		}
	}

	// normal linebreak within paragraphs
	if n.Type == html.ElementNode && n.Data == "br" {
		p.Content += "\n"
		return
	}

	// loop deeper
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		parseParagraph(c, p)
	}
}

func parseImage(n *html.Node, p *post) {
	img := &image{}
	for _, attr := range n.Attr {
		switch attr.Key {
		case "src":
			img.Source = attr.Val
		case "alt":
			fallthrough
		case "title":
			fallthrough
		case "aria-label":
			img.Caption = attr.Val
		}
	}
	p.Images = append(p.Images, img)
}

func parseLink(n *html.Node, p *post) {
	for _, attr := range n.Attr {
		switch attr.Key {
		case "href":
			urlraw, _ := url.PathUnescape(attr.Val)
			url, _ := url.Parse(urlraw)
			external := url.Query().Get("u")
			if external != "" {
				p.Content += " [" + external + "]"
			} else {
				base := strings.Split(urlraw, "?")[0]
				if base[0] == '/' {
					base = "https://www.facebook.com" + base
				}
				p.Content += " [" + base + "]"
			}
		}
	}
}