This commit is contained in:
mwiegand 2022-04-10 22:23:28 +02:00
parent 57a17342ed
commit cb12de50d8

View file

@ -92,8 +92,23 @@ import re
from urllib.parse import urlparse
import requests
from hashlib import sha3_256
import pb8000
pdt = pytz.timezone("Us/Pacific")
db = pg8000.connect(
host=credentials[app]['host'],
port=credentials[app]['port'],
database=credentials[app]['database'],
user=credentials[app]['user'],
password=credentials[app]['password'],
)
def query(app, query, **params):
cursor = db(app).cursor()
cursor.paramstyle = "named"
cursor.execute(query, params)
db(app).commit()
return cursor
def parse_trs(trs):
for tr in trs:
@ -103,12 +118,35 @@ def parse_trs(trs):
to_url = tds[1].find('a')['href']
to_name = tds[1].text
date = datetime.strptime(tds[2].text, '%b %d, %Y @ %I:%M%p PDT').replace(tzinfo=pdt)
text = tds[3].text
message = tds[3].text
checksum = sha3_256(
(from_url + to_url + str(date.timestamp()) + text).encode()
(from_url + to_url + str(date.timestamp()) + message).encode()
).hexdigest()
print(f'(#{checksum}@{date}) {from_name} -> {to_name}: {text}')
print(f'(#{checksum}@{date}) {from_name} -> {to_name}: {message}')
if query(
'''
SELECT 1 FROM messages
WHERE checksum = :checksum
''',
checksum=checksum,
).rowcount:
print(f'message {checksum} already exists')
else:
print(f'adding new message {checksum}')
query(
'''
INSERT INTO messages (checksum, from_url, from_name, to_url, to_name, date, message)
VALUES(:checksum, :from_url, :from_name, :to_url, :to_name, :date, :message)
''',
checksum=checksum,
from_url=from_url,
from_name=from_name,
to_url=to_url,
to_name=to_name,
date=date,
message=message,
)
# download steamuserimages
for url_string in re.findall(r'(https?://\S+)', text):
url = urlparse(url_string)