summaryrefslogtreecommitdiff
path: root/flatbot.go
blob: 3cb052f8dc7ac767965c5da2cd2a9e7ee33aa407 (plain) (blame)
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
package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
)

func main() {
	url := "http://localhost:8000/2025-02-19-isle-of-dogs.html"
	body, err := fetch(url)
	if err != nil {
		log.Fatal(err)
	}
	flats, err := parse(body)
	if err != nil {
		log.Fatal(err)
	}
	sent, err := readSent("sent/sent.json")
	if err != nil {
		log.Fatal(err)
	}
	flats = removeAlreadySent(flats, sent)
	fmt.Println(flats)
	writeSent(flats, "/tmp/sent.json")
}

func fetch(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return make([]byte, 0), err
	}
	if resp.StatusCode != http.StatusOK {
		return make([]byte, 0),
			fmt.Errorf("Bad response status: %d", resp.StatusCode)
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return make([]byte, 0), err
	}
	return body, nil
}