python-gotosocial-notif/main.py

175 lines
5 KiB
Python
Raw Normal View History

2024-09-29 12:14:20 +02:00
# %%
# ZED Python REPL
import asyncio
2024-10-01 06:32:29 +02:00
import os
import re
2024-09-29 12:14:20 +02:00
from datetime import datetime
2024-10-01 06:32:29 +02:00
import requests
import telegram
from dotenv import load_dotenv
from sqlalchemy import desc, update
2024-10-01 06:32:29 +02:00
from sqlalchemy.exc import IntegrityError
from models import Notifikasi, Session
2024-09-29 12:14:20 +02:00
# Request ke kauaku
load_dotenv()
GTS_API = os.getenv("GTS_API")
GTS_TOKEN = os.getenv("GTS_TOKEN")
TELE_BOT = os.getenv("TELE_BOT")
TELE_CHATID = os.getenv("TELE_CHATID")
2024-09-29 12:14:20 +02:00
headers = {"Authorization": f"Bearer {GTS_TOKEN}"}
2024-09-29 12:14:20 +02:00
session = Session()
2024-10-01 06:32:29 +02:00
bot = telegram.Bot(token=f"{TELE_BOT}")
2024-09-29 12:14:20 +02:00
async def getNotif():
req = requests.get(f"{GTS_API}?limit=5", headers=headers)
2024-09-29 12:14:20 +02:00
if req.status_code == 200:
data = req.json()
try:
for notif in data:
2024-10-01 06:32:29 +02:00
type = notif["type"]
inreply = None
status_content = None
url = None
2024-10-01 06:32:29 +02:00
if "status" in notif:
inreply = notif["status"]["in_reply_to_id"]
status_content = notif["status"]["content"]
url = notif["status"]["url"]
postid = notif["id"]
created = datetime.strptime(
notif["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ"
)
handler = notif["account"]["acct"]
username = notif["account"]["display_name"]
kirim = "Belum"
if type == "follow":
insert_follow = Notifikasi(
post_id=postid,
created_at=created,
handler=handler,
display_name=username,
type=type,
remark=kirim,
)
2024-09-29 12:14:20 +02:00
session.add(insert_follow)
else:
2024-10-01 06:32:29 +02:00
insert_mentions = Notifikasi(
inreplyto=inreply,
post_id=postid,
created_at=created,
handler=handler,
display_name=username,
type=type,
status=status_content,
url=url,
remark=kirim,
)
2024-09-29 12:14:20 +02:00
session.add(insert_mentions)
session.commit()
except IntegrityError as err:
session.rollback()
2024-10-01 06:32:29 +02:00
if "post_id" in str(err.orig):
2024-09-29 12:14:20 +02:00
print("Post Id sudah ada")
else:
2024-10-01 06:32:29 +02:00
print("Integritas data bermasalah", str(err))
2024-09-29 12:14:20 +02:00
finally:
session.close()
async def kirim_ke_tele():
2024-10-01 06:32:29 +02:00
cek_data = (
session.query(Notifikasi)
.filter(Notifikasi.remark.like("%elu%"))
.order_by(desc(Notifikasi.id))
2024-10-01 06:32:29 +02:00
.limit(10)
.all()
)
2024-09-29 12:14:20 +02:00
for notif in cek_data:
2024-10-01 06:32:29 +02:00
def icon_(type_):
2024-10-01 06:32:29 +02:00
if type_ == "mention":
return "💬 mention your note"
elif type_ == "reblog":
return "🚀 boost your note"
elif type_ == "favourite":
return "💖 falling in love with your note"
elif type_ == "follow":
return "✋ started to follow you"
else:
return None
flag = icon_(notif.type)
2024-10-01 06:32:29 +02:00
if str(notif.type) == "follow":
2024-09-29 12:14:20 +02:00
try:
2024-10-01 06:32:29 +02:00
await bot.send_message(
parse_mode="html",
chat_id=str(TELE_CHATID),
text=f"""
<b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}
2024-10-01 06:32:29 +02:00
""",
)
except Exception as e:
2024-10-01 06:32:29 +02:00
print("Follow: Ada masalah saat kirim ke Telebot", e)
2024-09-29 12:14:20 +02:00
else:
2024-10-02 07:39:37 +02:00
raw = str(notif.status)
raw = raw.replace("<p>", "", 1)
raw = raw.rsplit("</p>", 1)[0]
rubah_p = re.sub(r"</p>", "\n\n", raw)
rubah_br = re.sub(r"<br>", "\n", rubah_p)
status_text = re.sub(r"<.*?>", "", rubah_br)
try:
2024-10-01 06:32:29 +02:00
await bot.send_message(
parse_mode="html",
chat_id=str(TELE_CHATID),
text=f"""
<b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}\n\n{status_text}\n\n<a href="{notif.url}">source</a>
2024-10-01 06:32:29 +02:00
""",
2024-09-29 12:14:20 +02:00
)
except Exception as e:
2024-10-01 06:32:29 +02:00
print("Mentions: Ada masalah saat kirim ke Telebot", e)
session.close()
2024-10-01 06:32:29 +02:00
async def tandai_notif():
try:
session.execute(
2024-10-01 06:32:29 +02:00
update(Notifikasi)
.where(Notifikasi.remark == "Belum")
.values(remark="Sudah")
)
session.commit()
except Exception as e:
2024-10-01 06:32:29 +02:00
print("Ada masalah saat update remark", e)
finally:
session.close()
2024-10-01 06:32:29 +02:00
async def jalan():
await asyncio.sleep(2)
await getNotif()
await asyncio.sleep(5)
2024-10-03 15:41:49 +02:00
await kirim_ke_tele()
await asyncio.sleep(5)
await tandai_notif()
2024-10-01 06:32:29 +02:00
#
if __name__ == "__main__":
asyncio.run(jalan())