def get_article_list(status: ArticleStatusEnum, article_type: str = 'public', free: bool = True, user_id: int = Query(default=0)): article_obj_list = session.query(Article).filter_by(type=article_type, free=free) if user_id: article_obj_list = article_obj_list.filter(Article.user_id == user_id) return return_data(data_list=article_obj_list.all())
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 user_articles(user_id: int): articles = session.query(Article).filter(Article.user_id == user_id).all() return return_data(data_list=articles)
async def user_profile(user_id: int): user_obj = session.query(User).get(user_id) return return_data(data=user_obj)
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()
async def book_list(user_id: int = Form(None)): books = session.query(Books).all() return return_data(data_list=books)
async def newst_comment_list(user_id: int): comments_list = session.query(Comment).filter() return return_data(data_list=comments_list)
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 comment_operate_detail(id: int): comment_operate = session.query(CommentOperate).filter(CommentOperate.id == id).first() return return_data(data=comment_operate)
async def comment_detail(comment_id: int): comment = session.query(Comment).filter(Comment.id == comment_id).first() return return_data(data=comment)
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 article_detail(article_id: int): article_obj = session.query(Article).get(article_id) return return_data(data=article_obj)
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()