feat(dodo): 增加创建菜谱功能
增加创建菜谱功能 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
e2ed14784a
commit
ea42cef954
104
recipe/models.py
104
recipe/models.py
@ -5,8 +5,6 @@ import datetime
|
|||||||
from utils import const
|
from utils import const
|
||||||
import utils
|
import utils
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Recipe(models.Model):
|
class Recipe(models.Model):
|
||||||
name = models.CharField(max_length=128)
|
name = models.CharField(max_length=128)
|
||||||
recipe_type = models.CharField(max_length=32,
|
recipe_type = models.CharField(max_length=32,
|
||||||
@ -30,6 +28,108 @@ class Recipe(models.Model):
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def create_from_str(cls, content):
|
||||||
|
content = content.strip()
|
||||||
|
if not content:
|
||||||
|
return
|
||||||
|
name, *data = content.split(' ')
|
||||||
|
recipe_type = rate = difficulty = None
|
||||||
|
keys = [recipe_type, rate, difficulty]
|
||||||
|
for x in range(len(data)):
|
||||||
|
keys[x] = data[x]
|
||||||
|
recipe_type, rate, difficulty = keys
|
||||||
|
|
||||||
|
if recipe_type == '肉':
|
||||||
|
recipe_type = const.RECIPE_TYPE_MEAT
|
||||||
|
elif recipe_type == '菜':
|
||||||
|
recipe_type = const.RECIPE_TYPE_VEGETABLE
|
||||||
|
elif recipe_type == '汤':
|
||||||
|
recipe_type = const.RECIPE_TYPE_SOUP
|
||||||
|
else:
|
||||||
|
recipe_type = const.RECIPE_TYPE_MEAT
|
||||||
|
if rate:
|
||||||
|
try:
|
||||||
|
rate = int(rate)
|
||||||
|
except:
|
||||||
|
rate = 0
|
||||||
|
else:
|
||||||
|
rate = 0
|
||||||
|
if difficulty:
|
||||||
|
try:
|
||||||
|
difficulty = int(difficulty)
|
||||||
|
except:
|
||||||
|
difficulty = 0
|
||||||
|
else:
|
||||||
|
difficulty = 0
|
||||||
|
recipe = cls.objects.create(
|
||||||
|
name=name,
|
||||||
|
recipe_type=recipe_type,
|
||||||
|
rate=rate,
|
||||||
|
difficulty=difficulty
|
||||||
|
)
|
||||||
|
return recipe
|
||||||
|
|
||||||
|
@property
|
||||||
|
def display_recipe_type(self):
|
||||||
|
if self.recipe_type == const.RECIPE_TYPE_VEGETABLE:
|
||||||
|
return '菜'
|
||||||
|
elif self.recipe_type == const.RECIPE_TYPE_MEAT:
|
||||||
|
return '肉'
|
||||||
|
elif self.recipe_type == const.RECIPE_TYPE_SOUP:
|
||||||
|
return '汤'
|
||||||
|
return '肉'
|
||||||
|
|
||||||
|
@property
|
||||||
|
def display_rate(self):
|
||||||
|
return '🍚' * self.rate
|
||||||
|
|
||||||
|
@property
|
||||||
|
def display_difficult(self):
|
||||||
|
return '⭐️' * self.difficulty
|
||||||
|
|
||||||
|
def construct_lart_card(self):
|
||||||
|
|
||||||
|
data = {
|
||||||
|
"config": {
|
||||||
|
"wide_screen_mode": True
|
||||||
|
},
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"tag": "markdown",
|
||||||
|
"content": "# **%s**\n类型:%s\n难度:%s\n评分:%s" % (self.name, self.display_recipe_type, self.display_difficult, self.display_rate)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tag": "action",
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"tag": "button",
|
||||||
|
"text": {
|
||||||
|
"tag": "plain_text",
|
||||||
|
"content": "查看"
|
||||||
|
},
|
||||||
|
"multi_url": {
|
||||||
|
"url": "https://recipe.tunpok.com/recipe/%s" % self.id,
|
||||||
|
"android_url": "https://recipe.tunpok.com/recipe-mobile/recipe/%s" % self.id,
|
||||||
|
"ios_url": "https://recipe.tunpok.com/recipe-mobile/recipe/%s" % self.id,
|
||||||
|
"pc_url": "https://recipe.tunpok.com/recipe/%s" % self.id
|
||||||
|
},
|
||||||
|
"type": "primary"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"tag": "button",
|
||||||
|
"text": {
|
||||||
|
"tag": "plain_text",
|
||||||
|
"content": "删除"
|
||||||
|
},
|
||||||
|
"type": "danger"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
class DailyRecipe(models.Model):
|
class DailyRecipe(models.Model):
|
||||||
recipes = models.ManyToManyField(Recipe)
|
recipes = models.ManyToManyField(Recipe)
|
||||||
|
|||||||
@ -15,6 +15,9 @@ import time
|
|||||||
import subprocess
|
import subprocess
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
import recipe.models
|
||||||
|
from utils import const
|
||||||
|
|
||||||
APP_VERIFICATION_TOKEN = 'uKQQiOVMYg2cTgrjkyBmodrHTUaCXzG3'
|
APP_VERIFICATION_TOKEN = 'uKQQiOVMYg2cTgrjkyBmodrHTUaCXzG3'
|
||||||
APP_ID = 'cli_a115fe8b83f9100c'
|
APP_ID = 'cli_a115fe8b83f9100c'
|
||||||
APP_SECRET = 'yuSQenId0VfvwdZ3qL9wMd8FpCMEUL0u'
|
APP_SECRET = 'yuSQenId0VfvwdZ3qL9wMd8FpCMEUL0u'
|
||||||
@ -186,7 +189,9 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||||||
if orig_text.startswith('/'):
|
if orig_text.startswith('/'):
|
||||||
redis_cli.set(event_id, int(time.time()), ex=60*60*7)
|
redis_cli.set(event_id, int(time.time()), ex=60*60*7)
|
||||||
if orig_text not in ['/last', '/del']:
|
if orig_text not in ['/last', '/del']:
|
||||||
if not orig_text.startswith('/deploy '):
|
for action_ in ['/deploy ', '/菜谱 ']:
|
||||||
|
if orig_text.startswith(action_):
|
||||||
|
break
|
||||||
self.msg_compoment(access_token, open_id, '指令错误')
|
self.msg_compoment(access_token, open_id, '指令错误')
|
||||||
return
|
return
|
||||||
if orig_text == '/last':
|
if orig_text == '/last':
|
||||||
@ -216,6 +221,16 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||||||
else:
|
else:
|
||||||
self.msg_compoment(access_token, open_id, '⚠️ %s 不存在 ⚠️' % site_)
|
self.msg_compoment(access_token, open_id, '⚠️ %s 不存在 ⚠️' % site_)
|
||||||
return
|
return
|
||||||
|
elif orig_text.startswith('/菜谱 '):
|
||||||
|
content = orig_text.split('/菜谱 ')[1]
|
||||||
|
recipe_ = recipe.models.Recipe.create_from_str(content)
|
||||||
|
if recipe_:
|
||||||
|
self.msg_compoment(access_token, open_id, None,
|
||||||
|
const.LARK_WEBHOOK_MSG_TYPE_INTERACTIVE,
|
||||||
|
recipe_.construct_lart_card())
|
||||||
|
else:
|
||||||
|
self.msg_compoment(access_token, open_id, '⚠️ 创建失败 ⚠️')
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
toot_resp = mastodon.status_post(text)
|
toot_resp = mastodon.status_post(text)
|
||||||
@ -240,18 +255,22 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(body.encode())
|
self.wfile.write(body.encode())
|
||||||
|
|
||||||
def send_message(self, token, open_id, text):
|
def send_message(self, token, open_id, text, msg_type=None, content=None):
|
||||||
url = "https://open.feishu.cn/open-apis/message/v4/send/"
|
url = "https://open.feishu.cn/open-apis/message/v4/send/"
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": "Bearer " + token
|
"Authorization": "Bearer " + token
|
||||||
}
|
}
|
||||||
|
if not msg_type:
|
||||||
|
msg_type = const.LARK_WEBHOOK_MSG_TYPE_TEXT
|
||||||
req_body = {
|
req_body = {
|
||||||
"msg_type": "text",
|
"msg_type": msg_type
|
||||||
"content": {
|
|
||||||
"text": text
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if msg_type == const.LARK_WEBHOOK_MSG_TYPE_TEXT:
|
||||||
|
req_body['content'] = {'text': text}
|
||||||
|
elif msg_type == const.LARK_WEBHOOK_MSG_TYPE_INTERACTIVE:
|
||||||
|
req_body['card'] = content
|
||||||
|
|
||||||
req_body = dict(req_body, **open_id) # 根据open_id判断返回域
|
req_body = dict(req_body, **open_id) # 根据open_id判断返回域
|
||||||
|
|
||||||
data = bytes(json.dumps(req_body), encoding='utf8')
|
data = bytes(json.dumps(req_body), encoding='utf8')
|
||||||
@ -270,8 +289,8 @@ class RequestHandler(BaseHTTPRequestHandler):
|
|||||||
code,
|
code,
|
||||||
rsp_dict.get("msg", ""))
|
rsp_dict.get("msg", ""))
|
||||||
|
|
||||||
def msg_compoment(self, token, open_id, text):
|
def msg_compoment(self, token, open_id, text, msg_type=None, content=None):
|
||||||
self.send_message(token, open_id, text)
|
self.send_message(token, open_id, text, msg_type, content)
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
port = 5000
|
port = 5000
|
||||||
|
|||||||
@ -22,3 +22,4 @@ FILTER_GTE_LTE = ['exact', 'gte', 'gt', 'lte', 'lt']
|
|||||||
|
|
||||||
|
|
||||||
LARK_WEBHOOK_MSG_TYPE_TEXT = 'text'
|
LARK_WEBHOOK_MSG_TYPE_TEXT = 'text'
|
||||||
|
LARK_WEBHOOK_MSG_TYPE_INTERACTIVE = 'interactive'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user