Skip to content

Instantly share code, notes, and snippets.

@akleemans
Last active August 10, 2020 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akleemans/e1de3548484e037d0bd419ad6cedfe6d to your computer and use it in GitHub Desktop.
Save akleemans/e1de3548484e037d0bd419ad6cedfe6d to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''Download the current Spotify Top 50 from Youtube.'''
import urllib.request
import subprocess
# fetch charts
print('Downloading charts...')
req = urllib.request.Request('https://spotifycharts.com/regional/global/weekly/latest/download', headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req) as page:
charts = page.read().decode('utf-8')
for row in charts.split('\n')[2:52]:
title = row.split(',')[2] + ' - ' + row.split(',')[1]
title = title.replace('"', '')
print('Searching for:', title)
title = urllib.parse.quote(title)
content = urllib.request.urlopen('http://www.youtube.com/results?search_query=' + title).read().decode('utf-8')
yt_link = content.split('"videoId":"')[1].split('"')[0]
download_query = 'youtube-dl --extract-audio --audio-format mp3 https://www.youtube.com/watch?v=' + yt_link
subprocess.run(download_query.split())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment