This repository has been archived on 2018-10-14. You can view files and clone it, but cannot push or open issues or pull requests.
twtxt-registry/update.py

33 lines
777 B
Python

import requests,time,re
FIX_TZ = re.compile(r"([-+]\d\d):(\d\d)")
def fixtz(m):
return m.group(1)+m.group(2)
TWEETFILE = "tweets.txt"
USERFILE = "users.txt"
USERS = []
with open(USERFILE) as f:
USERS = [l.rstrip().split("\t") for l in f if l.rstrip()]
tweets = []
for user in USERS:
# print(user)
r = requests.get(user[1])
if r.status_code!=200:
continue
t = r.text.rstrip().split("\n")
for tx in t:
if not re.match(r"\d{4,4}-\d{2,2}-\d{2,2}T\d{2,2}:\d{2,2}:\d{2,2}[+-]\d{2,2}:\d{2,2}",tx.split("\t")[0]):
print(tx)
continue
tweets.append("\t".join([user[0],user[1],tx]))
tweets.sort(key=lambda x: time.strptime(FIX_TZ.sub(fixtz,x.split("\t")[2]),"%Y-%m-%dT%H:%M:%S%z"))
with open(TWEETFILE,"w") as f:
f.write("\n".join(tweets[-200:]))
f.write("\n")