all repos

smutok @ 0ff9ce8

yet another tui rss reader (not abandoned, just paused development)

smutok/internal/provider/freshrss.go(view raw)

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
package provider

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
)

var (
	ErrInvalidRequest = errors.New("invalid invalid request")
	ErrUnauthorized   = errors.New("unauthorized")
)

type FreshRSS struct {
	host      string
	authToken string
	client    *http.Client
}

func NewFreshRSS(host string) *FreshRSS {
	return &FreshRSS{
		host: host,
		client: &http.Client{
			Timeout: 10 * time.Second,
		},
	}
}

func (g FreshRSS) Login(ctx context.Context, email, password string) (string, error) {
	body := url.Values{}
	body.Set("Email", email)
	body.Set("Passwd", password)

	var resp string
	if err := g.postRequest(ctx, "/accounts/ClientLogin", body, &resp); err != nil {
		return "", err
	}

	for line := range strings.SplitSeq(resp, "\n") {
		if after, ok := strings.CutPrefix(line, "Auth="); ok {
			return after, nil
		}
	}

	return "", ErrUnauthorized
}

func (g *FreshRSS) SetAuthToken(token string) {
	// todo: validate token
	g.authToken = token
}

func (g FreshRSS) GetWriteToken(ctx context.Context) (string, error) {
	var resp string
	err := g.request(ctx, "/reader/api/0/token", nil, &resp)
	return resp, err
}

type subscriptionList struct {
	Subscriptions []Subscriptions `json:"subscriptions"`
}
type Subscriptions struct {
	Categories struct {
		ID    string `json:"id"`
		Label string `json:"label"`
	} `json:"categories"`
	ID      string `json:"id"`
	HTMLURL string `json:"htmlUrl"`
	IconURL string `json:"iconUrl"`
	Title   string `json:"title"`
	URL     string `json:"url"`
}

func (g FreshRSS) SubscriptionList(ctx context.Context) ([]Subscriptions, error) {
	var resp subscriptionList
	err := g.request(ctx, "/reader/api/0/subscription/list?output=json", nil, &resp)
	return resp.Subscriptions, err
}

type tagList struct {
	Tags []Tag `json:"tags"`
}

type Tag struct {
	ID   string `json:"id"`
	Type string `json:"type,omitempty"`
}

func (g FreshRSS) TagList(ctx context.Context) ([]Tag, error) {
	var resp tagList
	err := g.request(ctx, "/reader/api/0/tag/list?output=json", nil, &resp)
	return resp.Tags, err
}

type StreamContents struct {
	Continuation string `json:"continuation"`
	ID           string `json:"id"`
	Items        []struct {
		Alternate []struct {
			Href string `json:"href"`
		} `json:"alternate"`
		Author    string `json:"author"`
		Canonical []struct {
			Href string `json:"href"`
		} `json:"canonical"`
		Categories    []string `json:"categories"`
		CrawlTimeMsec string   `json:"crawlTimeMsec"`
		ID            string   `json:"id"`
		Origin        struct {
			HTMLURL  string `json:"htmlUrl"`
			StreamID string `json:"streamId"`
			Title    string `json:"title"`
		} `json:"origin"`
		Published int `json:"published"`
		Summary   struct {
			Content string `json:"content"`
		} `json:"summary"`
		TimestampUsec string `json:"timestampUsec"`
		Title         string `json:"title"`
	} `json:"items"`
	Updated int `json:"updated"`
}

func (g FreshRSS) GetItems(ctx context.Context, excludeTarget string, lastModified, n int) (StreamContents, error) {
	params := url.Values{}
	setOption(&params, "xt", excludeTarget)
	setOptionInt(&params, "ot", lastModified)
	setOptionInt(&params, "n", n)

	var resp StreamContents
	err := g.request(ctx, "/reader/api/0/stream/contents/user/-/state/com.google/reading-list", params, &resp)
	return resp, err
}

func (g FreshRSS) GetStaredItems(ctx context.Context, n int) (StreamContents, error) {
	params := url.Values{}
	setOptionInt(&params, "n", n)

	var resp StreamContents
	err := g.request(ctx, "/reader/api/0/stream/contents/user/-/state/com.google/starred", params, &resp)
	return resp, err
}

