tecuts commited on
Commit
73ead9a
·
verified ·
1 Parent(s): 83f5abd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -63,23 +63,24 @@ class DownloadRequest(BaseModel):
63
 
64
 
65
 
66
- # Download a Spotify track and return a download URL
67
 
 
 
68
  @app.post("/spot-track/{track_id}")
69
  def download_spotify_track(track_id: str):
70
  try:
71
  # Clear the downloads directory
72
- for root, dirs, files in os.walk("downloads"):
73
- for file in files:
74
- os.remove(os.path.join(root, file))
75
- for dir in dirs:
76
- shutil.rmtree(os.path.join(root, dir))
77
 
78
  # Download the track using spotloader
79
  logger.info(f"Downloading Spotify track: {track_id}")
80
  spo.download_track(
81
  link_track=f"https://open.spotify.com/track/{track_id}",
82
- output_dir="downloads",
83
  quality_download="VERY_HIGH",
84
  recursive_quality=False,
85
  recursive_download=False,
@@ -87,27 +88,25 @@ def download_spotify_track(track_id: str):
87
  method_save=1
88
  )
89
 
90
-
91
  file_extension = "ogg"
92
  # Recursively search for the file in the downloads directory
93
  filepath = None
94
- for root, dirs, files in os.walk("downloads"):
95
  for file in files:
96
  if file.endswith(f'.{file_extension}'):
97
- filepath = os.path.join(root, file)
98
  break
99
  if filepath:
100
  break
101
 
102
  if not filepath:
103
  raise HTTPException(status_code=500, detail=f"{file_extension} file not found after download")
104
- if filepath:
105
- file_size = os.path.getsize(filepath)
106
- logger.info(f"Downloaded file size: {file_size} bytes")
107
 
108
  # Return the download URL
109
- relative_path = quote(str(os.path.relpath(filepath, "downloads")))
110
- # Remove spaces from the relative path
111
  download_url = f"{BASE_URL}/downloads/{relative_path}"
112
  logger.info(f"Download successful: {download_url}")
113
  return {"download_url": download_url}
@@ -116,6 +115,7 @@ def download_spotify_track(track_id: str):
116
  raise HTTPException(status_code=500, detail=str(e))
117
 
118
 
 
119
  @app.get("/")
120
  def read_root():
121
  return {"message": "running"}
 
63
 
64
 
65
 
 
66
 
67
+
68
+ # Download a Spotify track and return a download URL
69
  @app.post("/spot-track/{track_id}")
70
  def download_spotify_track(track_id: str):
71
  try:
72
  # Clear the downloads directory
73
+ for item in downloads_dir.iterdir():
74
+ if item.is_file():
75
+ item.unlink()
76
+ elif item.is_dir():
77
+ shutil.rmtree(item)
78
 
79
  # Download the track using spotloader
80
  logger.info(f"Downloading Spotify track: {track_id}")
81
  spo.download_track(
82
  link_track=f"https://open.spotify.com/track/{track_id}",
83
+ output_dir=str(downloads_dir),
84
  quality_download="VERY_HIGH",
85
  recursive_quality=False,
86
  recursive_download=False,
 
88
  method_save=1
89
  )
90
 
 
91
  file_extension = "ogg"
92
  # Recursively search for the file in the downloads directory
93
  filepath = None
94
+ for root, dirs, files in os.walk(downloads_dir):
95
  for file in files:
96
  if file.endswith(f'.{file_extension}'):
97
+ filepath = Path(root) / file
98
  break
99
  if filepath:
100
  break
101
 
102
  if not filepath:
103
  raise HTTPException(status_code=500, detail=f"{file_extension} file not found after download")
104
+
105
+ file_size = filepath.stat().st_size
106
+ logger.info(f"Downloaded file size: {file_size} bytes")
107
 
108
  # Return the download URL
109
+ relative_path = quote(str(filepath.relative_to(downloads_dir)))
 
110
  download_url = f"{BASE_URL}/downloads/{relative_path}"
111
  logger.info(f"Download successful: {download_url}")
112
  return {"download_url": download_url}
 
115
  raise HTTPException(status_code=500, detail=str(e))
116
 
117
 
118
+
119
  @app.get("/")
120
  def read_root():
121
  return {"message": "running"}