set notif follow dan buat delay antar fungsi

This commit is contained in:
Kusaeni 2024-09-30 22:22:41 +07:00
parent 3cde26d4f0
commit 92623b7369
2 changed files with 45 additions and 15 deletions

View file

@ -14,9 +14,11 @@ App ini masih dalam pengembangan dan banyak hal yang belum bisa dilakukan, berik
- [x] Mentions
- [x] Like/Favourite
- [x] Boost/Reblog
- [ ] Follow
- [ ] Tandai data di database jika sudah terkirim
- [ ] Cron
- [x] Follow
- [x] Tandai data di database jika sudah terkirim
- [ ] Delay antar fungsi
- [ ] Cron internal
- [ ] Build
## Install
Clone repositori ini dan install di lokal.

52
main.py
View file

@ -7,8 +7,9 @@ import logging
import asyncio
import telegram
import re
import json
from models import Notifikasi, Session
from sqlalchemy import and_, or_, func
from sqlalchemy import and_, or_, func, update
from sqlalchemy.exc import IntegrityError
from dotenv import load_dotenv
from datetime import datetime
@ -31,23 +32,28 @@ async def getNotif():
if req.status_code == 200:
data = req.json()
print(data.__class__)
try:
for notif in data:
type = notif['type']
inreply = notif['status']['in_reply_to_id']
inreply = None
status_content = None
url = None
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']
status = notif['status']['content']
url = notif['status']['url']
kirim = 'Belum'
if type == 'follow':
insert_follow = Notifikasi(post_id=postid, created_at=created, handler=handler, display_name=username, type=type, remark=kirim)
session.add(insert_follow)
else:
insert_mentions = Notifikasi(post_id=postid, created_at=created, handler=handler, display_name=username, type=type, status=status,url=url, remark=kirim)
insert_mentions = Notifikasi(post_id=postid, created_at=created, handler=handler, display_name=username, type=type, status=status_content,url=url, remark=kirim)
session.add(insert_mentions)
session.commit()
@ -84,11 +90,11 @@ async def kirim_ke_tele():
if str(notif.type) == 'follow':
try:
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}
<b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}\n from python
"""
)
except:
print('Ada masalah saat kirim ke Telebot')
except Exception as e:
print(f'Follow: Ada masalah saat kirim ke Telebot', e)
else:
html_strip = re.compile('<.*?>')
status = str(notif.status)
@ -96,13 +102,35 @@ async def kirim_ke_tele():
try:
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{notif.url}
<b>{notif.display_name}</b>\n<i>{notif.handler}</i>\n{flag}\n\n{status_text}\nfrom python\n{notif.url}
"""
)
except Exception as e:
print(f'Ada masalah saat kirim ke Telebot1', e)
print(f'Mentions: Ada masalah saat kirim ke Telebot', e)
session.close()
async def tandai_notif():
try:
cek_data = session.query(Notifikasi).filter(Notifikasi.remark == 'Belum').all()
session.execute(
update(Notifikasi).
where(Notifikasi.remark == 'Belum').
values(remark='Sudah')
)
session.commit()
except Exception as e:
print(f'Ada masalah saat update remark', e)
finally:
session.close()
async def jalan():
await asyncio.sleep(2)
await getNotif()
await asyncio.sleep(5)
await kirim_ke_tele()
await asyncio.sleep(5)
await tandai_notif()
# asyncio.run(getNotif())
asyncio.run(kirim_ke_tele())
#
if __name__ == "__main__":
asyncio.run(jalan())