""" @author: luojunhui """ import datetime import traceback from applications import longArticlesMySQL, bot from coldStartTasks.crawler.weixinCategoryCrawler import weixinCategory from coldStartTasks.publish.publishCategoryArticles import CategoryColdStartTask DEFAULT_CATEGORY_LIST = ['1030-手动挑号'] class AccountColdStartDailyTask(object): """ 账号冷启动代码 """ def __init__(self): """ """ self.db_client = None def init_db(self): """ 初始化数据库 :return: """ try: self.db_client = longArticlesMySQL() return True except Exception as e: bot( title='账号抓取任务, 冷启动数据库连接失败', detail={ "error": str(e), "error_msg": traceback.format_exc() } ) return False def crawler_task(self, category_list): """ :return: """ # 初始化category抓取类 try: weixin_category_crawler = weixinCategory(db_client=self.db_client) weixin_category_crawler.deal(category_list=category_list) bot( title="账号冷启动任务,抓取完成", detail={ "finish_time": datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'), "category": category_list }, mention=False ) except Exception as e: bot( title="账号抓取冷启动任务,抓取失败", detail={ "error": str(e), "error_msg": traceback.format_exc() } ) def publish_task(self, category_list, article_source): """ 将账号文章发布到aigc抓取计划,并且绑定生成计划 :param category_list: 文章品类 :param article_source: 文章来源(toutiao or weixin) :return: """ try: weixin_category_publisher = CategoryColdStartTask(db_client=self.db_client) weixin_category_publisher.do_job( category_list=category_list, article_source=article_source ) bot( title="账号冷启任务,发布完成", detail={ "finish_time": datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'), "category": category_list }, mention=False ) except Exception as e: bot( title="账号发布冷启动任务,发布失败", detail={ "error": str(e), "error_msg": traceback.format_exc() } ) def main(category_list=None, article_source=None): """ main job, use crontab to do job daily todo: 1. 开放一个输入可以输入指定品类 2. 增加对指定账号的抓取&&发布 :return: """ if not category_list: category_list = DEFAULT_CATEGORY_LIST if not article_source: article_source = 'weixin' task = AccountColdStartDailyTask() if task.init_db(): if article_source == 'weixin': task.crawler_task(category_list=category_list) task.publish_task(category_list=category_list, article_source=article_source) if __name__ == '__main__': # 执行微信抓取发布 main() # 执行头条发布 main( category_list=['history', 'tech', 'finance', 'entertainment'], article_source='toutiao' )