account_cold_start_daily.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. @author: luojunhui
  3. """
  4. import datetime
  5. import traceback
  6. from applications import longArticlesMySQL, bot
  7. from coldStartTasks.crawler.weixinCategoryCrawler import weixinCategory
  8. from coldStartTasks.publish.publishCategoryArticles import CategoryColdStartTask
  9. DEFAULT_CATEGORY_LIST = ['1030-手动挑号']
  10. class AccountColdStartDailyTask(object):
  11. """
  12. 账号冷启动代码
  13. """
  14. def __init__(self):
  15. """
  16. """
  17. self.db_client = None
  18. def init_db(self):
  19. """
  20. 初始化数据库
  21. :return:
  22. """
  23. try:
  24. self.db_client = longArticlesMySQL()
  25. return True
  26. except Exception as e:
  27. bot(
  28. title='账号抓取任务, 冷启动数据库连接失败',
  29. detail={
  30. "error": str(e),
  31. "error_msg": traceback.format_exc()
  32. }
  33. )
  34. return False
  35. def crawler_task(self, category_list):
  36. """
  37. :return:
  38. """
  39. # 初始化category抓取类
  40. try:
  41. weixin_category_crawler = weixinCategory(db_client=self.db_client)
  42. weixin_category_crawler.deal(category_list=category_list)
  43. bot(
  44. title="账号冷启动任务,抓取完成",
  45. detail={
  46. "finish_time": datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
  47. "category": category_list
  48. },
  49. mention=False
  50. )
  51. except Exception as e:
  52. bot(
  53. title="账号抓取冷启动任务,抓取失败",
  54. detail={
  55. "error": str(e),
  56. "error_msg": traceback.format_exc()
  57. }
  58. )
  59. def publish_task(self, category_list):
  60. """
  61. 将账号文章发布到aigc抓取计划,并且绑定生成计划
  62. :param category_list:
  63. :return:
  64. """
  65. try:
  66. weixin_category_publisher = CategoryColdStartTask(db_client=self.db_client)
  67. weixin_category_publisher.do_job(
  68. category_list=category_list
  69. )
  70. bot(
  71. title="账号冷启任务,发布完成",
  72. detail={
  73. "finish_time": datetime.datetime.today().strftime('%Y-%m-%d %H:%M:%S'),
  74. "category": category_list
  75. },
  76. mention=False
  77. )
  78. except Exception as e:
  79. bot(
  80. title="账号发布冷启动任务,抓取失败",
  81. detail={
  82. "error": str(e),
  83. "error_msg": traceback.format_exc()
  84. }
  85. )
  86. def main(category_list=None):
  87. """
  88. main job, use crontab to do job daily
  89. todo: 1. 开放一个输入可以输入指定品类 2. 增加对指定账号的抓取&&发布
  90. :return:
  91. """
  92. if not category_list:
  93. category_list = DEFAULT_CATEGORY_LIST
  94. task = AccountColdStartDailyTask()
  95. if task.init_db():
  96. task.crawler_task(category_list=category_list)
  97. task.publish_task(category_list=category_list)
  98. if __name__ == '__main__':
  99. main()