simplify, un-userspaced

This commit is contained in:
simon 2022-04-30 16:38:23 +07:00
parent 9b107b9940
commit 4e3d71e0e3
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -17,10 +17,8 @@ class CookieHandler:
CONFIG = AppConfig().config CONFIG = AppConfig().config
CACHE_PATH = CONFIG["application"]["cache_dir"] CACHE_PATH = CONFIG["application"]["cache_dir"]
COOKIE_FILE_NAME = "cookies.google.txt" COOKIE_FILE_NAME = "cookies.google.txt"
COOKIE_KEY = "cookie"
def __init__(self, user_id): COOKIE_PATH = "cookie.txt"
self.cookie_path = f"cookie_{user_id}.txt"
self.cookie_key = f"{user_id}:cookie"
def import_cookie(self): def import_cookie(self):
"""import cookie from file""" """import cookie from file"""
@ -30,24 +28,24 @@ class CookieHandler:
with open(import_path, encoding="utf-8") as cookie_file: with open(import_path, encoding="utf-8") as cookie_file:
cookie = cookie_file.read() cookie = cookie_file.read()
RedisArchivist().set_message(self.cookie_key, cookie, expire=False) RedisArchivist().set_message(self.COOKIE_KEY, cookie, expire=False)
os.remove(import_path) os.remove(import_path)
print("cookie: import successfully") print("cookie: import successfully")
def use(self): def use(self):
"""make cookie available in FS""" """make cookie available in FS"""
cookie = RedisArchivist().get_message(self.cookie_key) cookie = RedisArchivist().get_message(self.COOKIE_KEY)
with open(self.cookie_path, "w", encoding="utf-8") as cookie_file: with open(self.COOKIE_PATH, "w", encoding="utf-8") as cookie_file:
cookie_file.write(cookie) cookie_file.write(cookie)
print("cookie: made available") print("cookie: made available")
return self.cookie_path return self.COOKIE_PATH
def hide(self): def hide(self):
"""hide cookie file if not in use""" """hide cookie file if not in use"""
try: try:
os.remove(self.cookie_path) os.remove(self.COOKIE_PATH)
except FileExistsError: except FileExistsError:
print("cookie: not available") print("cookie: not available")
return return
@ -57,7 +55,7 @@ class CookieHandler:
def revoke(self): def revoke(self):
"""revoke cookie""" """revoke cookie"""
self.hide() self.hide()
RedisArchivist().del_message(self.cookie_key) RedisArchivist().del_message(self.COOKIE_KEY)
print("cookie: revoked") print("cookie: revoked")
def validate(self): def validate(self):
@ -68,7 +66,7 @@ class CookieHandler:
"quiet": True, "quiet": True,
"skip_download": True, "skip_download": True,
"extract_flat": True, "extract_flat": True,
"cookiefile": self.cookie_path, "cookiefile": self.COOKIE_PATH,
} }
try: try:
response = yt_dlp.YoutubeDL(yt_obs).extract_info(url) response = yt_dlp.YoutubeDL(yt_obs).extract_info(url)