Last active
December 10, 2022 18:38
-
-
Save neverlink/6e9449622883edf6cce7af450c410a33 to your computer and use it in GitHub Desktop.
Split your Soundcloud likes into playlists of 500, each named after the date liked of the first and last song in that specific batch. Useful to users with a large amount of likes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
import pprint | |
import requests | |
from datetime import datetime | |
auth_token = 'OAuth XYZ123' | |
client_id = 'ABCDEF' | |
user_id = '123456' | |
headers = { | |
'Accept': 'application/json, text/javascript, */*; q=0.01', | |
'Authorization': auth_token, | |
'Origin': 'https://soundcloud.com' | |
} | |
params = { | |
'client_id': client_id, | |
'limit': '1000', | |
'app_version': '1665133451' | |
} | |
def attempt(method, url, data=None): | |
while True: | |
resp = method(url, headers=headers, params=params, data=data) | |
if resp == {}: | |
print(kwargs) | |
print('Empty response. Retrying...') | |
time.sleep(2) | |
elif resp.status_code // 100 != 2: | |
print(f'Response code: {resp.status_code}') | |
time.sleep(2) | |
else: | |
return resp.json() | |
def fetch_likes(url): | |
return attempt(requests.get, url) | |
def dump_to_playlist(songs): | |
start_date = datetime.strptime(songs[-1]['date_liked'], '%Y-%m-%d').strftime('%b %Y') | |
end_date = datetime.strptime(songs[0]['date_liked'], '%Y-%m-%d').strftime('%b %Y') | |
data = { | |
"playlist": { | |
"title": f"{start_date} - {end_date}", | |
"sharing": "private", | |
"tracks": [x['track_id'] for x in songs] | |
} | |
} | |
resp = attempt(requests.post, 'https://api-v2.soundcloud.com/playlists', data=json.dumps(data)) | |
print(f"Playlist created: {resp['permalink_url']}") | |
def extract_tracks(resp): | |
songs = [] | |
for x, song in enumerate(resp['collection']): | |
if 'playlist' not in song: | |
songs.append( | |
{ | |
'title': song['track']['title'], | |
'artist': song['track']['user']['username'], | |
'track_id': song['track']['id'], | |
'date_liked': song['created_at'][:10] | |
} | |
) | |
print(f"Fetched {len(resp['collection'])} new songs.") | |
return songs | |
liked_songs = [] | |
initial_url = f'https://api-v2.soundcloud.com/users/{user_id}/likes' | |
initial_resp = fetch_likes(initial_url) | |
liked_songs.extend(extract_tracks(initial_resp)) | |
while initial_resp['next_href'] is not None: | |
resp = fetch_likes(initial_resp['next_href']) | |
initial_resp = resp | |
liked_songs.extend(extract_tracks(resp)) | |
time.sleep(1) | |
with open('liked_songs.txt', 'w', encoding='utf-8') as file: | |
print(f"Total Songs: {len(liked_songs)}") | |
json.dump(liked_songs, file, indent=2, ensure_ascii=False) | |
splits = [liked_songs[i:i + 500] for i in range(0, len(liked_songs), 500)] | |
for split in splits: | |
dump_to_playlist(split) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment