aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: bde760637422b095fcb73d664ec8ab2f51cd8e57 (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
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
// piupiu: grab tweets and put them into RSS feeds.
// Copyright (C) 2026-Ω Miquel Sabaté Solà <mssola@mssola.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"encoding/json"
	"encoding/xml"
	"flag"
	"fmt"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"time"
)

type Tweet struct {
	ID        string `json:"id"`
	Text      string `json:"text"`
	CreatedAt string `json:"created_at"`
}

type TweetResponse struct {
	Data []Tweet `json:"data"`
}

type RSS struct {
	XMLName xml.Name   `xml:"rss"`
	Version string     `xml:"version,attr"`
	Channel RSSChannel `xml:"channel"`
}

type RSSChannel struct {
	Title       string    `xml:"title"`
	Link        string    `xml:"link"`
	Description string    `xml:"description"`
	Items       []RSSItem `xml:"item"`
}

type RSSItem struct {
	Title       string `xml:"title"`
	Link        string `xml:"link"`
	Description string `xml:"description"`
	PubDate     string `xml:"pubDate"`
	GUID        string `xml:"guid"`
}

// Given the directory 'path' and the 'username', create a
// '<path>/<username>.xml' file with the 'tweets' in RSS format.
func tweetsToRss(username, path string, tweets []Tweet) error {
	// XML definition of this whole thing.
	rss := RSS{
		Version: "2.0",
		Channel: RSSChannel{
			Title:       fmt.Sprintf("piu piu: @%s", username),
			Link:        fmt.Sprintf("https://x.com/%s", username),
			Description: fmt.Sprintf("@%s", username),
			Items:       make([]RSSItem, 0),
		},
	}

	// Now iterate on the given tweets and transform them into RSSItem's.
	for _, tweet := range tweets {
		tweetURL := fmt.Sprintf("https://x.com/%s/status/%s", username, tweet.ID)

		// Remember how the X API required a specific timestamp format? Well,
		// RSS wants another one. Because timestamp ISO's are how programmers
		// become (even) more cynical.
		t, err := time.Parse(time.RFC3339, tweet.CreatedAt)
		pubDate := tweet.CreatedAt
		if err == nil {
			pubDate = t.Format(time.RFC1123Z)
		}

		// Use the first few words or characters as the RSS item title.
		title := tweet.Text
		if len(title) > 50 {
			title = title[:47] + "..."
		}

		// Voila!
		rss.Channel.Items = append(rss.Channel.Items, RSSItem{
			Title:       title,
			Link:        tweetURL,
			Description: tweet.Text,
			PubDate:     pubDate,
			GUID:        tweetURL,
		})
	}

	// Marshal into XML with a standard header.
	output, err := xml.MarshalIndent(rss, "", "  ")
	if err != nil {
		return err
	}
	fullXML := []byte(xml.Header + string(output))

	// And write the damn thing.
	filename := fmt.Sprintf("%s.xml", username)
	filePath := filepath.Join(path, filename)
	return os.WriteFile(filePath, fullXML, 0644)
}

// Given a user 'id' and the API 'token', fetch all the tweets from the last 24
// hours.
func fetch(id, token string) ([]Tweet, error) {
	apiURL := fmt.Sprintf("https://api.x.com/2/users/%v/tweets", id)
	u, err := url.Parse(apiURL)
	if err != nil {
		return nil, err
	}

	q := u.Query()
	startTime := time.Now().UTC().Add(-24 * time.Hour).Format(time.RFC3339)
	q.Set("start_time", startTime)
	q.Set("tweet.fields", "created_at,text")
	q.Set("exclude", "replies")
	u.RawQuery = q.Encode()

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Authorization", "Bearer "+token)

	client := &http.Client{Timeout: 10 * time.Second}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("API error: %v (%v)", resp.Status, resp.StatusCode)
	}

	var object TweetResponse
	if err := json.NewDecoder(resp.Body).Decode(&object); err != nil {
		return nil, err
	}
	return object.Data, nil
}

func main() {
	// First of all, enforce the stupid ass API token.
	token := os.Getenv("PIUPIU_TOKEN")
	if token == "" {
		fmt.Println("error: you need to provide a token via the 'PIUPIU_TOKEN' environment variable.")
		os.Exit(1)
	}

	// Behold, argument parsing!
	query := flag.String("q", "", "Comma-separated list of '<@user_name>:<user_id>' values.")
	output := flag.String("o", ".", "Directory where RSS files will be dropped")
	flag.Parse()

	// This is cursed in unholy ways, as path sanitization shenanigans have
	// proved since the existence of the concept of a path in operating
	// systems. That being said, I'm the sole user of this, so whatever...
	if *output != "." {
		if err := os.MkdirAll(*output, 0755); err != nil {
			fmt.Printf("error: cannot create directory: %w", err)
			os.Exit(1)
		}
	}

	ids := strings.Split(*query, ",")
	exit := 0
	for _, id := range ids {
		// I could've gone with something friendlier, but this was faster, and I
		// just didn't care. If you don't recall the format, just RTFM (in this
		// case just -h, as manuals are for people who actually want to make a
		// living out of ones and zeroes :P).
		vals := strings.Split(id, ":")
		if len(vals) != 2 {
			fmt.Printf("error: bad argument format\n\n")
			flag.PrintDefaults()
			os.Exit(1)
		}

		// Fetch the tweet data from the API.
		name := strings.TrimSpace(vals[0])
		id = strings.TrimSpace(vals[1])
		tweets, err := fetch(id, token)
		if err != nil {
			fmt.Printf("error: %v\n", err)
			exit = 2
			continue
		}

		// Generate the RSS file for this user.
		err = tweetsToRss(name, *output, tweets)
		if err != nil {
			fmt.Printf("error: %v\n", err)
			exit = 2
		}
	}

	os.Exit(exit)
}