-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_feeds.py
More file actions
executable file
·40 lines (28 loc) · 1.14 KB
/
load_feeds.py
File metadata and controls
executable file
·40 lines (28 loc) · 1.14 KB
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
#!/usr/bin/python3
import json
import sqlite3
class Feed:
def __init__(self, url, img_url=None):
self.url = url
self.img_url = img_url
def main():
with open("feeds.json", "r") as f:
feeds = read_feeds_list(f)
with sqlite3.connect("rssread.db") as db_connection:
save_feeds(feeds, db_connection)
def read_feeds_list(file_handle):
parsed_feeds = json.load(file_handle)
feeds = []
for feed in parsed_feeds:
feeds.append(Feed(feed["url"], feed.get("img", None)))
return feeds
def save_feeds(feeds, db_connection):
db_connection.execute('CREATE TABLE IF NOT EXISTS feeds (feed_id INTEGER PRIMARY KEY, url TEXT UNIQUE, img_url TEXT, active INTEGER)')
db_connection.execute('UPDATE feeds SET active=0')
for feed in feeds:
db_connection.execute('UPDATE feeds SET img_url=?, active=1 WHERE url=?', (feed.img_url, feed.url))
db_connection.execute('INSERT OR IGNORE INTO feeds(url, img_url, active) VALUES (?, ?, 1)', (feed.url, feed.img_url))
db_connection.execute('DELETE FROM feeds WHERE active=0')
db_connection.commit()
if __name__ == "__main__":
main()