async def post(book: BookItem): book_obj = session.query(Books).filter_by(id=book.id).first() if not book: return return_data(msg='没有找到该书籍', code=404) book_obj.title = book.title book_obj.new_percent = book.new_percent book_obj.author = book.author book_obj.pub_com = book.pub_com session.commit() session.close() return return_data(data={"title": book.title})
def callback_qutoutiao(ch, method, properties, body): data = json.loads(body.decode()) logger.info("获得任务 : {}".format(data)) ch.basic_ack(delivery_tag=method.delivery_tag) can = True # 校验标识 # 数据处理 data["tags"] = data["tags"].split(",") data["image_path"] = data["image_path"].replace("/", "\\") data["video_path"] = data["video_path"].replace("/", "\\") did = data["id"] res = session.query(TdResourceAccount).get(did) if not res: # 判断任务是否存在 logger.error("趣头条任务{} 状态异常 原因: id=>{} 任务不存在 ".format(data, did)) res.status = 9 re_handler.del_a_key('pubId:' + str(did)) can = False if res.status != 3: # 判断状态是否允许发布 logger.error("趣头条任务{} 状态异常 status=> {}| ".format(did, res.status)) res.remark = "发布状态异常 status=> {}| ".format(res.status) + datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") res.status = 9 re_handler.del_a_key('pubId:' + str(did)) can = False if can: try: status, aid, vid, msg = QutoutiaoUpload(**data).run() logger.info("当前任务{} status: {}, aid: {}, vid: {}, msg:{}".format(did, status, aid, vid, msg)) except KeyError: logger.error("任务 {} 缺少参数 消息=>{}".format(did, data)) res.status = 9 re_handler.del_a_key('pubId:' + str(did)) return except Exception as e: logger.error("上传未知错误 任务id:{} error=>{}".format(did, e)) res.status = 9 res.remark = "未知错误 请检查发文日志 任务id {}".format(did) + '|' + datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") re_handler.del_a_key('pubId:' + str(did)) else: if not status: res.status = 9 res.remark = '发布失败' re_handler.del_a_key('pubId:' + str(did)) else: logger.info("id: {} 发布成功".format(did)) res.remark = '发布成功' res.status = 8 res.article_id = aid res.vid = vid re_handler.del_a_key('pubId:' + str(did)) session.commit()
async def pulish_book(title: str, new_percent: float, author: str = Query('', max_length=50), pub_com: str = None, can_email: bool = True): """ 发布书籍 :param title: 书籍名称 :param new_percent: 新旧程度 :param author: 作者 :param pub_com: 出版社 :return: """ book = Books(title=title, new_percent=new_percent, author=author, pub_com=pub_com) session.add(book) session.commit() return return_data()
def callback_toutiao(ch, method, properties, body): data = json.loads(body.decode()) logger.info("获得任务 : {}".format(data)) ch.basic_ack(delivery_tag=method.delivery_tag) did = data["id"] can = True # 校验标识 acc_flag = False # 格式处理 data["tags"] = data["tags"].split(",") data["image_path"] = data["image_path"].replace("/", "\\") data["video_path"] = data["video_path"].replace("/", "\\") # 校验 res = session.query(TdResourceAccount).get(did) #查看账户是否存在,如果不存在将无法更新cookies try: acc = session.query(TdPlatformAccount).filter( TdPlatformAccount.login_name == data['account']) acc_flag = True except Exception: pass if not res: # 判断任务是否存在 logger.error("当前任务{} 状态异常 原因: id=>{} 任务不存在 ".format(data, did)) res.remark = '任务不存在数据库中,请联系技术人员' res.status = 9 re_handler.del_a_key('pubId:' + str(did)) can = False if res.status != 3: # 判断状态是否允许发布 logger.error("头条任务{} 状态异常 status=> {}| ".format(did, res.status)) res.remark = "发布状态异常 status=> {}| ".format( res.status) + datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") res.status = 9 re_handler.del_a_key('pubId:' + str(did)) can = False if can: try: status, aid, vid, msg, cookies = ToutiaoUpload(**data).run() logger.info("当前任务{} status: {}, aid: {}, vid: {}, msg:{}".format( did, status, aid, vid, msg)) except KeyError: logger.error("任务 {} 缺少参数 消息=>{}".format(did, data)) res.remark = '任务参数传递错误' res.status = 9 re_handler.del_a_key('pubId:' + str(did)) except Exception as e: logger.error("上传未知错误 任务id:{} error=>{}".format(did, e)) res.remark = "未知错误 请检查发文日志 任务id {}".format( did) + '|' + datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") res.status = 9 re_handler.del_a_key('pubId:' + str(did)) else: if not status: res.remark = '发布失败,请重新提交' res.status = 9 res.remark = msg + '|' + datetime.strftime( datetime.now(), "%Y-%m-%d %H:%M:%S") re_handler.del_a_key('pubId:' + str(did)) if acc_flag: acc.cookies = cookies else: logger.info("id: {} 发布成功".format(did)) res.remark = '发布成功' res.status = 8 res.article_id = aid res.vid = vid re_handler.del_a_key('pubId:' + str(did)) if acc_flag: acc.cookies = cookies session.commit()
""" @file: test.py @author: rrh @time: 2021/4/16 10:30 下午 """ from apps.blog.models import * from apps.user.models import * from db.mysql import session, engine # c = Article(title='test', detail='122', user=User(login_name='pipi')) # try: # a = session.add(c) # session.commit() # print(c.id) # except Exception as ex: # print(ex) # session.rollback() import datetime start = datetime.datetime.now() data_list = [] for i in range(40000): data_list.append(dict(title=str(i), detail='')) engine.execute(Article.__table__.insert(), data_list) print('批量提交', datetime.datetime.now() - start) session.commit() print('commit完成', datetime.datetime.now() - start)
async def updata_article_status(status: ArticleStatusEnum, article_ids: List[int] = Query(...)): print(status.value) session.query(Article).filter(Article.id.in_(article_ids)).update({'status': status.value}) session.commit() return return_data(data_list=article_ids)
async def add_comment(article_id: int, comment: AddCommentItem): comment_obj = Comment(detail=comment.detail, img_url=comment.img_url, article_id=article_id) session.add(comment_obj) session.commit() return return_data()
async def like_article(article_id: int): article_obj = session.query(Article).get(article_id) article_obj.like_num += 1 session.commit() # todo 生成一条谁点赞谁的文章记录 return return_data()
async def public_article(article: ArticleItem): article_obj = Article(**article.dict()) session.add(article_obj) session.commit() return return_data()
def callback_dayu(ch, method, properties, body): data = json.loads(body.decode()) logger.info("获得任务 : {}".format(data)) can = True # 校验标识 acc_flag = False # 格式处理 tag_list = data["tags"].split(",") data["tags"] = data["tags"].split(",") data["image_path"] = data["image_path"].replace("/", "\\") data["video_path"] = data["video_path"].replace("/", "\\") # 查询发布状态 did = data["id"] video_type = data["video_type"] res = session.query(TdResourceAccount).get(did) try: acc = session.query(TdPlatformAccount).filter( TdPlatformAccount.login_name == data['account']) acc_flag = True except Exception: pass if not res: # 判断任务是否存在 logger.error("大鱼任务{} 状态异常 原因: id=>{} 任务不存在 ".format(data, did)) res.status = 9 res.remark = '任务不存在' re_handler.del_a_key('pubId:' + str(did)) can = False for _ in range(len(tag_list) - 1): if tag_list[_] == tag_list[_ + 1]: logger.info("标签重复,请重新提交") res.remark = '标签重复,请重新提交' res.status = 9 re_handler.del_a_key('pubId:' + str(did)) can = False if res.status != 3: # 判断状态是否允许发布 logger.error("大鱼任务{} 状态异常 status=> {}| ".format(did, res.status)) res.remark = "发布状态异常 status=> {}| ".format( res.status) + datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") res.remark = '状态不为3,状态异常' re_handler.del_a_key('pubId:' + str(did)) res.status = 9 can = False if video_type not in vtype_set: # 判断视频类型 logger.error("大鱼任务 {} 视频类型错误 =>".format(did, video_type)) res.remark = "视频类型错误 => {}|".format(video_type) + datetime.strftime( datetime.now(), "%Y-%m-%d %H:%M:%S") res.status = 9 re_handler.del_a_key('pubId:' + str(did)) can = False if can: try: status, aid, vid, msg, cookies = DayuUpload(**data).run() logger.info("当前任务{} status: {}, aid: {}, vid: {}, msg:{}".format( did, status, aid, vid, msg)) except KeyError: logger.error("任务 {} 缺少参数 消息=>{}".format(did, data)) res.remark = '消息队列参数缺失' res.status = 9 re_handler.del_a_key('pubId:' + str(did)) except Exception as e: logger.error("上传未知错误 任务id:{} error=>{}".format(did, e)) res.status = 9 res.remark = "未知错误 请检查发文日志 任务id {}".format( did) + '|' + datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") re_handler.del_a_key('pubId:' + str(did)) else: if not status: res.status = 9 if acc_flag: acc.cookies = cookies logger.info('发布失败 {}'.format(msg)) res.remark = '发布失败 {}'.format(msg) res.status = 9 re_handler.del_a_key('pubId:' + str(did)) else: logger.info("id: {} 发布成功".format(did)) res.remark = '发布成功' res.status = 8 res.article_id = aid res.vid = vid if acc_flag: acc.cookies = cookies re_handler.del_a_key('pubId:' + str(did)) res.publish_date = datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S") ch.basic_ack(delivery_tag=method.delivery_tag) session.commit()