-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
73 lines (63 loc) · 1.93 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import re
import prime
import time
import logging
import sys
import traceback
import boto3
import configparser
import botocore
import os
import tiny_bsky
BUCKET = "primenumber-bot"
KEY = "bsky-since.txt"
class idstore:
def __init__(self, filename=None):
self._filename = filename
if filename is None:
config = configparser.ConfigParser()
config.read("aws_credentials")
AWS_ACCESS_KEY_ID = config["default"]["AWS_ACCESS_KEY_ID"]
AWS_SECRET_ACCESS_KEY = config["default"]["AWS_SECRET_ACCESS_KEY"]
sess = sess = boto3.Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
self._s3_client = sess.client("s3")
def get(self):
if self._filename is None:
r = self._s3_client.get_object(Bucket=BUCKET, Key=KEY)
body = r["Body"].read()
return body.decode("utf-8")
else:
if not os.path.exists(self._filename):
return None
with open(self._filename) as f:
return f.read()
def put(self, s):
if self._filename is None:
self._s3_client.put_object(
Bucket=BUCKET, Key=KEY, Body=str(s).encode("utf-8")
)
else:
with open(self._filename, "w") as f:
f.write(s)
def botmain():
store = idstore()
client = tiny_bsky.Client(ini_file="bsky.ini")
since = store.get()
l = client.getMentions(since=since)
for x in l[::-1]:
text = x["record"]["text"]
t = re.search(r"-?\d+", text)
try:
n = int(t.group())
except:
n = None
if n is not None:
p = prime.getPrime(n)
client.post(str(p), uri=x["uri"], cid=x["cid"])
since = x["record"]["createdAt"]
store.put(since)
if __name__ == "__main__":
botmain()