telegram 群组监控 / 群组功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.8 KiB

8 months ago
  1. import os
  2. root_path = os.path.abspath(os.path.dirname(__file__)).split('telegram-crawler')[0] + "telegram-crawler"
  3. import time
  4. from loguru import logger
  5. import shutil
  6. """
  7. 0 0 * * * cd /opt/crawl/telegram/telegram-crawler && /root/anaconda3/envs/python3.8/bin/python clean.py > /dev/null 2>&1
  8. cd /opt/crawl/telegram/telegram-crawler && /root/anaconda3/envs/python3.8/bin/python clean.py > /dev/null 2>&1
  9. """
  10. def release_resources(days=3):
  11. """
  12. :param days:
  13. :return:
  14. """
  15. resources_dir = 'tg_module/resources'
  16. profile_dir = 'tg_module/profile'
  17. files = []
  18. # 获取目录下所有文件
  19. resources_files = [os.path.join(resources_dir, f) for f in os.listdir(resources_dir)]
  20. profile_files = [os.path.join(profile_dir, f) for f in os.listdir(profile_dir)]
  21. files.extend(resources_files)
  22. files.extend(profile_files)
  23. now = time.time()
  24. delay = days * 24 * 60 * 60
  25. delay = days * 60 * 60
  26. logger.info(f"获取数量共:{len(files)};resources: {len(resources_files)}, profile: {len(profile_files)}")
  27. limited_time = now - delay
  28. # 按照创建时间排序
  29. files = sorted(files, key=os.path.getctime)
  30. count = 0
  31. for i in files:
  32. create_time = os.path.getctime(i)
  33. if create_time < limited_time: # 需要删除
  34. if os.path.isfile(i) or os.path.islink(i): # 如果是文件则直接删除
  35. os.remove(i)
  36. count += 1
  37. else: # 删除文件夹及其所有内容
  38. shutil.rmtree(i)
  39. logger.debug(f"删除了一个文件夹: {i}")
  40. else:
  41. break
  42. logger.info(f"删除文件数量:{count}")
  43. if __name__ == '__main__':
  44. release_resources()