gongzhonghao_accounts_manage.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import sys
  3. import time
  4. import datetime
  5. import schedule
  6. from concurrent.futures.thread import ThreadPoolExecutor
  7. sys.path.append(os.getcwd())
  8. from common.db import MysqlHelper
  9. from common.feishu import Feishu
  10. from common.common import Common
  11. class AccountManager(object):
  12. def __init__(self):
  13. self.unbind_account_list = []
  14. self.user_accounts = None
  15. def select_accounts(self):
  16. select_sql = "SELECT uid FROM crawler_user_v3 where mode = 'author' and source = 'gongzhonghao' and task_id = 27;"
  17. self.user_accounts = MysqlHelper.get_values(
  18. log_type="recommend",
  19. crawler="gongzhonghao",
  20. sql=select_sql,
  21. env="prod",
  22. machine="",
  23. )
  24. def search_user_video(self, uid):
  25. search_sql = f"""SELECT user_id, update_time from crawler_video where user_id = {uid[0]} order by update_time DESC"""
  26. videos = MysqlHelper.get_values(
  27. log_type="recommend",
  28. crawler="gongzhonghao",
  29. sql=search_sql,
  30. env="prod",
  31. machine=""
  32. )
  33. if videos:
  34. user_id, last_update_time = videos[0]
  35. self.change_task_status(user_id, last_update_time)
  36. def change_task_status(self, user_id, last_update_time):
  37. now = time.time()
  38. last_update_stamp = last_update_time.timestamp()
  39. if int(now) - int(last_update_stamp) > 10 * 24 * 60 * 60:
  40. self.unbind_account_list.append(user_id)
  41. update_sql = f"""update crawler_user_v3 set task_id = 0 where uid = {user_id};"""
  42. MysqlHelper.update_values(
  43. log_type="recommend",
  44. crawler="gongzhonghao",
  45. sql=update_sql,
  46. env="prod",
  47. machine=""
  48. )
  49. else:
  50. print("账号正常")
  51. pass
  52. def bot(self):
  53. if self.unbind_account_list:
  54. text = "ID: 是 {} 的公众号,近10天无新视频入库,自动取消绑定,操作日期: {}".format(self.unbind_account_list,
  55. datetime.datetime.now().__str__())
  56. Feishu.bot(
  57. log_type="recommend",
  58. crawler="gongzhonghao_scheduler",
  59. text=text
  60. )
  61. else:
  62. Common.logger(
  63. log_type="recommend",
  64. crawler="gongzhonghao"
  65. ).info("一切正常")
  66. def run():
  67. AcM = AccountManager()
  68. AcM.select_accounts()
  69. with ThreadPoolExecutor(max_workers=8) as pool:
  70. pool.map(AcM.search_user_video, AcM.user_accounts)
  71. AcM.bot()
  72. if __name__ == '__main__':
  73. schedule.every().day.at("00:05").do(run)
  74. while True:
  75. schedule.run_pending()