namespace the refs for hn and substack.

This commit is contained in:
Jason Schwarzenberger 2020-11-23 16:09:12 +13:00
parent 54a4c7e55a
commit c1b6349771
2 changed files with 22 additions and 3 deletions

View File

@ -40,7 +40,7 @@ def api(route, ref=None):
return False return False
def feed(): 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): def comment(i):
if 'author' not in 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 return sum([comment_count(c) for c in i['comments']]) + alive
def story(ref): def story(ref):
ref = ref.replace('hn:', '')
r = api(API_ITEM, ref) r = api(API_ITEM, ref)
if not r: return False if not r: return False

View File

@ -66,13 +66,20 @@ class Publication:
def __init__(self, domain): def __init__(self, domain):
self.BASE_DOMAIN = 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): def feed(self):
stories = api(lambda x: api_stories(x, self.BASE_DOMAIN), referer=self.BASE_DOMAIN) stories = api(lambda x: api_stories(x, self.BASE_DOMAIN), referer=self.BASE_DOMAIN)
if not stories: return [] if not stories: return []
stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories])) 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): def story(self, ref):
ref = self.strip_ref_prefix(ref)
stories = api(lambda x: api_stories(x, self.BASE_DOMAIN), referer=self.BASE_DOMAIN) stories = api(lambda x: api_stories(x, self.BASE_DOMAIN), referer=self.BASE_DOMAIN)
if not stories: return False if not stories: return False
stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories])) stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories]))
@ -116,13 +123,24 @@ class Publication:
class Top: 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): def feed(self):
stories = api(SUBSTACK_API_TOP_POSTS, referer=SUBSTACK_REFERER) stories = api(SUBSTACK_API_TOP_POSTS, referer=SUBSTACK_REFERER)
if not stories: return [] if not stories: return []
stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories])) 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): def story(self, ref):
ref = self.strip_ref_prefix(ref)
stories = api(SUBSTACK_API_TOP_POSTS, referer=SUBSTACK_REFERER) stories = api(SUBSTACK_API_TOP_POSTS, referer=SUBSTACK_REFERER)
if not stories: return False if not stories: return False
stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories])) stories = list(filter(None, [i if i.get("audience") == "everyone" else None for i in stories]))