Skip to content

Instantly share code, notes, and snippets.

@moopet
Last active December 19, 2015 10:49
Show Gist options
  • Save moopet/5943531 to your computer and use it in GitHub Desktop.
Save moopet/5943531 to your computer and use it in GitHub Desktop.
Hack to let you locate and download mp3s using mp3skull.com as the search provider. mp3skull's HTML is basically a shit sandwich, so this is equally messy, but it does the trick.
#!/usr/bin/env python
# License: No.
# I am explicitly placing this in the public domain.
import sys
import re
import requests
import urllib
import urllib2
MAX_RESULTS = 10
MIN_FILE_SIZE = 3.5 # MB
MIN_BITRATE = 160 # Kbps
if len(sys.argv) == 1:
print "Usage: {} <search string>".format(sys.argv[0])
sys.exit(0)
q = '_'.join(sys.argv[1:])
payload = {'ord': 'br'}
url = 'http://mp3skull.com/mp3/{}.html'.format(q)
r = requests.post(url, data=payload)
if not r:
print "Couldn't connect to remote service"
sys.exit(1)
started = False
results = []
for line in r.text.split("\n"):
if "info mp3 here" in line:
started = True
elif started is True:
match = re.search("\s([0-9]+)\s*kbps", line)
if match:
bitrate = int(match.group(1))
match = re.search("<b>(.+)</b>", line)
if match:
title = match.group(1).replace('mp3', '').strip()
match = re.search('href="(.+?)"', line)
if match:
mp3_url = match.group(1)
started = False
if bitrate < MIN_BITRATE:
print u"Ignoring: {} (low bitrate)".format(mp3_url)
continue
try:
u = urllib2.urlopen(mp3_url)
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
except:
print u"Ignoring: {} (file not available)".format(mp3_url)
else:
file_size /= 100000
file_size = float(file_size) / 10
if file_size > MIN_FILE_SIZE:
results.append({'bitrate': bitrate,
'title': title,
'size': file_size,
'url': mp3_url})
if len(results) == MAX_RESULTS:
break
else:
print u"Ignoring: {} (file too small)".format(mp3_url)
if len(results) == 0:
print "No results found"
else:
for result in results:
print u"{index: >3} : {bitrate: <3}Kbps : {size: >3} MB : {title}".format(index=results.index(result) + 1, **result)
while True:
if len(results) == 1:
choice = raw_input('Choose [enter] to download this result, [q] to quit: ')
else:
choice = raw_input('Choose [1-{}], [enter] to choose top result, [q] to quit: '.format(len(results)))
if choice == 'q':
sys.exit(0)
if choice == '':
choice = '1'
try:
number = int(choice)
except:
pass
else:
if 0 < number <= len(results):
result = results[number - 1]
filename = u"{}.mp3".format(result['title'])
choice = raw_input(u'Save as file [{}]: '.format(filename))
if choice:
filename = choice
urllib.urlretrieve(result['url'], filename)
sys.exit(0)
print "Invalid choice."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment