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

Update ccc.py

Browse files
Files changed (1) hide show
  1. ccc.py +153 -44
ccc.py CHANGED
@@ -1,8 +1,14 @@
1
  import time
2
  import requests
3
  import random
4
- from bs4 import BeautifulSoup
5
  import os
 
 
 
 
 
 
 
6
 
7
  # Get webhook URL from environment variable
8
  WEBHOOK_URL = os.environ['web']
@@ -12,74 +18,177 @@ BASE_URL = "https://tidal.com/browse/track/"
12
  START_TRACK_ID = 1186 # Change this to your desired starting track ID
13
  TRACK_CHECK_INTERVAL = 2.1 # Speed of checking in seconds (lower = faster)
14
 
15
- def send_webhook_message(track_link, track_info):
16
  data = {
17
  "content": f"{track_link}\n{track_info}"
18
  }
19
- response = requests.post(WEBHOOK_URL, json=data)
20
-
21
- if response.status_code != 204:
22
- print(f"Failed to send webhook: {response.status_code}")
23
 
24
- def check_tidal_track(track_id):
25
  try:
26
  url = f"{BASE_URL}{track_id}/u"
27
  headers = {
28
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"
29
  }
30
- response = requests.get(url, headers=headers, timeout=5)
31
 
32
- # Check status code first - 200 means it exists, 404 means it doesn't
33
- if response.status_code == 200:
34
- soup = BeautifulSoup(response.content, 'html.parser')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- # Try to find the track title
37
- title_element = soup.find('h1', class_="font-size-regular mt-0 mb-0 ellipsis-double overflow-hidden")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- if title_element:
40
- # Try to find artist information - fixed to match the correct class
41
- artist_element = soup.find('a', class_="artist-list-link")
42
-
43
- if not artist_element:
44
- # Try alternative class names that might be used for artist links
45
- artist_element = soup.find('a', class_="text-dec-none visible-offset-0 artist-list-link hover-desktop")
46
-
47
- artist_name = artist_element.text.strip() if artist_element else "Unknown Artist"
48
-
49
- track_info = f"{title_element.text}\n{artist_name}"
50
- return True, track_info
51
- else:
52
- # If we got a 200 but couldn't find the title, still consider it valid
53
- return True, "Unknown Track"
54
 
55
- return False, None
56
  except Exception as e:
57
- print(f"Error checking track {track_id}: {e}")
58
- return False, None
59
 
60
- def main():
61
  current_track_id = START_TRACK_ID
62
 
63
  print(f"Starting from track ID: {current_track_id}")
64
 
65
  while True:
66
  try:
67
- track_exists, track_info = check_tidal_track(current_track_id)
68
-
69
- if track_exists:
70
- track_link = f"{BASE_URL}{current_track_id}/u"
71
- print(f"Found valid track: {track_link} - {track_info}")
72
- send_webhook_message(track_link, track_info)
73
- else:
74
- print(f"Track {current_track_id} does not exist")
75
-
76
  current_track_id += 1
77
- time.sleep(TRACK_CHECK_INTERVAL)
78
 
79
  except Exception as e:
80
  print(f"Error in main loop: {e}")
81
- time.sleep(TRACK_CHECK_INTERVAL)
82
  current_track_id += 1
83
 
84
  if __name__ == "__main__":
85
- main()
 
1
  import time
2
  import requests
3
  import random
 
4
  import os
5
+ 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']
 
18
  START_TRACK_ID = 1186 # Change this to your desired starting track ID
19
  TRACK_CHECK_INTERVAL = 2.1 # Speed of checking in seconds (lower = faster)
20
 
21
+ async def send_webhook_message(track_link, track_info):
22
  data = {
23
  "content": f"{track_link}\n{track_info}"
24
  }
25
+ async with aiohttp.ClientSession() as session:
26
+ async with session.post(WEBHOOK_URL, json=data) as response:
27
+ if response.status != 204:
28
+ print(f"Failed to send webhook: {response.status}")
29
 
