2019-01-26 09:36:44 +00:00
|
|
|
from celery_once import QueueOnce
|
2019-01-11 19:16:01 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from telebot import TeleBot
|
2019-01-19 06:52:00 +00:00
|
|
|
from djconfig import config
|
2019-01-11 19:16:01 +00:00
|
|
|
|
|
|
|
from config.celery import app
|
2019-01-26 09:36:44 +00:00
|
|
|
|
|
|
|
|
2019-01-26 09:43:15 +00:00
|
|
|
@app.task(base=QueueOnce, once={'graceful': True})
|
2019-01-26 09:36:44 +00:00
|
|
|
def check_feeds():
|
2019-01-26 09:38:13 +00:00
|
|
|
from feeds.models import Feed
|
|
|
|
|
2019-01-26 09:36:44 +00:00
|
|
|
feeds = Feed.objects.filter(lock=False)
|
|
|
|
for feed in feeds:
|
|
|
|
feed.run_check()
|
2019-01-11 19:16:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.task()
|
|
|
|
def execute_feed(feed_pk):
|
|
|
|
from feeds.models import Feed
|
2019-01-25 16:33:22 +00:00
|
|
|
|
2019-01-25 16:33:54 +00:00
|
|
|
config._reload_maybe()
|
2019-01-11 19:16:01 +00:00
|
|
|
|
2019-02-04 09:15:53 +00:00
|
|
|
feed = None
|
2019-01-11 19:16:01 +00:00
|
|
|
try:
|
|
|
|
feed = Feed.objects.get(pk=feed_pk)
|
|
|
|
|
|
|
|
if not feed.lock:
|
|
|
|
feed.lock = True
|
|
|
|
feed.save()
|
|
|
|
|
2019-03-09 19:11:42 +00:00
|
|
|
bot = TeleBot(config.feed_bot_token, threaded=False)
|
2019-02-04 09:15:53 +00:00
|
|
|
for last_id in feed.config.execute(bot, feed.chat_id, feed.last_id):
|
|
|
|
if last_id:
|
|
|
|
feed.last_id = last_id
|
|
|
|
feed.save()
|
2019-02-19 10:57:08 +00:00
|
|
|
bot.stop_bot()
|
2019-01-11 19:16:01 +00:00
|
|
|
feed.last_check = timezone.now()
|
|
|
|
feed.save()
|
|
|
|
finally:
|
2019-02-04 09:15:53 +00:00
|
|
|
if feed:
|
|
|
|
feed.lock = False
|
|
|
|
feed.save()
|