2023-03-15 19:40:39 +08:00
|
|
|
import tempfile
|
|
|
|
import platform
|
|
|
|
import shutil
|
|
|
|
import glob
|
2023-03-17 04:35:44 +08:00
|
|
|
import time
|
2023-03-15 19:40:39 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def clear_selenium():
|
|
|
|
if platform.uname().system == 'Windows':
|
|
|
|
user_home = [os.environ.get('HOMEDRIVE'), os.environ.get('HOMEPATH')]
|
|
|
|
if user_home[0] and user_home[1]:
|
|
|
|
try:
|
|
|
|
shutil.rmtree('%s%s/.cache/selenium' % (user_home[0], user_home[1]))
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
2023-03-17 04:35:44 +08:00
|
|
|
|
2023-03-15 19:40:39 +08:00
|
|
|
def clear_driver_cache():
|
|
|
|
for cache in ['scoped_dir*', 'chrome_BITS*', 'chrome_url_fetcher*']:
|
|
|
|
for i in glob.glob('%s/%s' % (tempfile.gettempdir(), cache)):
|
|
|
|
try:
|
|
|
|
shutil.rmtree(i)
|
|
|
|
except (FileNotFoundError, PermissionError, WindowsError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-03-17 04:35:44 +08:00
|
|
|
def file_get_content(file, text=None):
|
|
|
|
if not os.path.exists(file):
|
|
|
|
return text
|
|
|
|
return open(file=file, mode='r', encoding='utf-8').read()
|
|
|
|
|
|
|
|
|
|
|
|
def file_put_content(file, text=None):
|
|
|
|
return open(file=file, mode='w', encoding='utf-8').write(text)
|
|
|
|
|
|
|
|
|
|
|
|
regular_clear = True
|
|
|
|
try:
|
|
|
|
selenium_clear_file = '%s/.selenium_clear_last' % tempfile.gettempdir()
|
|
|
|
selenium_clear_last = file_get_content(selenium_clear_file, '0')
|
|
|
|
try:
|
|
|
|
if int(selenium_clear_last) + 86400 < int(time.time()):
|
|
|
|
clear_selenium()
|
|
|
|
clear_driver_cache()
|
|
|
|
file_put_content(selenium_clear_file, str(int(time.time())))
|
|
|
|
except ValueError:
|
|
|
|
os.remove(selenium_clear_file)
|
|
|
|
except Exception:
|
|
|
|
pass
|