30
+ async def check_tidal_track(track_id):
31
  try:
32
  url = f"{BASE_URL}{track_id}/u"
33
  headers = {
34
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36"
35
  }
 
36
 
37
+ async with aiohttp.ClientSession() as session:
38
+ async with session.get(url, headers=headers, timeout=5) as response:
39
+ # Check status code first - 200 means it exists, 404 means it doesn't
40
+ if response.status == 200:
41
+ content = await response.text()
42
+ soup = BeautifulSoup(content, 'html.parser')
43
+
44
+ # Try to find the track title
45
+ title_element = soup.find('h1', class_="font-size-regular mt-0 mb-0 ellipsis-double overflow-hidden")
46
+
47
+ if title_element:
48
+ # Try to find artist information - fixed to match the correct class
49
+ artist_element = soup.find('a', class_="artist-list-link")
50
+
51
+ if not artist_element:
52
+ # Try alternative class names that might be used for artist links
53
+ artist_element = soup.find('a', class_="text-dec-none visible-offset-0 artist-list-link hover-desktop")
54
+
55
+ artist_name = artist_element.text.strip() if artist_element else "Unknown Artist"
56
+
57
+ track_info = f"{title_element.text}\n{artist_name}"
58
+ return True, track_info, title_element.text, artist_name
59
+ else:
60
+ # If we got a 200 but couldn't find the title, still consider it valid
61
+ return True, "Unknown Track", "Unknown Track", "Unknown Artist"
62
+
63
+ return False, None, None, None
64
+ except Exception as e:
65
+ print(f"Error checking track {track_id}: {e}")
66
+ return False, None, None, None
67
+
68
+ async def tidal_downloader(track_id, song_name, artist_name):
69
+ api_url = "https://hifi.kratosgodofwar6076.workers.dev/getSongLink"
70
+ payload = {
71
+ "trackId": track_id,
72
+ "quality": "3"
73
+ }
74
+
75
+ try:
76
+ async with aiohttp.ClientSession() as session:
77
+ async with session.post(api_url, json=payload) as response:
78
+ if response.status == 200:
79
+ data = await response.json()
80
+
81
+ download_url = None
82
+ if isinstance(data, list) and len(data) > 2:
83
+ download_url = data[2].get('OriginalTrackUrl')
84
+
85
+ if download_url:
86
+ print(f"Found download URL for track {track_id}")
87
+ return download_url
88
+ else:
89
+ print(f"No download URL found for track {track_id}")
90
+ return None
91
+ else:
92
+ print(f"Error getting download URL: {response.status}")
93
+ return None
94
+
95
+ except Exception as e:
96
+ print(f"Error in tidal_downloader: {str(e)}")
97
+ return None
98
+
99
+ async def download_file(url):
100
+ try:
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
116
+ except Exception as e:
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:
152
+ track_exists, track_info, song_name, artist_name = await check_tidal_track(track_id)
153
+
154
+ if track_exists:
155
+ track_link = f"{BASE_URL}{track_id}/u"
156
+ print(f"Found valid track: {track_link} - {track_info}")
157
+ await send_webhook_message(track_link, track_info)
158
 
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:
170
+ print(f"Track {track_id} does not exist")
 
 
 
171
 
172
+ return track_exists
173
  except Exception as e:
174
+ print(f"Error processing track {track_id}: {e}")
175
+ return False
176
 
177
+ async def main():
178
  current_track_id = START_TRACK_ID
179
 
180
  print(f"Starting from track ID: {current_track_id}")
181
 
182
  while True:
183
  try:
184
+ track_processed = await process_track(current_track_id)
 
 
 
 
 
 
 
 
185
  current_track_id += 1
186
+ await asyncio.sleep(TRACK_CHECK_INTERVAL)
187
 
188
  except Exception as e:
189
  print(f"Error in main loop: {e}")
190
+ await asyncio.sleep(TRACK_CHECK_INTERVAL)
191
  current_track_id += 1
192
 
193
  if __name__ == "__main__":
194
+ asyncio.run(main())