account_pool.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2023-04-24
  4. ---------
  5. @summary: 竞品账号管理
  6. ---------
  7. @author: Dzr
  8. """
  9. from common.databases import mongo_table, object_id
  10. from common.log import logger
  11. from common.utils import now_date
  12. class AccountManagePool:
  13. def __init__(self, db: str, table: str):
  14. self.account_tab = mongo_table(db, table)
  15. self.sleep_interval = 300
  16. self.output_log = True
  17. list_attr = dict(
  18. name='crawl_list',
  19. lock=dict(crawl_list=True),
  20. release=dict(crawl_list=False),
  21. )
  22. detail_attr = dict(
  23. name='crawl_detail',
  24. lock=dict(crawl_detail=True),
  25. release=dict(crawl_detail=False),
  26. )
  27. self._schedule = {'list': list_attr, 'detail': detail_attr}
  28. def _update_account(self, uid, **kwargs):
  29. kwargs['update_time'] = now_date()
  30. self.account_tab.update_one({'_id': uid}, {'$set': kwargs})
  31. def _find_account(self, query, *args, **kwargs):
  32. return self.account_tab.find_one(query, *args, **kwargs)
  33. def _find_and_update_account(self, query, update, *args, **kwargs):
  34. update['$set']['update_time'] = now_date()
  35. return self.account_tab.find_one_and_update(query, update, *args, **kwargs)
  36. def release_account(self, uid, crawl_type, ip):
  37. if isinstance(uid, str):
  38. uid = object_id(uid)
  39. query = dict(_id=uid)
  40. account = self._find_account(query)
  41. application = self._schedule[crawl_type]['name']
  42. response = {'status': 'duplicate'}
  43. if account is not None and account[application]:
  44. release = self._schedule[crawl_type]['release']
  45. self._update_account(uid, **release)
  46. logger.info(f'[账号池]释放账号:{uid},用途:{crawl_type}, {ip}')
  47. response = {'status': 'ok'}
  48. return response
  49. def lock_account(self, site, crawl_type, ip):
  50. release = self._schedule[crawl_type]['release']
  51. query = dict(site=site, **release)
  52. lock = self._schedule[crawl_type]['lock']
  53. update = {'$set': lock}
  54. sort = [('update_time', 1)]
  55. account = self._find_and_update_account(query, update, sort=sort)
  56. if account is not None:
  57. _account = {}
  58. for key, val in account.items():
  59. if key == '_id':
  60. _account[key] = str(val)
  61. else:
  62. _account[key] = val
  63. account = _account
  64. logger.info(f'[账号池]锁定账号:{account["_id"]},用途:{crawl_type},{ip}')
  65. return account