tecuts commited on
Commit
9d6531a
·
verified ·
1 Parent(s): a4df8ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py CHANGED
@@ -12,6 +12,69 @@ from datetime import datetime
12
  logging.basicConfig(level=logging.INFO)
13
  logger = logging.getLogger(__name__)
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  app = FastAPI(
16
  title="GAMDL API",
17
  description="API for downloading Google Drive files using gamdl",
@@ -36,6 +99,8 @@ class DownloadResponse(BaseModel):
36
 
37
  @app.post("/download", response_model=DownloadResponse)
38
  async def download_file(request: DownloadRequest):
 
 
39
  try:
40
  # Create a unique subdirectory for this download
41
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
 
12
  logging.basicConfig(level=logging.INFO)
13
  logger = logging.getLogger(__name__)
14
 
15
+ def env_to_cookies(env_content: str, output_file: str) -> None:
16
+ """Convert environment variable content back to cookie file"""
17
+ try:
18
+ # Extract content from env format
19
+ if '="' not in env_content:
20
+ raise ValueError("Invalid env content format")
21
+
22
+ content = env_content.split('="', 1)[1].strip('"')
23
+
24
+ # Replace escaped newlines with actual newlines
25
+ cookie_content = content.replace('\\n', '\n')
26
+
27
+ # Write to cookie file
28
+ with open(output_file, 'w') as f:
29
+ f.write(cookie_content)
30
+
31
+ except Exception as e:
32
+ raise ValueError(f"Error converting to cookie file: {str(e)}")
33
+
34
+ def save_to_env_file(env_content: str, env_file: str = '.env') -> None:
35
+ """Save environment variable content to .env file"""
36
+ try:
37
+ with open(env_file, 'w') as f:
38
+ f.write(env_content)
39
+ #print(f"Successfully saved to {env_file}")
40
+ except Exception as e:
41
+ raise ValueError(f"Error saving to env file: {str(e)}")
42
+
43
+ def env_to_cookies_from_env(output_file: str) -> None:
44
+ """Convert environment variable from .env file to cookie file"""
45
+ try:
46
+ load_dotenv() # Load from .env file
47
+ env_content = os.getenv('COOKIES')
48
+ #print(f"Printing env content: \n{env_content}")
49
+ if not env_content:
50
+ raise ValueError("COOKIES not found in .env file")
51
+
52
+ env_to_cookies(f'COOKIES="{env_content}"', output_file)
53
+ except Exception as e:
54
+ raise ValueError(f"Error converting to cookie file: {str(e)}")
55
+
56
+ def get_cookies():
57
+ """Get cookies from environment variable"""
58
+ load_dotenv()
59
+ cookie_content = os.getenv('COOKIES')
60
+ #print(cookie_content)
61
+ if not cookie_content:
62
+ raise ValueError("COOKIES environment variable not set")
63
+ return cookie_content
64
+
65
+ def create_temp_cookie_file():
66
+ """Create temporary cookie file from environment variable"""
67
+ temp_cookie = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.txt')
68
+ try:
69
+ cookie_content = get_cookies()
70
+ # Replace escaped newlines with actual newlines
71
+ cookie_content = cookie_content.replace('\\n', '\n')
72
+ temp_cookie.write()
73
+ temp_cookie.flush()
74
+ return Path(temp_cookie.name)
75
+ finally:
76
+ temp_cookie.close()
77
+
78
  app = FastAPI(
79
  title="GAMDL API",
80
  description="API for downloading Google Drive files using gamdl",
 
99
 
100
  @app.post("/download", response_model=DownloadResponse)
101
  async def download_file(request: DownloadRequest):
102
+ cookiefile = "cookies.txt"
103
+ env_to_cookies_from_env("cookies.txt")
104
  try:
105
  # Create a unique subdirectory for this download
106
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")