From c1b6349771462edc327656f4941661dfcff483e9 Mon Sep 17 00:00:00 2001 From: Jason Schwarzenberger Date: Mon, 23 Nov 2020 16:09:12 +1300 Subject: [PATCH] namespace the refs for hn and substack. --- apiserver/feeds/hackernews.py | 3 ++- apiserver/feeds/substack.py | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/apiserver/feeds/hackernews.py b/apiserver/feeds/hackernews.py index 5e951b0..6ef5556 100644 --- a/apiserver/feeds/hackernews.py +++ b/apiserver/feeds/hackernews.py @@ -40,7 +40,7 @@ def api(route, ref=None): return False def feed(): - return [str(x) for x in api(API_TOPSTORIES) or []] + return ['hn:'+str(x) for x in api(API_TOPSTORIES) or []] def comment(i): if 'author' not in i: @@ -60,6 +60,7 @@ def comment_count(i): return sum([comment_count(c) for c in i['comments']]) + alive def story(ref): + ref = ref.replace('hn:', '') r = api(API_ITEM, ref) if not r: return False diff --git a/apiserver/feeds/substack.py b/apiserver/feeds/substack.py index e47273b..b52fd50 100644 --- a/apiserver/feeds/substack.py +++ b/apiserver/feeds/substack.py @@ -66,13 +66,20 @@ class Publication: def __init__(self, domain): self.BASE_DOMAIN = domain + def ref_prefix(self, ref): + return f"{self.BASE_DOMAIN}/#id:{ref}" + + def strip_ref_prefix(self, ref): + return ref.replace(f"{self.BASE_DOMAIN}/#id:", '') + def feed(self): stories = api(lambda x: api_stories(x, self.BASE_DOMAIN), referer=self.BASE_DOMAIN) if not stories: return [] stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories])) - return [str(i.get("id")) for i in stories or []] + return [self.ref_prefix(str(i.get("id"))) for i in stories or []] def story(self, ref): + ref = self.strip_ref_prefix(ref) stories = api(lambda x: api_stories(x, self.BASE_DOMAIN), referer=self.BASE_DOMAIN) if not stories: return False stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories])) @@ -116,13 +123,24 @@ class Publication: class Top: + def ref_prefix(self, base_url, ref): + return f"{base_url}/#id:{ref}" + + def strip_ref_prefix(self, ref): + if '/#id:' in ref: + base_url, item = ref.split(f"/#id:") + return item + return ref + def feed(self): stories = api(SUBSTACK_API_TOP_POSTS, referer=SUBSTACK_REFERER) if not stories: return [] stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories])) - return [str(i.get("id")) for i in stories or []] + stories = [dict(id=i.get('id'), base_url=i.get("pub", { 'base_url': '' }).get("base_url")) for i in stories or []] + return [self.ref_prefix(str(i.get("base_url")), str(i.get("id"))) for i in stories] def story(self, ref): + ref = self.strip_ref_prefix(ref) stories = api(SUBSTACK_API_TOP_POSTS, referer=SUBSTACK_REFERER) if not stories: return False stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories]))