jerrrycans commited on
Commit
3832fe5
·
verified ·
1 Parent(s): 2508bff

Update ccc.py

Browse files
Files changed (1) hide show
  1. ccc.py +96 -30
ccc.py CHANGED
@@ -7,6 +7,7 @@ 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']
@@ -14,16 +15,51 @@ BASE_URL = "https://tidal.com/browse/track/"
14
 
15
  # Configuration - Edit these values as needed
16
  START_TRACK_ID = 200353201 # Change this to your desired starting track ID
17
- TRACK_CHECK_INTERVAL = 2.1 # Speed of checking in seconds (lower = faster)
 
 
18
 
19
- async def send_webhook_message(track_link, track_info):
20
- data = {
21
- "content": f"{track_link}\n{track_info}"
22
- }
23
- async with aiohttp.ClientSession() as session:
24
- async with session.post(WEBHOOK_URL, json=data) as response:
25
- if response.status != 204:
26
- print(f"Failed to send webhook: {response.status}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  async def check_tidal_track(track_id):
29
  try:
@@ -122,7 +158,6 @@ async def upload_to_service(file_content, song_name, artist_name):
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
 
@@ -140,25 +175,37 @@ async def upload_to_service(file_content, song_name, artist_name):
140
  print(f"Error uploading file: {e}")
141
  return None
142
 
143
- async def process_track(track_id):
144
  try:
145
  track_exists, track_info, song_name, artist_name = await check_tidal_track(track_id)
146
 
147
  if track_exists:
148
  track_link = f"{BASE_URL}{track_id}/u"
149
  print(f"Found valid track: {track_link} - {track_info}")
150
- await send_webhook_message(track_link, track_info)
151
 
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:
163
  print(f"Track {track_id} does not exist")
164
 
@@ -169,19 +216,38 @@ async def process_track(track_id):
169
 
170
  async def main():
171
  current_track_id = START_TRACK_ID
 
172
 
173
  print(f"Starting from track ID: {current_track_id}")
174
 
175
- while True:
176
- try:
177
- track_processed = await process_track(current_track_id)
178
- current_track_id += 1
179
- await asyncio.sleep(TRACK_CHECK_INTERVAL)
 
 
 
 
 
180
 
181
- except Exception as e:
182
- print(f"Error in main loop: {e}")
 
 
 
 
 
183
  await asyncio.sleep(TRACK_CHECK_INTERVAL)
184
- current_track_id += 1
 
 
 
 
 
 
 
 
185
 
186
  if __name__ == "__main__":
187
  asyncio.run(main())
 
7
  import asyncio
8
  from bs4 import BeautifulSoup
9
  import io
10
+ from collections import deque
11
 
12
  # Get webhook URL from environment variable
13
  WEBHOOK_URL = os.environ['web']
 
15
 
16
  # Configuration - Edit these values as needed
17
  START_TRACK_ID = 200353201 # Change this to your desired starting track ID
18
+ TRACK_CHECK_INTERVAL = 0.5 # Speed of checking in seconds (lower = faster)
19
+ WEBHOOK_INTERVAL = 10 # Send webhook messages every 10 seconds
20
+ MAX_CONCURRENT_DOWNLOADS = 30 # Maximum number of concurrent downloads
21
 
22
+ # Queue to store messages to be sent to webhook
23
+ webhook_queue = deque()
24
+ processed_tracks = [] # List to store information about processed tracks
25
+
26
+ async def send_webhook_messages():
27
+ while True:
28
+ if webhook_queue:
29
+ message = webhook_queue.popleft()
30
+ async with aiohttp.ClientSession() as session:
31
+ try:
32
+ async with session.post(WEBHOOK_URL, json=message) as response:
33
+ if response.status != 204:
34
+ print(f"Failed to send webhook: {response.status}")
35
+ except Exception as e:
36
+ print(f"Error sending webhook: {e}")
37
+ webhook_queue.appendleft(message) # Put the message back in the queue
38
+
39
+ # If there are processed tracks to report in a batch
40
+ if processed_tracks:
41
+ # Create a summary message of recently processed tracks
42
+ summary = "Recently processed tracks:\n"
43
+ count = 0
44
+ while processed_tracks and count < 15: # Limit to 15 tracks per message
45
+ track = processed_tracks.pop(0)
46
+ summary += f"• {track['song_name']} - {track['artist_name']}\n"
47
+ count += 1
48
+
49
+ # Add completion stats
50
+ if count > 0:
51
+ message = {
52
+ "content": summary
53
+ }
54
+ async with aiohttp.ClientSession() as session:
55
+ try:
56
+ async with session.post(WEBHOOK_URL, json=message) as response:
57
+ if response.status != 204:
58
+ print(f"Failed to send webhook: {response.status}")
59
+ except Exception as e:
60
+ print(f"Error sending webhook: {e}")
61
+
62
+ await asyncio.sleep(WEBHOOK_INTERVAL)
63
 
64
  async def check_tidal_track(track_id):
65
  try:
 
158
  content_type='audio/flac')
