#!/usr/bin/env python ### # # RBCD - RadioBlogClub Downloader - Download songs from RadioBlogClub. # Copyright (C) 2006 foch # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA # ### __version__ = "0.5" __date__ = "12/04/2007" import urllib, re, sys def changeRBSExtension(file): """ Change the RBS extension to MP3 """ name = file.split("/")[-1].split(".") #remove some extensions name = [part for part in name if not part.lower() in ("rbs", "mp3")] #the file is a mp3, add .mp3 at the end of the name name.append("mp3") return ".".join(name) def searchURL(i, params): """ Return the URL to search the MP3 """ url = "http://www.radioblogclub.com/search/" + str(i) + "/" + params try: return urllib.urlopen(url).read() except IOError: print "Error : could not connect" sys.exit(1) def main(argv): """ Main function, one to rule them all """ #get the webpage data data = searchURL(0, "_".join(argv)) lines = data.split('\n') #regular expression to find the number of songs reg1 = re.compile('^.*(.*) for .*$') n_songs = 0 #get the number of songs for line in lines: if reg1.match(line): n_songs = int(reg1.search(line).groups()[0]) break #if no song is found, exit if n_songs == 0: print "No song found" sys.exit(0) #get the number of pages to browse n_pages = n_songs / 50 + 1 #download the pages pages = [] pages.append(lines) numbers = [i * 50 for i in range(1, n_pages)] for i in numbers: pages.append(searchURL(i, "_".join(argv)).split('\n')) #regular expression to find the song URLs reg2 = re.compile('^.*(.*).*$') songs = [] i = 0 #parse the HTML data to find the songs and save in a list for page in pages: for line in page: if reg2.match(line): result = reg2.search(line).groups() url = result[0] name = result[1] songs.append((url, name)) i += 1 print "%4d : %s" % (i, name) while 1: #get the user choice while 1: user = raw_input("Choice (q to exit): ") if user == 'q': sys.exit(0) try: choice = int(user) except ValueError: continue if choice > 0 and choice <= len(songs): break name = songs[choice - 1][1] name_search = name.replace('(', '\(').replace(')', '\)')\ .replace('[', '\[').replace(']', '\]')\ .replace('\'', '\\\\\'') url = "http://www.radioblogclub.com" + songs[choice - 1][0]\ .replace(' ', '%20') #get the HTML file with the RBS adress playlist = urllib.urlopen(url).read().split('\n') #get the RBS file URL reg3 = re.compile('^.*javascript:openRadio\(\'(.*)\?autoplay.*$') #find the URL url = "" for line in playlist: if reg3.match(line): url = reg3.search(line).groups()[0] + 'sounds/' + name_search + '.rbs' url = url.replace('\\', '').replace(' ', '%20') print "File URL :\n%s" % (url, ) break #the RBS file was not found if url == "": print "Song not found. Maybe the song was a SWF. Try another one." sys.exit(1) #change the extension to .mp3 name = changeRBSExtension(name) #download the file print "downloading %s..." % (name, ) urllib.urlretrieve(url, name) #program entry point if __name__ == "__main__": if len(sys.argv) == 1: print "RadioBlogClub Downloader v. " + __version__ #enter interactive mode if no keywords are given user = raw_input("Enter keywords (q to exit): ") #problem with the ' user = user.replace('\'', ' ') if user == 'q': sys.exit(0) else: main(user.split(" ")) else: main(sys.argv[1:])