From 06bd2ef61a3e6ebcb2fc33e74268692dfb6d0321 Mon Sep 17 00:00:00 2001 From: VikingKong Date: Tue, 22 Nov 2022 22:05:22 +0300 Subject: [PATCH] Implement fetching categories, feeds and unread counts and displaying them in the left pane --- .gitignore | 2 ++ API.py | 24 +++++++++++++++++++++ App.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------ app.css | 37 -------------------------------- 4 files changed, 82 insertions(+), 44 deletions(-) create mode 100644 API.py delete mode 100644 app.css diff --git a/.gitignore b/.gitignore index 1ee8ead..5b334d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ venv/ .env +config.yml +__pycache__ diff --git a/API.py b/API.py new file mode 100644 index 0000000..ee8792d --- /dev/null +++ b/API.py @@ -0,0 +1,24 @@ +import requests + + +class Fetcher: + def __init__(self, URL, token): + self.URL = URL + self.token = token + self.headers = {"Authorization": "GoogleLogin auth="+token} + self.getUnreadCounts() + self.getSubscriptions() + + def getUnreadCounts(self): + response = requests.get(self.URL+"/reader/api/0/unread-count?output=json", headers=self.headers) + result = dict([(item["id"], (item["count"], item["newestItemTimestampUsec"])) for item in response.json()["unreadcounts"]]) + self.unreadCounts = result + self.categories = [{"id": item, "name": item[13:], "count": self.unreadCounts[item][0]} + for item in self.unreadCounts.keys() if item[0:13] == "user/-/label/"] + + def getSubscriptions(self): + response = requests.get(self.URL+"/reader/api/0/subscription/list?output=json", headers=self.headers) + self.feeds = response.json()["subscriptions"] + + def feedsFromCategory(self, category): + return [item for item in self.feeds if item["categories"][0]["id"] == category] diff --git a/App.py b/App.py index 47e4ca0..14964ba 100644 --- a/App.py +++ b/App.py @@ -1,5 +1,53 @@ import urwid +import yaml from concurrent.futures import ThreadPoolExecutor +from API import Fetcher + + +class LeftPane(urwid.ListBox): + def __init__(self, categories): + super().__init__(self) + items = [urwid.AttrMap(urwid.Columns([(5, urwid.Text(str(category["count"]))), urwid.Text(category["name"])]), + category["id"], "reveal focus") for category in categories] + walker = urwid.SimpleListWalker(items) + self.body = walker + self.categoryPosition = 0 + + def keypress(self, size, key): + if key in ("j", "down"): + item_size = len(self.body) + focus_widget, idx = self.get_focus() + if idx < item_size - 1: + idx = idx + 1 + self.set_focus(idx) + return + elif key in ("k", "up"): + focus_widget, idx = self.get_focus() + if idx > 0: + idx = idx - 1 + self.set_focus(idx) + return + elif key in ("l", "right"): + focus_widget, idx = self.get_focus() + self.categoryPosition = idx + itemId = focus_widget.attr_map[None] + feeds = tui.fetcher.feedsFromCategory(itemId) + feedItems = [urwid.AttrMap(urwid.Columns([(5, urwid.Text(str(tui.fetcher.unreadCounts[feed["id"]][0]))), + urwid.Text(feed["title"])]), feed["id"], "reveal focus") for feed in feeds] + walker = urwid.SimpleListWalker(feedItems) + self.body = walker + tui.leftBox.set_title(itemId[13:]) + return + elif key in ("h", "left"): + items = [urwid.AttrMap(urwid.Columns([(5, urwid.Text(str(category["count"]))), urwid.Text(category["name"])]), + category["id"], "reveal focus") for category in tui.fetcher.categories] + walker = urwid.SimpleFocusListWalker(items) + self.body = walker + tui.leftBox.set_title("Categories") + tui.feeds.set_focus(self.categoryPosition) + return + + return super().keypress(size, key) class Items(urwid.ListBox): @@ -61,18 +109,19 @@ class TUI(urwid.Frame): def __init__(self): + with open('config.yml', 'r') as file: + config = yaml.safe_load(file) + URL = config["server"]["URL"] + token = config["server"]["token"] + + self.fetcher = Fetcher(URL, token) + self.leftPaneItems = {} self.activePane = False - for i in range(0, 15): - self.leftPaneItems["Category# " + str(i)] = {"count": str(i), - "feeds": [{"count": str(k), "name": "Feed# " + str(k)} for k in range(0, 10)]} - self.executor = ThreadPoolExecutor(max_workers=1) self.loop = None - self.feedItems = [urwid.Columns([(4, urwid.Text(self.leftPaneItems[item]["count"])), urwid.Text(item)]) - for item in self.leftPaneItems.keys()] self.articleItems = [urwid.Text("Article# " + str(i)) for i in range(0, 10)] - self.feeds = Items(self.feedItems) + self.feeds = LeftPane(self.fetcher.categories) self.articles = Items(self.articleItems) self.leftBox = urwid.LineBox(self.feeds, title="Categories") self.container = urwid.Columns( diff --git a/app.css b/app.css deleted file mode 100644 index 450f11a..0000000 --- a/app.css +++ /dev/null @@ -1,37 +0,0 @@ -#app-grid { - layout: grid; - grid-size: 2; - grid-columns: 1fr 2fr; - grid-rows: 1fr; -} - -#left-container > Static { - background: $boost; - margin-bottom: 1; -} - -#feed-active { - color: blue; - border: dodgerblue; -} - -#feed-nonactive { - color: auto; -} - -#feed-pane { - row-span: 1; - background: $panel; - border: dodgerblue; -} - -#right-pane { - background: $panel; - border: dodgerblue; -} - -#right-pane > Static { - margin-bottom: 1; - background: $boost; -} -