Skip to content

Commit

Permalink
add manual delivery reason param
Browse files Browse the repository at this point in the history
  • Loading branch information
cdhigh committed Apr 24, 2024
1 parent f0f786a commit e26eb3e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
3 changes: 2 additions & 1 deletion application/back_end/task_queue_apscheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def create_delivery_task(payload: dict):
from ..work.worker import WorkerImpl
userName = payload.get('userName', '')
recipeId = payload.get('recipeId', '')
scheduler.add_job(f'Worker{random.randint(0, 1000)}', WorkerImpl, args=[userName, recipeId],
reason = payload.get('reason', 'cron')
scheduler.add_job(f'Worker{random.randint(0, 1000)}', WorkerImpl, args=[userName, recipeId, reason],
misfire_grace_time=20*60, replace_existing=True)

def create_url2book_task(payload: dict):
Expand Down
16 changes: 10 additions & 6 deletions application/lib/calibre/web/feeds/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ class BasicNewsRecipe(Recipe):
# set by worker.py
translator = {}
tts = {}
delivery_reason = 'cron'

# See the built-in recipes for examples of these settings.

Expand Down Expand Up @@ -2159,17 +2160,20 @@ def parse_feeds(self):
continue

added.add(url)
lastTime = LastDelivered.get_or_none((LastDelivered.user==self.user.name) & (LastDelivered.url==url))
delta = (datetime.datetime.utcnow() - lastTime.datetime) if lastTime else None
timeItem = LastDelivered.get_or_none((LastDelivered.user==self.user.name) & (LastDelivered.url==url))
delta = (datetime.datetime.utcnow() - timeItem.datetime) if timeItem else None
#这里oldest_article和其他的recipe不一样,这个参数表示在这个区间内不会重复推送
if ((not lastTime) or (not self.oldest_article) or
if ((not timeItem) or (not self.oldest_article) or (self.delivery_reason == 'manual') or
(delta.days * 24 * 3600 + delta.seconds > 24 * 3600 * self.oldest_article)):
id_counter += 1
feed.articles.append(Article(f'internal id#{id_counter}', title, url, 'KindleEar', '', now, ''))

if lastTime:
lastTime.datetime = datetime.datetime.utcnow()
lastTime.save()
#如果是手动推送,不单不记录已推送日期,还将已有的上次推送日期数据删除
if ((self.delivery_reason == 'manual') or (not self.oldest_article)) and timeItem:
timeItem.delete_instance()
elif timeItem:
timeItem.datetime = datetime.datetime.utcnow()
timeItem.save()
else:
LastDelivered.create(user=self.user.name, url=url)
else:
Expand Down
17 changes: 9 additions & 8 deletions application/view/deliver.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def MultiUserDelivery():
continue

#到了这里就是需要推送的
queueOneBook(bkQueue, user, book.recipe_id, book.separated)
queueOneBook(bkQueue, user, book.recipe_id, book.separated, reason='cron')
sentCnt += 1
flushQueueToPush(bkQueue)
flushQueueToPush(bkQueue, reason='cron')
return "Put {} recipes into queue.".format(sentCnt)

#判断指定用户的书籍和订阅哪些需要推送
Expand All @@ -77,9 +77,9 @@ def SingleUserDelivery(userName: str, idList: list=None):

bkQueue = defaultdict(list)
for bkRecipe in recipesToPush: #BookedRecipe实例
queueOneBook(bkQueue, user, bkRecipe.recipe_id, bkRecipe.separated)
queueOneBook(bkQueue, user, bkRecipe.recipe_id, bkRecipe.separated, reason='manual')
sent.append(f'<i>{bkRecipe.title}</i>')
flushQueueToPush(bkQueue)
flushQueueToPush(bkQueue, reason='manual')

if sent:
tips = (_("The following recipes has been added to the push queue.") + '<br/>&nbsp;&nbsp;&nbsp;&nbsp;'
Expand All @@ -94,18 +94,19 @@ def SingleUserDelivery(userName: str, idList: list=None):
#user: KeUser实例
#recipeId: Recipe Id, custom:xx, upload:xx, builtin:xx
#separated: 是否单独推送
def queueOneBook(queueToPush: defaultdict, user: KeUser, recipeId: str, separated: bool):
#reason: cron/manual,启动推送的原因
def queueOneBook(queueToPush: defaultdict, user: KeUser, recipeId: str, separated: bool, reason='cron'):
from ..back_end.task_queue_adpt import create_delivery_task
recipeId = recipeId.replace(':', '__')
if separated:
create_delivery_task({"userName": user.name, "recipeId": recipeId})
create_delivery_task({'userName': user.name, 'recipeId': recipeId, 'reason': reason})
else:
queueToPush[user.name].append(recipeId) #合并推送

#启动推送队列中的书籍
def flushQueueToPush(queueToPush: defaultdict):
def flushQueueToPush(queueToPush: defaultdict, reason='cron'):
from ..back_end.task_queue_adpt import create_delivery_task
for name in queueToPush:
create_delivery_task({'userName': name, 'recipeId': ','.join(queueToPush[name])})
create_delivery_task({'userName': name, 'recipeId': ','.join(queueToPush[name]), 'reason': reason})
queueToPush.clear()

2 changes: 1 addition & 1 deletion application/view/inbound_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def ReceiveMailImpl(sender, to, subject, txtBodies, htmlBodies, attachments):

#通过邮件触发一次“现在投递”
if to.lower() == 'trigger':
create_delivery_task({'userName': userName, 'recipeId': subject})
create_delivery_task({'userName': userName, 'recipeId': subject, 'reason': 'manual'})
return f'A delivery task for "{userName}" is triggered'

forceToLinks = False
Expand Down
12 changes: 8 additions & 4 deletions application/work/worker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#后台实际的推送任务,由任务队列触发
#Author: cdhigh<https://github.com/cdhigh>
import os, datetime, time, io, logging
from collections import defaultdict
from flask import Blueprint, request
Expand All @@ -25,15 +26,17 @@ def WorkerAllNow():
#下载文章和生成电子书并且发送邮件
@bpWorker.route("/worker")
def Worker():
userName = request.args.get('userName', '')
recipeId = request.args.get('recipeId', '') #如果有多个Recipe,使用','分隔
return WorkerImpl(userName, recipeId, default_log)
args = request.args
userName = args.get('userName', '')
recipeId = args.get('recipeId', '') #如果有多个Recipe,使用','分隔
reason = args.get('reason', 'cron') #cron/manual
return WorkerImpl(userName, recipeId, reason, default_log)

#执行实际抓取网页生成电子书任务
#userName: 需要执行任务的账号名
#recipeId: 需要投递的Recipe ID,如果有多个,使用逗号分隔
#返回执行结果字符串
def WorkerImpl(userName: str, recipeId: list=None, log=None):
def WorkerImpl(userName: str, recipeId: list=None, reason='cron', log=None):
if not log:
log = default_log

Expand Down Expand Up @@ -75,6 +78,7 @@ def WorkerImpl(userName: str, recipeId: list=None, log=None):
if not ro.language or ro.language == 'und':
ro.language = user.book_cfg('language')

ro.delivery_reason = reason
ro.extra_css = combine_css(ro.extra_css) #合并自定义css
ro.translator = bked.translator #设置网页翻译器信息
ro.tts = bked.tts.copy() #文本转语音设置,需要中途修改tts内容
Expand Down

0 comments on commit e26eb3e

Please sign in to comment.