aboutsummaryrefslogtreecommitdiff
path: root/tagrss.py
blob: c5ad368e90b2ad714eee0e087be966f976fd31a0 (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
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
"""
Copyright (c) 2023-present Arjun Satarkar <me@arjunsatarkar.net>.
Licensed under the GNU Affero General Public License v3.0. See LICENSE.txt in
the root of this repository for the text of the license.
"""
import feedparser
import requests

import calendar
import io
import pathlib
import sqlite3
import time
import typing


class FeedAlreadyAddedError(Exception):
    pass


class FeedFetchError(Exception):
    def __init__(self, feed_source: str, status_code: int):
        super().__init__(f"Get {feed_source} returned HTTP {status_code}")


class SqliteMissingForeignKeySupportError(Exception):
    pass


class Sqlite3NotSerializedModeError(Exception):
    pass


class StorageConstraintViolationError(Exception):
    def __init__(self, error):
        super().__init__(error)


def fetch_parsed_feed(feed_source: str) -> tuple[feedparser.FeedParserDict, int]:
    response = requests.get(feed_source)
    epoch_downloaded: int = int(time.time())
    if response.status_code != requests.codes.ok:
        raise FeedFetchError(feed_source, response.status_code)
    try:
        base: str = response.headers["Content-Location"]
    except KeyError:
        base: str = feed_source
    parsed = feedparser.parse(
        io.BytesIO(bytes(response.text, encoding="utf-8")),
        response_headers={"Content-Location": base},
    )
    return (parsed, epoch_downloaded)


class TagRss:
    def __init__(self, *, storage_path: str | pathlib.Path):
        if sqlite3.threadsafety != 3:
            raise Sqlite3NotSerializedModeError

        self.connection: sqlite3.Connection = sqlite3.connect(
            storage_path, check_same_thread=False
        )

        with self.connection:
            with open("setup.sql", "r") as setup_script:
                self.connection.executescript(setup_script.read())
            if (1,) not in self.connection.execute("PRAGMA foreign_keys;").fetchmany(1):
                raise SqliteMissingForeignKeySupportError

    def add_feed(
        self,
        *,
        feed_source: str,
        parsed_feed: feedparser.FeedParserDict,
        epoch_downloaded: int,
        tags: list[str],
    ) -> int:
        with self.connection:
            feed_title: str = parsed_feed.feed.get("title", "")  # type: ignore
            try:
                self.connection.execute(
                    "INSERT INTO feeds(source, title) VALUES(?, ?);",
                    (feed_source, feed_title),
                )  # Note: ensure no more INSERTs between this and the last_insert_rowid() call
            except sqlite3.IntegrityError:
                raise FeedAlreadyAddedError
            feed_id: int = int(
                self.connection.execute("SELECT last_insert_rowid();").fetchone()[0]
            )
            self.connection.executemany(
                "INSERT INTO feed_tags(feed_id, tag) VALUES(?, ?);",
                ((feed_id, tag) for tag in tags),
            )
            self.store_feed_entries(feed_id, parsed_feed, epoch_downloaded)
        return feed_id

    def get_entries(
        self,
        *,
        limit: int,
        offset: int = 0,
        included_feeds: typing.Optional[typing.Collection[int]] = None,
        included_tags: typing.Optional[typing.Collection[str]] = None,
    ) -> list[dict[str, typing.Any]]:
        where_clause: str = "WHERE 1"
        if included_feeds:
            where_clause += f" AND feed_id IN ({','.join('?' * len(included_feeds))})"
        if included_tags:
            where_clause += (
                " AND feed_id IN (SELECT feed_id FROM feed_tags WHERE tag = ?)"
                * len(included_tags)
            )
        with self.connection:
            resp = self.connection.execute(
                f"SELECT id, feed_id, title, link, epoch_published, epoch_updated FROM entries \
                    {where_clause} \
                    ORDER BY id DESC LIMIT ? OFFSET ?;",
                (
                    *(included_feeds if included_feeds else ()),
                    *(included_tags if included_tags else ()),
                    limit,
                    offset,
                ),
            ).fetchall()

        entries = []
        for entry in resp:
            entries.append(
                {
                    "id": entry[0],
                    "feed_id": entry[1],
                    "title": entry[2],
                    "link": entry[3],
                    "epoch_published": entry[4],
                    "epoch_updated": entry[5],
                }
            )
        return entries

    def get_feed_source(self, feed_id: int) -> str:
        with self.connection:
            return self.connection.execute(
                "SELECT source FROM feeds WHERE id = ?;", (feed_id,)
            ).fetchone()[0]

    def get_feed_title(self, feed_id: int) -> str:
        with self.connection:
            return self.connection.execute(
                "SELECT title FROM feeds WHERE id = ?;", (feed_id,)
            ).fetchone()[0]

    def get_feed_tags(self, feed_id: int) -> list[str]:
        with self.connection:
            return [
                t[0]
                for t in self.connection.execute(
                    "SELECT tag FROM feed_tags WHERE feed_id = ?;", (feed_id,)
                ).fetchall()
            ]

    def set_feed_source(self, feed_id: int, feed_source: str):
        with self.connection:
            self.connection.execute(
                "UPDATE feeds SET source = ? WHERE id = ?;", (feed_source, feed_id)
            )

    def set_feed_title(self, feed_id: int, feed_title: str):
        with self.connection:
            self.connection.execute(
                "UPDATE feeds SET title = ? WHERE id = ?;", (feed_title, feed_id)
            )

    def set_feed_tags(self, feed_id: int, feed_tags: list[str]):
        with self.connection:
            self.connection.execute(
                "DELETE FROM feed_tags WHERE feed_id = ?;", (feed_id,)
            )
            self.connection.executemany(
                "INSERT INTO feed_tags(feed_id, tag) VALUES(?, ?);",
                ((feed_id, tag) for tag in feed_tags),
            )

    def delete_feed(self, feed_id: int) -> None:
        with self.connection:
            self.connection.execute("DELETE FROM feeds WHERE id = ?;", (feed_id,))

    def get_feeds(self, *, limit: int, offset: int = 0) -> list[dict[str, typing.Any]]:
        with self.connection:
            resp = self.connection.execute(
                "SELECT id, source, title FROM feeds \
                ORDER BY id ASC LIMIT ? OFFSET ?;",
                (limit, offset),
            ).fetchall()
        feeds = []
        for row in resp:
            feeds.append(
                {
                    "id": row[0],
                    "source": row[1],
                    "title": row[2],
                }
            )
        return feeds

    def get_entry_count(
        self,
        *,
        included_feeds: typing.Optional[typing.Collection[int]] = None,
        included_tags: typing.Optional[typing.Collection[str]] = None,
    ) -> int:
        if not (included_feeds or included_tags):
            with self.connection:
                return self.connection.execute(
                    "SELECT count from entry_count;"
                ).fetchone()[0]
        else:
            where_clause: str = "WHERE 1"
            if included_feeds:
                where_clause += (
                    f" AND feed_id IN ({','.join('?' * len(included_feeds))})"
                )
            if included_tags:
                where_clause += (
                    " AND feed_id IN (SELECT feed_id FROM feed_tags WHERE tag = ?)"
                    * len(included_tags)
                )
            with self.connection:
                return self.connection.execute(
                    f"SELECT COUNT(*) FROM entries {where_clause};",
                    (
                        *(included_feeds if included_feeds else ()),
                        *(included_tags if included_tags else ()),
                    ),
                ).fetchone()[0]

    def get_feed_count(self) -> int:
        with self.connection:
            return self.connection.execute("SELECT count from feed_count;").fetchone()[
                0
            ]

    def store_feed_entries(self, feed_id: int, parsed_feed, epoch_downloaded: int):
        for entry in reversed(parsed_feed.entries):
            link: str = entry.get("link", None)
            title: str = entry.get("title", None)
            try:
                epoch_published: typing.Optional[int] = calendar.timegm(
                    entry.get("published_parsed", None)
                )
            except TypeError:
                epoch_published = None
            try:
                epoch_updated: typing.Optional[int] = calendar.timegm(
                    entry.get("updated_parsed", None)
                )
            except TypeError:
                epoch_updated = None
            with self.connection:
                try:
                    self.connection.execute(
                        "INSERT INTO entries(feed_id, title, link, epoch_published, epoch_updated, epoch_downloaded) \
                            VALUES(?, ?, ?, ?, ?, ?);",
                        (
                            feed_id,
                            title,
                            link,
                            epoch_published,
                            epoch_updated,
                            epoch_downloaded,
                        ),
                    )
                except sqlite3.IntegrityError as e:
                    # Probably feed deleted before we got here, so foreign key
                    # constraints would have been violated by the insert.
                    raise StorageConstraintViolationError(e)

    def close(self) -> None:
        self.connection.close()