diff --git a/app/item/tasks.py b/app/item/tasks.py index 3b063f8..bc17e37 100644 --- a/app/item/tasks.py +++ b/app/item/tasks.py @@ -1,14 +1,45 @@ +import os +import tempfile + +import requests + from celery.schedules import crontab from django.conf import settings +from django.utils import timezone from ..signalbot import rpc from ..celery import app - from . import models + @app.task(queue="default") def announce_items(): - pass + 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 + r = rpc.send( + message, group=settings.SIGNAL_ANNOUNCE_GROUP, + preview_url=link, + preview_title=item.title, + preview_image=image, + preview_description=description, + ) + item.save() + if image: + os.unlink(f.name) @app.task(queue="default") diff --git a/app/signalbot/rpc.py b/app/signalbot/rpc.py index f28ef6a..4962b44 100644 --- a/app/signalbot/rpc.py +++ b/app/signalbot/rpc.py @@ -29,7 +29,7 @@ def api(method, params): raise Exception("Error: %s", response) -def send(msg, to=None, group=None): +def send(msg, to=None, group=None, preview_url=None, preview_title=None, preview_description=None, preview_image=None): params = { "message": msg } @@ -37,6 +37,14 @@ def send(msg, to=None, group=None): params["groupId"] = group else: params["recipient"] = to + if preview_url: + params['previewUrl'] = preview_url + if preview_title: + params['previewTitle'] = preview_title + if preview_description: + params['previewDescription'] = preview_description + if preview_image: + params['previewImage'] = preview_image return api("send", params)