Feed date scraper for Gemini (protocol)
template: be able to replace existing dates
| -rwxr-xr-x | zachwalk.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/zachwalk.py b/zachwalk.py index fd8ed64..b45d333 100755 --- a/zachwalk.py +++ b/zachwalk.py @@ -7,7 +7,7 @@ import ssl import fileinput from dateutil.parser import parse -DEFAULT = '1970-01-01' +DEFAULT = parse('1970-01-01').date() def getnewestdate(url): """load the url, and find the newest date listed in a link""" @@ -42,14 +42,26 @@ def gnd(fp): pass return DEFAULT +def replaceDateIfNewer(desc, newestdate): + try: + tup = parse(desc, fuzzy_with_tokens=True) + date = tup[0].date() + except: + return f'{newestdate} - {desc}' + if newestdate > date: + return str(newestdate) + ' '.join(tup[1]) + return desc + def main(): for line in fileinput.input(): #stdin or file from argv if line[0:2] == '=>': # plz don't use multiple spaces. url = line.split(' ')[1] desc = getdesc(line) + olddate = gnd(url) newestdate = getnewestdate(url) - print(f'=> {url} {newestdate} - {desc}') + desc = replaceDateIfNewer(desc, newestdate) + print(f'=> {url} {desc}') else: print(line.rstrip()) |