jerrrycans commited on
Commit
92eeae5
·
verified ·
1 Parent(s): 799b9fa

Update ccc.py

Browse files
Files changed (1) hide show
  1. ccc.py +23 -30
ccc.py CHANGED
@@ -6,9 +6,7 @@ import tempfile
6
  import aiohttp
7
  import asyncio
8
  from bs4 import BeautifulSoup
9
- import discord
10
- from discord.ext import commands
11
- import json
12
 
13
  # Get webhook URL from environment variable
14
  WEBHOOK_URL = os.environ['web']
@@ -101,15 +99,9 @@ async def download_file(url):
101
  async with aiohttp.ClientSession() as session:
102
  async with session.get(url) as response:
103
  if response.status == 200:
104
- # Create a temporary file to store the downloaded content
105
- with tempfile.NamedTemporaryFile(delete=False, suffix=".flac") as temp_file:
106
- temp_file_path = temp_file.name
107
-
108
- # Write the content to the temporary file
109
- with open(temp_file_path, 'wb') as f:
110
- f.write(await response.read())
111
-
112
- return temp_file_path
113
  else:
114
  print(f"Failed to download file: {response.status}")
115
  return None
@@ -117,35 +109,36 @@ async def download_file(url):
117
  print(f"Error downloading file: {e}")
118
  return None
119
 
120
- async def upload_to_service(file_path, song_name, artist_name):
121
  try:
122
  upload_url = "https://jerrrycans-wholemusic.hf.space/upload"
123
 
124
- # Prepare the form data with the file
125
- with open(file_path, 'rb') as f:
126
- file_content = f.read()
127
-
128
  async with aiohttp.ClientSession() as session:
129
- # Create form data
130
  form = aiohttp.FormData()
131
- form.add_field('file', file_content, filename=f"{song_name} - {artist_name}.flac")
 
 
 
 
 
 
 
 
132
 
133
- # Send the POST request
134
- async with session.post(upload_url, data=form) as response:
135
  if response.status == 200:
136
  result = await response.json()
137
  print(f"Upload successful: {result}")
138
  return result.get('url')
139
  else:
140
- print(f"Upload failed: {response.status}")
 
141
  return None
142
  except Exception as e:
143
  print(f"Error uploading file: {e}")
144
  return None
145
- finally:
146
- # Clean up the temporary file
147
- if file_path and os.path.exists(file_path):
148
- os.unlink(file_path)
149
 
150
  async def process_track(track_id):
151
  try:
@@ -159,11 +152,11 @@ async def process_track(track_id):
159
  # Download the track
160
  download_url = await tidal_downloader(track_id, song_name, artist_name)
161
  if download_url:
162
- # Download the file
163
- file_path = await download_file(download_url)
164
- if file_path:
165
  # Upload the file to the service
166
- upload_url = await upload_to_service(file_path, song_name, artist_name)
167
  if upload_url:
168
  await send_webhook_message(f"Uploaded: {song_name} - {artist_name}", f"URL: {upload_url}")
169
  else:
 
6
  import aiohttp
7
  import asyncio
8
  from bs4 import BeautifulSoup
9
+ import io
 
 
10
 
11
  # Get webhook URL from environment variable
12
  WEBHOOK_URL = os.environ['web']
 
99
  async with aiohttp.ClientSession() as session:
100
  async with session.get(url) as response:
101
  if response.status == 200:
102
+ # Read the file content directly into memory
103
+ file_content = await response.read()
104
+ return file_content
 
 
 
 
 
 
105
  else:
106
  print(f"Failed to download file: {response.status}")
107
  return None
 
109
  print(f"Error downloading file: {e}")
110
  return None
111
 
112
+ async def upload_to_service(file_content, song_name, artist_name):
113
  try:
114
  upload_url = "https://jerrrycans-wholemusic.hf.space/upload"
115
 
 
 
 
 
116
  async with aiohttp.ClientSession() as session:
117
+ # Create form data with the correct file name
118
  form = aiohttp.FormData()
119
+ form.add_field('file',
120
+ file_content,
121
+ filename=f"{song_name}.flac",
122
+ content_type='audio/flac')
123
+
124
+ # Add metadata fields if needed
125
+ # The server might be expecting these fields
126
+ form.add_field('song_name', song_name)
127
+ form.add_field('artist', artist_name)
128
 
129
+ # Send the POST request with a timeout
130
+ async with session.post(upload_url, data=form, timeout=60) as response:
131
  if response.status == 200:
132
  result = await response.json()
133
  print(f"Upload successful: {result}")
134
  return result.get('url')
135
  else:
136
+ error_text = await response.text()
137
+ print(f"Upload failed: {response.status} - {error_text}")
138
  return None
139
  except Exception as e:
140
  print(f"Error uploading file: {e}")
141
  return None
 
 
 
 
142
 
143
  async def process_track(track_id):
144
  try:
 
152
  # Download the track
153
  download_url = await tidal_downloader(track_id, song_name, artist_name)
154
  if download_url:
155
+ # Download the file content
156
+ file_content = await download_file(download_url)
157
+ if file_content:
158
  # Upload the file to the service
159
+ upload_url = await upload_to_service(file_content, song_name, artist_name)
160
  if upload_url:
161
  await send_webhook_message(f"Uploaded: {song_name} - {artist_name}", f"URL: {upload_url}")
162
  else: