~thewhodidthis/longform

longform/main.go -rw-r--r-- 6.0 KiB
d9b0d2fbthewhodidthis git: MXXII remake 3 days ago
                                                                                
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Long form tracks arab spring related tweets.
package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strings"
	"time"
)

// ENDPOINT is the streaming resource to be fetching data from.
const ENDPOINT = "https://api.twitter.com/2/tweets/search/stream"

// PORT is the default port number. Use the `-p` flag to override.
const PORT = "8001"

// KEYWORDS is the list of search terms to be filtering against.
var KEYWORDS = []string{
	"beirut",
	"palestine",
	"tunisia",
	"egypt",
	"yemen",
	"libya",
	"syria",
}

// Data contains fields found across tweets and rules responses.
type Data struct {
	ID   string `json:"id"`
	Tag  string `json:"tag"`
	Text string `json:"text"`
}

// Error allows for parsing errors embedded in an otherwise successful response.
type Error struct {
	Message string `json:"message"`
}

// Meta contains extra fields for parsing rules responses.
type Meta struct {
	Count int `json:"result_count"`
}

// Tracker implements the http Handler interface and keeps
// track of active connections.
type Tracker struct {
	Clients map[string]chan string
	Delay   time.Duration
	Idle    bool
	Suspend chan bool
	Resume  chan bool
}

func (t *Tracker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if !strings.HasSuffix(r.URL.Path, "/io") {
		http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
		log.Printf("ServeHTTP: odd path: %s", r.URL.Path)

		return
	}

	// Need to manually flush the response writer for SSE to work.
	f, ok := w.(http.Flusher)

	if !ok {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

		return
	}

	if a := r.Header.Get("Accept"); a != "text/event-stream" {
		http.Error(w, http.StatusText(http.StatusNotAcceptable), http.StatusNotAcceptable)

		return
	}

	if o := r.Header.Get("Origin"); strings.Contains(o, "localhost") {
		w.Header().Set("Access-Control-Allow-Origin", o)
	}

	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")
	w.Header().Set("Content-Type", "text/event-stream")

	c := make(chan string)

	defer func() {
		delete(t.Clients, r.RemoteAddr)
		t.Suspend <- true
	}()

	t.Clients[r.RemoteAddr] = c
	t.Resume <- true

L:
	for {
		select {
		case d := <-c:
			fmt.Fprintf(w, "retry: 10000\ndata: %s\n\n", d)
			f.Flush()
		case <-r.Context().Done():
			break L
		}
	}
}

// Stream tells the tracker to call ENDPOINT for collecting data.
func (t *Tracker) Stream() {
	defer func() {
		// Free up for calling again.
		t.Idle = true
	}()

	// Set busy state.
	t.Idle = false

	// Attempt to contact the streaming resource.
	res, err := fetch(http.MethodGet, ENDPOINT, nil)

	if err != nil {
		log.Printf("tracker: failed to connect / %v", err)

		// Calculate back off delay, wait as much and try again.
		if t.Delay == 0 {
			t.Delay = 1 * time.Minute
		} else {
			t.Delay = 2 * t.Delay
		}

		time.Sleep(t.Delay)
		t.Resume <- true

		return
	}

	// Safe to reset since the request seems to have went through OK.
	t.Delay = 0

	defer res.Body.Close()
	dec := json.NewDecoder(res.Body)

M:
	for {
		var msg struct {
			Data  Data
			Error Error `json:"error"`
		}

		err := dec.Decode(&msg)
		if err != nil {
			// Done streaming.
			if err == io.EOF {
				break
			}

			log.Fatalf("tracker: failed to parse / %v", err)
		}

		if msg.Error.Message != "" {
			log.Printf("tracker: API error: %v", msg.Error.Message)

			break
		}

		select {
		case <-t.Suspend:
			if len(t.Clients) == 0 {
				break M
			}
		default:
			for _, c := range t.Clients {
				c <- msg.Data.Text
			}
		}
	}
}

// Check password and filtering rules ahead of launching the tracker.
func init() {
	// Only proceed if a password is available.
	if _, ok := os.LookupEnv("BEARER_TOKEN"); !ok {
		log.Fatal("init: the BEARER_TOKEN environment variable is required")
	}

	tap := ENDPOINT + "/rules"
	tag := "long form"

	add := func() error {
		val := strings.Join(KEYWORDS, " OR ")
		add := fmt.Sprintf(`{ "add": [{ "value": "%v", "tag": "%v" }] }`, val, tag)

		_, err := fetch(http.MethodPost, tap, []byte(add))

		return err
	}

	// A failed GET request could mean there are no rules available.
	res, err := fetch(http.MethodGet, tap, nil)

	if err != nil {
		log.Printf("init: failed reading rules, %v", err)

		// Try posting search rules.
		if err := add(); err != nil {
			log.Printf("init: failed adding rules, %v", err)
		}

		return
	}

	// To be parsing the response onto.
	var msg struct {
		Data []Data
		Meta Meta `json:"meta"`
	}

	defer res.Body.Close()

	if err := json.NewDecoder(res.Body).Decode(&msg); err != nil {
		log.Printf("init: failed parsing rules, %v", err)

		return
	}

	if msg.Meta.Count == 0 {
		if err := add(); err != nil {
			log.Printf("init: failed adding rules, %v", err)
		}
	}

	tagMissing := func() bool {
		for i := range msg.Data {
			if msg.Data[i].Tag == tag {
				return false
			}
		}
		return true
	}()

	if tagMissing {
		log.Print("init: failed finding rules")
	}
}

func main() {
	var p string

	flag.StringVar(&p, "p", PORT, "Choose a port number")
	flag.Parse()

	t := &Tracker{
		Clients: make(map[string]chan string),
		Delay:   0,
		Idle:    true,
		Suspend: make(chan bool),
		Resume:  make(chan bool),
	}

	// Listen for calls to start streaming.
	go func() {
		for <-t.Resume {
			if t.Idle {
				go t.Stream()
			}
		}
	}()

	s := &http.Server{
		Addr:    "localhost:" + p,
		Handler: t,
	}

	log.Fatal(s.ListenAndServe())
}

// fetch helps make BEARER_TOKEN authorized ENDPOINT json queries.
func fetch(verb string, hook string, b []byte) (*http.Response, error) {
	req, err := http.NewRequest(verb, hook, bytes.NewReader(b))

	if err != nil {
		return nil, err
	}

	req.Header.Set("Authorization", os.ExpandEnv("Bearer ${BEARER_TOKEN}"))
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)

	if err != nil {
		return nil, err
	}

	// If not 200/1, it's code 400 and above.
	if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated {
		// Reading from the response is up to the caller.
		return res, nil
	}

	return nil, fmt.Errorf("fetch: request failed / %v", res.StatusCode)
}