phantasmobile/app/item/tasks.py

92 lines
3 KiB
Python
Raw Normal View History

2023-07-25 09:33:50 +00:00
import os
import tempfile
import requests
2023-07-24 11:05:45 +00:00
from celery.schedules import crontab
from django.conf import settings
2023-07-25 09:33:50 +00:00
from django.utils import timezone
2023-07-24 11:05:45 +00:00
from ..signalbot import rpc
2023-08-17 06:36:51 +00:00
from ..telegrambot import rpc as telegram_rpc
2023-07-24 11:05:45 +00:00
from ..celery import app
from . import models
2023-07-25 09:33:50 +00:00
2023-07-25 19:03:54 +00:00
@app.task(queue="default", ignore_results=True)
2023-07-24 11:05:45 +00:00
def announce_items():
2023-07-25 09:33:50 +00:00
if not getattr(settings, 'SIGNAL_ANNOUNCE_GROUP'):
return
now = timezone.now()
qs = models.Item.objects.exclude(published=None).filter(announced=None).filter(published__lte=now).order_by('published')
for item in qs:
item.announced = now
link = settings.BASE_URL + item.get_absolute_url()
message = link
image = item.data.get('thumbnail')
description = ""
if image:
f = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
r = requests.get(image)
f.write(r.content)
f.close()
image = f.name
2023-08-16 15:21:32 +00:00
if getattr(settings, "SIGNAL_ANNOUNCE_GROUP"):
r = rpc.send(
message,
group=settings.SIGNAL_ANNOUNCE_GROUP,
preview_url=link,
preview_title=item.title,
preview_image=image,
preview_description=description,
)
if getattr(settings, "TELEGRAM_ANNOUNCE_CHANNEL"):
2023-08-21 21:02:38 +00:00
message = f'{item.title}\n<a href="{link}">{link}</a>'
2023-08-16 15:21:32 +00:00
r = telegram_rpc.send(
message,
channel=settings.TELEGRAM_ANNOUNCE_CHANNEL,
preview_image=image,
)
2023-07-25 09:33:50 +00:00
item.save()
if image:
os.unlink(f.name)
2023-07-24 11:05:45 +00:00
2023-08-31 16:12:34 +00:00
@app.task(queue="default", ignore_results=True)
def notify_telegram(message):
if getattr(settings, "TELEGRAM_ANNOUNCE_CHANNEL"):
r = telegram_rpc.send(
message,
channel=settings.TELEGRAM_ANNOUNCE_CHANNEL
)
2023-07-25 19:03:54 +00:00
@app.task(queue="default", ignore_results=True)
2023-07-24 11:05:45 +00:00
def notify_moderators(id, link):
comment = models.Comment.objects.filter(id=id).first()
if comment:
2023-07-25 19:03:54 +00:00
message = "%s commented on %s (%s)\n\n%s" % (
comment.name, comment.item.title, link, comment.text
)
2023-07-24 11:05:45 +00:00
r = rpc.send(message, group=settings.SIGNAL_MODERATORS_GROUP)
if r and "timestamp" in r:
comment.data["moderator_ts"] = r["timestamp"]
comment.save()
2023-07-25 19:03:54 +00:00
if comment.published:
account = settings.SIGNAL_ACCOUNT
group = settings.SIGNAL_MODERATORS_GROUP
rpc.send_reaction(
account, comment.data["moderator_ts"], "🎉", group=group
)
2023-08-31 16:12:34 +00:00
msg = '%s commented on <b>%s</b> at <a href="%s">%s</a>' % (
comment.name, comment.item.title, link, link
)
notify_telegram.delay(msg)
2023-07-24 11:05:45 +00:00
else:
print("failed to notify", r)
2023-07-25 19:03:54 +00:00
2023-07-24 11:05:45 +00:00
@app.on_after_finalize.connect
def setup_periodic_tasks(sender, **kwargs):
sender.add_periodic_task(crontab(minute="*/2"), announce_items.s())