Skip to content

Instantly share code, notes, and snippets.

@coldnight
Last active January 3, 2016 07:59
Show Gist options
  • Save coldnight/8433068 to your computer and use it in GitHub Desktop.
Save coldnight/8433068 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Author : cold
# E-mail : wh_linux@126.com
# Date : 14/01/15 15:55:30
# Desc : 从豆瓣FM指定频道下载音乐
#
""" 从豆瓣FM上下载音乐
依赖命令:
curl
依赖第三方库:
$ easy_install tornadohttpclient mutagen
Update:
2014-01-16 使用mutagen为音乐添加ID3信息(豆瓣下载的音乐这些信息貌似被去掉了)
"""
from __future__ import print_function
import os
import json
import random
from tornadohttpclient import TornadoHTTPClient
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3
client = TornadoHTTPClient()
def edit_mp3_tag(path, info):
valid_key = ["title", "artist", "album"]
dinfo = {key : value for key, value in info.items() if key in valid_key}
id3info = MP3(path, ID3 = EasyID3)
for key, val in dinfo.items():
id3info[key] = val
id3info.save()
def download(args):
cmd = u'curl -o "{0}" "{1}"'.format(*args[:2])
try:
os.system(cmd.encode("utf-8"))
info = args[2]
edit_mp3_tag(args[0], info)
except UnicodeError:
print(cmd)
def handle_response(resp):
data = json.loads(resp.body)
file_map = []
for each in data.get("song"):
file_name = u"{0}.{1}".format(each.get("title"), each.get("url").split(".")[-1])
file_name = file_name.replace("/", "-")
print(file_name)
if os.path.exists(file_name):
continue
file_map.append((file_name, each.get("url").replace("\/", "/"), each))
map(download, file_map)
fetch(data["song"][0]["sid"], "s")
def fetch(sid = "", type="n"):
url = "http://douban.fm/j/mine/playlist"
gd = 27 # 古典
hy = 1 # 华语
params = {"type":type, "sid":sid, "pt":0.0, "channel":hy,
"pb":64, "from":"mainsite",
"r":hex(random.randrange(0x0000000000, 0xffffffffff))[2:]}
client.get(url, params, callback = handle_response)
if __name__ == "__main__":
fetch()
client.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment