Last active
March 16, 2018 16:23
-
-
Save beng/902e434b7e4fe7923800050d8905f045 to your computer and use it in GitHub Desktop.
find the URL required to download a song from mixcloud
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
""" | |
Download songs from mixcloud | |
Usage: python mixcloud-downloader.py <url to song> | |
Example (download a single song): | |
$ python mixcloud-downloader.py https://www.mixcloud.com/gridface/756-farleydave03-wbmx-chicago-1984-85/ | |
Advanced example (download multiple songs): | |
$ <urls.txt xargs -n1 -P4 python mixcloud-downloader.py | |
""" | |
import os | |
import sys | |
import urllib | |
import requests | |
from bs4 import BeautifulSoup | |
BASE_DL_URL = "https://stream1.mixcloud.com/c/m4a/64/" | |
def pipe(val, *fns): | |
_val = val | |
for fn in fns: | |
_val = fn(_val) | |
return _val | |
def extract_preview_link(html): | |
return html.find('div', class_='cloudcast-page-play-button-container').find('span').attrs.get('m-preview') | |
def build_download_uri(preview_link): | |
preview_uri = preview_link.split('mixcloud.com/previews/')[-1] | |
return os.path.splitext(preview_uri)[0] + '.m4a' | |
def build_download_url(uri): | |
return urllib.parse.urljoin(BASE_DL_URL, uri) | |
def request(url): | |
resp = requests.get(url) | |
resp.raise_for_status() | |
return resp | |
def to_html(resp): | |
return BeautifulSoup(resp.text, "html.parser") | |
if __name__ == "__main__": | |
pipe( | |
sys.argv[1], | |
request, | |
to_html, | |
extract_preview_link, | |
build_download_uri, | |
build_download_url, | |
print) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
, line 62
print)
^
SyntaxError: invalid syntax
No results using the original pipe as well. Tried with Python 2.7 and 3.6.