159
 
160
  # Add metadata fields if needed
 
161
  form.add_field('song_name', song_name)
162
  form.add_field('artist', artist_name)
163
 
 
175
  print(f"Error uploading file: {e}")
176
  return None
177
 
178
+ async def process_track(track_id, semaphore):
179
  try:
180
  track_exists, track_info, song_name, artist_name = await check_tidal_track(track_id)
181
 
182
  if track_exists:
183
  track_link = f"{BASE_URL}{track_id}/u"
184
  print(f"Found valid track: {track_link} - {track_info}")
 
185
 
186
+ # Add notification to webhook queue for discovery
187
+ webhook_queue.append({
188
+ "content": f"{track_link}\n{track_info}"
189
+ })
190
+
191
+ # Download and upload with semaphore to limit concurrency
192
+ async with semaphore:
193
+ # Download the track
194
+ download_url = await tidal_downloader(track_id, song_name, artist_name)
195
+ if download_url:
196
+ # Download the file content
197
+ file_content = await download_file(download_url)
198
+ if file_content:
199
+ # Upload the file to the service
200
+ upload_url = await upload_to_service(file_content, song_name, artist_name)
201
+ if upload_url:
202
+ # Add to processed tracks list for batch reporting
203
+ processed_tracks.append({
204
+ 'song_name': song_name,
205
+ 'artist_name': artist_name,
206
+ 'url': upload_url
207
+ })
208
+ print(f"Successfully processed: {song_name} - {artist_name}")
209
  else:
210
  print(f"Track {track_id} does not exist")
211
 
 
216
 
217
  async def main():
218
  current_track_id = START_TRACK_ID
219
+ semaphore = asyncio.Semaphore(MAX_CONCURRENT_DOWNLOADS)
220
 
221
  print(f"Starting from track ID: {current_track_id}")
222
 
223
+ # Start webhook sender task
224
+ webhook_task = asyncio.create_task(send_webhook_messages())
225
+
226
+ # Tasks list to manage concurrent jobs
227
+ tasks = []
228
+
229
+ try:
230
+ while True:
231
+ # Cleanup completed tasks
232
+ tasks = [t for t in tasks if not t.done()]
233
 
234
+ # Create new tasks up to the maximum allowed concurrency
235
+ while len(tasks) < MAX_CONCURRENT_DOWNLOADS * 2: # Allow more tasks in queue than concurrent downloads
236
+ task = asyncio.create_task(process_track(current_track_id, semaphore))
237
+ tasks.append(task)
238
+ current_track_id += 1
239
+
240
+ # Wait a bit before checking for more tasks to create
241
  await asyncio.sleep(TRACK_CHECK_INTERVAL)
242
+ except Exception as e:
243
+ print(f"Error in main loop: {e}")
244
+ finally:
245
+ # Cancel webhook task on exit
246
+ webhook_task.cancel()
247
+
248
+ # Wait for all tasks to complete
249
+ if tasks:
250
+ await asyncio.gather(*tasks, return_exceptions=True)
251
 
252
  if __name__ == "__main__":
253
  asyncio.run(main())