Last active
August 29, 2016 08:36
-
-
Save oneamitj/7692742a2f9445578ec51081b60be2a8 to your computer and use it in GitHub Desktop.
Download songs from a ftp I found online...
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
#!/usr/bin/python3 | |
import urllib.request, urllib.error, urllib.parse | |
from pdb import set_trace | |
from bs4 import BeautifulSoup | |
import os,sys | |
file_name = "" | |
def reporthook(blocknum, blocksize, totalsize): | |
readsofar = blocknum * blocksize | |
if totalsize > 0: | |
percent = readsofar * 1e2 / totalsize | |
s = "\n\r%s %5.1f%% %*d / %d Bytes" % ( | |
file_name, percent, len(str(totalsize)), readsofar, totalsize) | |
sys.stderr.write(s) | |
if readsofar >= totalsize: # near the end | |
sys.stderr.write("\n") | |
else: # total size is unknown | |
sys.stderr.write("read %d\n" % (readsofar,)) | |
def dwld_songs(songs): | |
for key, value in songs.items(): | |
print(key, value) | |
file_name = key | |
urllib.request.urlretrieve(value, key, reporthook) | |
def get_songs(): | |
art_name = input('Enter Artist Name: ') #"Pink Floyd" | |
home = "http://hcmaslov.d-real.sci-nnov.ru/public/mp3/" | |
url = home + art_name.replace(" ","%20") | |
user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3' | |
headers = { 'User-Agent' : user_agent } | |
try: | |
req = urllib.request.Request(url, None, headers) | |
response = urllib.request.urlopen(req) | |
except: | |
print("Artist Not Found") | |
return | |
soup_page = BeautifulSoup(response.read(), "html.parser") | |
table = soup_page.find('table').find_all('tr') | |
songs = {} | |
for row in table[3:]: | |
try: | |
row_data = row.find_all("td") | |
songs[row_data[1].get_text()] = url + '/' + row_data[1].a['href'] | |
except: | |
break | |
dwld_songs(songs) | |
get_songs() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All the mp3 are from this site.
Artist name must be match as written in the site. Eg Pink Floyd, Maroon 5, ...
Not tested in py2.
Requirements: bs4 (BeautifulSoup) -
pip install bs4