autoreger.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. @ Author: Mr.Hat
  3. @ Date: 2024/3/30 14:05
  4. @ Description:
  5. @ History:
  6. """
  7. import random
  8. import traceback
  9. from asyncio import Semaphore, sleep, create_task, wait
  10. from itertools import zip_longest
  11. from core.utils import logger, file_to_list, str_to_file
  12. class AutoReger:
  13. def __init__(self, accounts: list):
  14. self.accounts = accounts
  15. # random.shuffle(self.accounts)
  16. self.success = 0
  17. self.semaphore = None
  18. self.delay = None
  19. @classmethod
  20. def get_accounts(cls, *file_names: str, amount: int = None, auto_creation: tuple = None, with_id: bool = False):
  21. consumables = [file_to_list(file_name) for file_name in file_names]
  22. if amount and consumables[0]:
  23. consumables = [consumable[:amount] for consumable in consumables]
  24. elif amount and auto_creation:
  25. for creation_func in auto_creation:
  26. consumables.append([creation_func() for _ in range(amount)])
  27. consumables[1] = consumables[1][:len(consumables[0])]
  28. if with_id:
  29. consumables.insert(0, (list(range(1, len(consumables[0]) + 1))))
  30. return cls(list(zip_longest(*consumables)))
  31. async def start(self, worker_func: callable, threads: int = 1, delay: tuple = (0, 0)):
  32. if not self.accounts or not self.accounts[0]:
  33. logger.warning("No accounts found :(")
  34. return
  35. logger.info(f"Successfully grabbed {len(self.accounts)} accounts")
  36. self.semaphore = Semaphore(threads)
  37. self.delay = delay
  38. await self.define_tasks(worker_func)
  39. (logger.success if self.success else logger.warning)(
  40. f"Successfully handled {self.success} accounts :)" if self.success
  41. else "No accounts handled :( | Check logs in logs/out.log")
  42. async def define_tasks(self, worker_func: callable):
  43. await wait([create_task(self.worker(account, worker_func)) for account in self.accounts])
  44. async def worker(self, account: tuple, worker_func: callable):
  45. account_id = account[0][:15] if isinstance(account, str) else account[0]
  46. is_success = False
  47. try:
  48. async with self.semaphore:
  49. await self.custom_delay()
  50. is_success = await worker_func(*account)
  51. except Exception as e:
  52. logger.error(f"{account_id} | not handled | error: {e} {traceback.format_exc()}")
  53. self.success += int(is_success or 0)
  54. AutoReger.logs(account_id, account, is_success)
  55. async def custom_delay(self):
  56. if self.delay[1] > 0:
  57. sleep_time = random.uniform(*self.delay)
  58. logger.info(f"Sleep for {sleep_time:.1f} seconds")
  59. await sleep(sleep_time)
  60. @staticmethod
  61. def logs(account_id: str, account: tuple, is_success: bool = False):
  62. if is_success:
  63. log_func = logger.success
  64. log_msg = "handled!"
  65. file_name = "success"
  66. else:
  67. log_func = logger.warning
  68. log_msg = "failed!"
  69. file_name = "failed"
  70. file_msg = "|".join(str(x) for x in account)
  71. str_to_file(f"./logs/{file_name}.txt", file_msg)
  72. log_func(f"Account №{account_id} {log_msg}")