Merge pull request #12 from SneakyBastardSword/master

precompile regexes
This commit is contained in:
Lynne 2019-02-23 10:23:23 +10:00 committed by GitHub
commit 0e01d5d8ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ toots.db-wal
__pycache__/* __pycache__/*
.vscode/ .vscode/
.editorconfig .editorconfig
.*.swp

33
main.py
View File

@ -31,15 +31,15 @@ if 'instance_blacklist' not in cfg:
#if the user is using a (very!) old version that still uses the .secret files, migrate to the new method #if the user is using a (very!) old version that still uses the .secret files, migrate to the new method
if os.path.exists("clientcred.secret"): if os.path.exists("clientcred.secret"):
print("Upgrading to new storage method") print("Upgrading to new storage method")
cc = open("clientcred.secret").read().split("\n") cc = open("clientcred.secret").read().split("\n")
cfg['client'] = { cfg['client'] = {
"id": cc[0], "id": cc[0],
"secret": cc[1] "secret": cc[1]
} }
cfg['secret'] = open("usercred.secret").read().rstrip("\n") cfg['secret'] = open("usercred.secret").read().rstrip("\n")
os.remove("clientcred.secret") os.remove("clientcred.secret")
os.remove("usercred.secret") os.remove("usercred.secret")
if "client" not in cfg: if "client" not in cfg:
@ -92,6 +92,13 @@ def handleCtrlC(signal, frame):
signal.signal(signal.SIGINT, handleCtrlC) signal.signal(signal.SIGINT, handleCtrlC)
patterns = {
"handle": re.compile(r"^.*@(.+)"),
"url": re.compile(r"https?:\/\/(.*)"),
"uri": re.compile(r'template="([^"]+)"'),
"pid": re.compile(r"[^\/]+$"),
}
for f in following: for f in following:
last_toot = c.execute("SELECT id FROM `toots` WHERE userid LIKE ? ORDER BY id DESC LIMIT 1", (f.id,)).fetchone() last_toot = c.execute("SELECT id FROM `toots` WHERE userid LIKE ? ORDER BY id DESC LIMIT 1", (f.id,)).fetchone()
if last_toot != None: if last_toot != None:
@ -102,9 +109,9 @@ for f in following:
#find the user's activitypub outbox #find the user's activitypub outbox
print("WebFingering... (do not laugh at this. WebFinger is a federated protocol. https://wikipedia.org/wiki/WebFinger)") print("WebFingering... (do not laugh at this. WebFinger is a federated protocol. https://wikipedia.org/wiki/WebFinger)")
instance = re.search(r"^.*@(.+)", f.acct) instance = patterns["handle"].search(f.acct)
if instance == None: if instance == None:
instance = re.search(r"https?:\/\/(.*)", cfg['site']).group(1) instance = patterns["url"].search(cfg['site']).group(1)
else: else:
instance = instance.group(1) instance = instance.group(1)
@ -114,7 +121,7 @@ for f in following:
try: try:
r = requests.get("https://{}/.well-known/host-meta".format(instance), timeout=10) r = requests.get("https://{}/.well-known/host-meta".format(instance), timeout=10)
uri = re.search(r'template="([^"]+)"', r.text).group(1) uri = patterns["uri"].search(r.text).group(1)
uri = uri.format(uri = "{}@{}".format(f.username, instance)) uri = uri.format(uri = "{}@{}".format(f.username, instance))
r = requests.get(uri, headers={"Accept": "application/json"}, timeout=10) r = requests.get(uri, headers={"Accept": "application/json"}, timeout=10)
j = r.json() j = r.json()
@ -162,7 +169,7 @@ for f in following:
#you might be wondering, "lynne, what if the instance ratelimits you after 40 posts, and they've made 60 since main.py was last run? wouldn't the bot miss 20 posts and never be able to see them?" to which i reply, "it's called mstdn-ebooks not fediverse-ebooks. pleroma support is an afterthought" #you might be wondering, "lynne, what if the instance ratelimits you after 40 posts, and they've made 60 since main.py was last run? wouldn't the bot miss 20 posts and never be able to see them?" to which i reply, "it's called mstdn-ebooks not fediverse-ebooks. pleroma support is an afterthought"
done = True done = True
break break
pid = re.search(r"[^\/]+$", oi['object']['id']).group(0) pid = patterns["pid"].search(oi['object']['id']).group(0)
c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)", ( c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)", (
pid, pid,
f.id, f.id,