type StreamItemsIDs struct {
	Continuation string `json:"continuation"`
	ItemRefs     []struct {
		ID string `json:"id"`
	} `json:"itemRefs"`
}

func (g FreshRSS) GetItemsIDs(ctx context.Context, excludeTarget, includeTarget string, n int) (StreamItemsIDs, error) {
	params := url.Values{}
	setOption(&params, "xt", excludeTarget)
	setOption(&params, "s", includeTarget)
	setOptionInt(&params, "n", n)

	var resp StreamItemsIDs
	err := g.request(ctx, "/reader/api/0/stream/items/ids", params, &resp)
	return resp, err
}

func (g FreshRSS) SetItemsState(ctx context.Context, token, itemID string, addAction, removeAction string) error {
	params := url.Values{}
	params.Set("T", token)
	params.Set("i", itemID)
	setOption(&params, "a", addAction)
	setOption(&params, "r", removeAction)

	err := g.postRequest(ctx, "/reader/api/0/edit-tag", params, nil)
	return err
}

type EditSubscription struct {
	// StreamID to operate on (required)
	// `feed/1` - the id
	// `feed/https:...` - or the url
	// it seems like 'feed' is required in the id
	StreamID string

	// Action can be one of those: subscribe OR unsubscribe OR edit
	Action string

	// Title, or for edit, or title for adding
	Title string

	// Add, StreamID to add the sub (generally a category)
	AddCategoryID string

	// Remove, StreamId to remove the subscription(s) from (generally a category)
	Remove string
}

func (g FreshRSS) SubscriptionEdit(ctx context.Context, token string, opts EditSubscription) (string, error) {
	// todo: action is required

	body := url.Values{}
	body.Set("T", token)
	body.Set("s", opts.StreamID)
	body.Set("ac", opts.Action)
	setOption(&body, "t", opts.Title)
	setOption(&body, "a", opts.AddCategoryID)
	setOption(&body, "r", opts.Remove)

	var resp string
	err := g.postRequest(ctx, "/reader/api/0/subscription/edit", body, &resp)
	return resp, err
}

func setOption(b *url.Values, k, v string) {
	if v != "" {
		b.Set(k, v)
	}
}

func setOptionInt(b *url.Values, k string, v int) {
	if v != 0 {
		b.Set(k, strconv.Itoa(v))
	}
}

// request, makes GET request with params passed as url params
func (g *FreshRSS) request(ctx context.Context, endpoint string, params url.Values, resp any) error {
	u, err := url.Parse(g.host + endpoint)
	if err != nil {
		return err
	}
	u.RawQuery = params.Encode()

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
	if err != nil {
		return fmt.Errorf("failed to create request: %w", err)
	}

	return g.handleResponse(req, resp)
}

// postRequest makes POST requests with parameters passed as form.
func (g *FreshRSS) postRequest(ctx context.Context, endpoint string, body url.Values, resp any) error {
	var reqBody io.Reader
	if body != nil {
		reqBody = strings.NewReader(body.Encode())
	}

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, g.host+endpoint, reqBody)
	if err != nil {
		return fmt.Errorf("failed to create request: %w", err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	return g.handleResponse(req, resp)
}

type apiResponse struct {
	Error string `json:"error,omitempty"`
}

func (g *FreshRSS) handleResponse(req *http.Request, out any) error {
	if g.authToken != "" {
		req.Header.Set("Authorization", "GoogleLogin auth="+g.authToken)
	}

	resp, err := g.client.Do(req)
	if err != nil {
		return fmt.Errorf("request failed: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		if resp.StatusCode == http.StatusUnauthorized {
			return ErrUnauthorized
		}
		body, _ := io.ReadAll(resp.Body)
		return fmt.Errorf("API error: status %d: %s", resp.StatusCode, string(body))
	}

	if strPtr, ok := out.(*string); ok {
		body, err := io.ReadAll(resp.Body)
		if err != nil {
			return fmt.Errorf("failed to read response body: %w", err)
		}
		*strPtr = string(body)

		slog.Debug("string response", "content", string(body))
		return nil
	}

	if err := json.NewDecoder(resp.Body).Decode(out); err != nil {
		return fmt.Errorf("failed to decode response: %w", err)
	}

	if apiResp, ok := out.(*apiResponse); ok && apiResp.Error != "" {
		return fmt.Errorf("%s", apiResp.Error)
	}

	return nil
}