-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathaccess_manager.py
More file actions
113 lines (89 loc) · 3 KB
/
access_manager.py
File metadata and controls
113 lines (89 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
access_manager.py
Token management system for YouTube Audio Converter API.
Handles token-based authentication, expiration, and cleanup of downloaded audio files.
Crafted with precision by Alperen Sümeroğlu — bringing clean code and clean audio together.
"""
import time
import os
from datetime import datetime, timedelta
from pathlib import Path
from constants import EXPIRY_TIME_MINUTES, DOWNLOADS_DIRECTORY
# Stores active tokens with their expiration timestamps
allowed_tokens = {}
# Maps tokens to their respective audio file names
audio_files = {}
def add_token(token: str, filename: str) -> None:
"""
Registers a new token with an expiration time and links it to an audio file.
Args:
token (str): The generated access token.
filename (str): The name of the associated audio file.
"""
expiry = datetime.now() + timedelta(minutes=EXPIRY_TIME_MINUTES)
allowed_tokens[token] = expiry
audio_files[token] = filename
def has_access(token: str) -> bool:
"""
Checks if a given token exists in the allowed tokens.
Args:
token (str): The access token.
Returns:
bool: True if the token is known, False otherwise.
"""
return token in allowed_tokens
def is_valid(token: str) -> bool:
"""
Determines whether a token is still valid based on expiration.
Args:
token (str): The access token.
Returns:
bool: True if the token is still valid, False if expired.
"""
return allowed_tokens[token] >= datetime.now()
def get_audio_file(token: str) -> str:
"""
Retrieves the audio file name associated with a token.
Args:
token (str): The access token.
Returns:
str: The filename of the audio file.
"""
return audio_files[token]
def remove_expired_tokens() -> list:
"""
Identifies expired access tokens, removes them from storage,
and collects filenames of associated audio files for deletion.
Returns:
list: List of filenames to delete.
"""
expired = []
files_to_remove = []
for token in list(allowed_tokens.keys()):
if not is_valid(token):
expired.append(token)
files_to_remove.append(audio_files.pop(token, None))
for token in expired:
allowed_tokens.pop(token, None)
return [f for f in files_to_remove if f]
def delete_expired_files(files: list) -> None:
"""
Deletes expired audio files from the filesystem.
Args:
files (list): List of filenames to delete.
"""
for file in files:
try:
full_path = Path(DOWNLOADS_DIRECTORY) / file
full_path.unlink(missing_ok=True)
except Exception as e:
print(f"Failed to delete file '{file}': {e}")
def manage_tokens() -> None:
"""
Background task that runs indefinitely to remove expired tokens
and delete associated audio files every second.
"""
while True:
expired_files = remove_expired_tokens()
delete_expired_files(expired_files)
time.sleep(1)