python 下载虾米音乐

#!/usr/bin/env python2 
# coding:utf-8

import urllib
import re
import sys
import urllib2

# xml => http://www.xiami.com/widget/xml-single/sid/1769215777

# string = "5h3%..i%2323F27%_3ueb5cc554e525-ntA2fx.2%9F211_5l%ty5d227E4-E8E%ut%FiicF2532759E.3h%1%7b7261%4%5lp2mlao9F25%6738mF_3654e7884585El%F5emm52%529774pakDeE98%5a%E%E-"


def parse_location(location_str):
    row = int(location_str[0])
    encrypt_location = location_str[1:]
    enc_loc_len = len(encrypt_location)
    column = enc_loc_len / row
    remainder = enc_loc_len % row
    loc_matrix = []
    index = 0
    for i in range(0, remainder):
        loc_matrix.append(encrypt_location[index: index + column + 1])
        index += column + 1
    for i in range(0, row - remainder):
        loc_matrix.append(encrypt_location[index: index + column])
        index += column
    location = ''
    for j in range(0, column + 1):
        for i in range(0, row):
            if len(loc_matrix[i]) > j:
                location += loc_matrix[i][j]
    location = urllib.unquote(location).replace('^','0')
    return location
    
#print parse_location(string)


def main(url):
    #print url
    xml = urllib2.urlopen(url).read()
    en_url = re.findall("(?<=<location><!\[CDATA\[).*?(?=]]></location>)",xml)
    print parse_location(en_url[0])
    

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print u"无效的参数,它应该像这个样子:http://www.xiami.com/song/3284034?spm=a1z1s.7154410.1996860142.3.LxxgQW"
    else:
        m = re.search("(?<=http://www.xiami.com/song/)\d*?(?=\?spm=)",sys.argv[1])
        if m:
            main("http://www.xiami.com/widget/xml-single/sid/"+m.group())
        else:
            print u"无效的参数,它应该像这个样子:http://www.xiami.com/song/3284034?spm=a1z1s.7154410.1996860142.3.LxxgQW"

 

posted @ 2014-05-16 20:51  r3call  阅读(399)  评论(0编辑  收藏  举报