/
facebook.go
245 lines (218 loc) · 5.8 KB
/
facebook.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package mlxscraper
import (
"encoding/json"
"log"
"net/http"
"sort"
"time"
"github.com/PuerkitoBio/goquery"
cache "github.com/patrickmn/go-cache"
"github.com/underlx/disturbancesmlx/types"
)
// FacebookScraper is an announcement scraper for the Metro de Lisboa Facebook page
// It reads the Facebook feed using the Facebook API
type FacebookScraper struct {
ticker *time.Ticker
stopChan chan struct{}
log *log.Logger
newAnnCallback func(announcement *types.Announcement)
firstUpdate bool
announcements []*types.Announcement
running bool
imageURLcache *cache.Cache
AccessToken string
Network *types.Network
Period time.Duration
}
// ID returns the ID of this scraper
func (sc *FacebookScraper) ID() string {
return "sc-pt-ml-facebook"
}
// Init initializes the scraper
func (sc *FacebookScraper) Init(log *log.Logger,
newAnnCallback func(announcement *types.Announcement)) {
sc.stopChan = make(chan struct{})
sc.ticker = time.NewTicker(sc.Period)
sc.log = log
sc.newAnnCallback = newAnnCallback
sc.firstUpdate = true
sc.imageURLcache = cache.New(12*time.Hour, 30*time.Minute)
sc.log.Println("FacebookScraper initializing")
sc.update()
sc.log.Println("FacebookScraper completed first fetch")
}
// Begin starts the scraper
func (sc *FacebookScraper) Begin() {
sc.stopChan = make(chan struct{})
sc.ticker = time.NewTicker(sc.Period)
sc.running = true
go sc.scrape()
}
// End stops the scraper
func (sc *FacebookScraper) End() {
sc.ticker.Stop()
close(sc.stopChan)
sc.running = false
}
// Running returns whether the scraper is running
func (sc *FacebookScraper) Running() bool {
return sc.running
}
func (sc *FacebookScraper) copyAnnouncements() []*types.Announcement {
c := make([]*types.Announcement, len(sc.announcements))
for i, annPointer := range sc.announcements {
ann := *annPointer
c[i] = &ann
}
return c
}
func (sc *FacebookScraper) scrape() {
sc.update()
sc.log.Println("FacebookScraper completed second fetch")
for {
select {
case <-sc.ticker.C:
sc.update()
sc.log.Println("FacebookScraper fetch complete")
case <-sc.stopChan:
return
}
}
}
func (sc *FacebookScraper) update() {
response, err := http.Get("https://mobile.facebook.com/metrolisboa")
if err != nil {
sc.log.Println(err)
return
}
doc, err := goquery.NewDocumentFromResponse(response)
if err != nil {
sc.log.Println(err)
return
}
recent := doc.Find("#recent")
if recent.Length() == 0 {
sc.log.Println("Missing elements in response")
return
}
recent = recent.Children().First()
if recent.Length() == 0 {
sc.log.Println("Missing elements in response")
return
}
recent = recent.Children().First()
if recent.Length() == 0 {
sc.log.Println("Missing elements in response")
return
}
announcements := []*types.Announcement{}
recent.Children().Each(func(i int, s *goquery.Selection) {
dataft, ok := s.Attr("data-ft")
if !ok {
sc.log.Println("Post missing data-ft attribute")
}
var data map[string]interface{}
if err := json.Unmarshal([]byte(dataft), &data); err != nil {
sc.log.Println(err)
return
}
if data == nil {
sc.log.Println("data-ft is not valid json")
return
}
postIDiface := data["mf_story_key"]
if postIDiface == nil {
sc.log.Println("data-ft missing mf_story_key")
return
}
postID, ok := postIDiface.(string)
if !ok {
sc.log.Println("mf_story_key has unexpected type")
return
}
pageInsights := data["page_insights"]
if pageInsights == nil {
sc.log.Println("data-ft missing page_insights")
return
}
pageInsightsMap, ok := pageInsights.(map[string]interface{})
if !ok {
sc.log.Println("page_insights has unexpected type")
return
}
var postTime time.Time
for _, item := range pageInsightsMap {
// we only care about the item which has publish_time, we don't care what's its key
pi, ok := item.(map[string]interface{})
if !ok {
continue
}
postContextIface, ok := pi["post_context"]
if !ok {
continue
}
postContext, ok := postContextIface.(map[string]interface{})
if !ok {
continue
}
tIface, ok := postContext["publish_time"]
if !ok {
continue
}
tfloat, ok := tIface.(float64)
if !ok {
continue
}
postTime = time.Unix(int64(tfloat), 0)
}
if postTime.IsZero() {
sc.log.Println("could not obtain post publish time")
return
}
body := s.Find("div:nth-child(2) > span").First().Find("p").Text()
imgURL, _ := s.Find("div:nth-child(3) img").First().Attr("src")
ann := types.Announcement{
Time: postTime,
Network: sc.Network,
Title: "",
Body: body,
ImageURL: imgURL,
URL: "https://www.facebook.com/MetroLisboa/posts/" + postID,
Source: "pt-ml-facebook",
}
announcements = append(announcements, &ann)
})
sort.SliceStable(announcements, func(i, j int) bool {
return announcements[i].Time.Before(announcements[j].Time)
})
if !sc.firstUpdate && len(announcements) > 0 {
isNew := false
curLast := announcements[len(announcements)-1]
if len(sc.announcements) == 0 {
isNew = true
} else {
// decide if an announcement is new by looking only into the published date
prevLast := sc.announcements[len(sc.announcements)-1]
if curLast.Time.After(prevLast.Time) {
isNew = true
}
}
if isNew {
sc.newAnnCallback(curLast)
}
}
sc.announcements = announcements
sc.firstUpdate = false
}
// Networks returns the networks monitored by this scraper
func (sc *FacebookScraper) Networks() []*types.Network {
return []*types.Network{sc.Network}
}
// Sources returns the sources provided by this scraper
func (sc *FacebookScraper) Sources() []string {
return []string{"pt-ml-facebook"}
}
// Announcements returns the announcements read by this scraper
func (sc *FacebookScraper) Announcements(source string) []*types.Announcement {
return sc.copyAnnouncements()
}