21 Jan 2010

Học python

Sửa lại chút cái shell script hồi xưa Dreammedia , viết lại bằng python.
Bạn nào dùng thì nhớ nếu tên ban nhạc hoặc album có khoảng trắng, thì thêm vào thành "band name" hoặc "album name" :).

#! /usr/bin/env python
# Script to download Music from Dreammedia.ru
# Try to find your favorite Band and Album, then use this script
# hungnv coded 
import os
import urllib2
import sys
def usage():
        print "Usage:",sys.argv[0], "\"bandname\" \"albumname\""    
        
def st_replace(string):
        new = string.replace(' ','%20')
        return new
            
def download(url,pl,band,album):
        band1 = st_replace(band)
        album1 = st_replace(album)
        temp = (url + pl[:8] + '/' + band1 + '/' + album1 + '/' + pl)
        try:
                response = urllib2.urlopen(temp)
        except:
                print 'Connection error'
        response = urllib2.urlopen(temp)
        content = response.readlines()
        for line in content:
                a = line.find('music/')
                b = line.find('.mp3')
                if a <> -1 and b <> -1:
                        path = line[a:b+4]
                        semi = (url + '%s') %path
                        final = st_replace(semi)
                        os.system('wget '+final)
                else:
                        continue
                        
if __name__ == '__main__':
        band = sys.argv[1]
        album = sys.argv[2]
        url = "http://dreammedia.ru/"
        pl = 'playlist.xml'
        if len(sys.argv) <> 3:
                usage()
                exit()
        else:
                download(url,pl,band,album)
exit()


No comments:

Disqus