{"id":1652,"date":"2011-08-11T20:49:00","date_gmt":"2011-08-11T12:49:00","guid":{"rendered":""},"modified":"2013-11-23T15:02:13","modified_gmt":"2013-11-23T07:02:13","slug":"%e8%87%aa%e5%8a%a8%e4%b8%8b%e8%bd%bdmp3%e7%9a%84python%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1652.html","title":{"rendered":"\u81ea\u52a8\u4e0b\u8f7dMP3\u7684python\u811a\u672c"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\n#-*- coding: utf-8 -*-\r\nimport sys\r\nimport os\r\nimport re\r\nimport urllib2\r\ncwd = os.getcwd()\r\nlc = sys.getfilesystemencoding()\r\ndowndir = cwd+os.sep+\u2019Music_\u2019\r\nprint \u2018\\n\u4e0b\u8f7d\u76ee\u5f55\u4e3a\uff1a\u2019,downdir,\u2019\\n\u2019\r\nif not os.path.isdir(downdir):\r\n    print \u2018\u4e0b\u8f7d\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u6b63\u5728\u521b\u5efa\u76ee\u5f55\uff1a\u2019,downdir\r\n    os.mkdir(downdir)\r\nif os.path.isfile(sys.argv&#x5B;1]):\r\n    list_file = sys.argv&#x5B;1]\r\nelse:\r\n    list_file = cwd + os.sep + sys.argv&#x5B;1]\r\ntry:\r\n    f = file(list_file,\u2019r')\r\nexcept IOError:\r\n    print \u2018\u6b4c\u5178\u5217\u8868\u6253\u5f00\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728!\u2019\r\n    sys.exit()\r\nfor eachLine in f:\r\n    song = eachLine.strip()\r\n    if not song:\r\n        continue\r\n    url=\u201dhttp:\/\/box.zhangmen.baidu.com\/x?op=12&amp;count=1&amp;title=%s$$\u201d % urllib2.quote(song.decode(lc).encode(\u2018gbk\u2019))\r\n    xmlf = urllib2.urlopen(url)\r\n    outfile = file(\u2018temp.xml\u2019,'w\u2019)\r\n    for line in xmlf:\r\n        outfile.write(line)\r\n    outfile.close()\r\n    in_file = file(\u2018temp.xml\u2019)\r\n    txt = in_file.read()\r\n    in_file.close()\r\n    os.remove(\u2018temp.xml\u2019)\r\n    rex1 = u\u2019()(http:\/\/.+?\/.+?\\..+?)()\u2019\r\n    rex2 = u\u2019()(.+?\\..+?)()\u2019\r\n    l1 = re.findall(rex1,txt)\r\n    l2 = re.findall(rex2,txt)\r\n    url_list = &#x5B;]\r\n    for i in range(len(l1)):\r\n        temp_list = re.split(\u2018\/\u2019,l1&#x5B;i]&#x5B;1])\r\n        temp_list.pop()\r\n        temp_list.append(l2&#x5B;i]&#x5B;1])\r\n        down_url = \u2018\/\u2019.join(temp_list)\r\n        url_list.append(down_url)\r\n    for i in range(len(url_list)):\r\n        extname = url_list&#x5B;i].split(\u2018.\u2019)&#x5B;-1]              #\u8df3\u8fc7\u975eMP3\u7684\u7c7b\u578b\r\n        if extname.upper() == \u2018MP3\u2032:\r\n            mp3file = downdir+os.sep+song+\u2019.mp3\u2032\r\n            if os.path.isfile(mp3file):\r\n                print \u2018\\n\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u8f6c\u5230\u4e0b\u4e00\u9996\\n\u2019\r\n                break\r\n            print \u2018\\n\u6b63\u5728\u4e0b\u8f7d\u6b4c\u66f2\uff1a\u2019,song,\u2019\u2026\\n\u2019\r\n            cmd = \u2018wget %s -c -t 3 -O %s\u2019 % (url_list&#x5B;i],downdir+os.sep+song+\u2019.mp3\u2032)\r\n            os.system(cmd)\r\n            if os.path.getsize(mp3file) &lt; 500000L:  #\u5c0f\u4e8e500K\u7684\u6587\u4ef6\u5c06\u88ab\u5220\u9664\uff0c\u5e76\u91cd\u65b0\u4e0b\u8f7d\r\n                print \u2018\\n\u6587\u4ef6\u8fc7\u5c0f\uff0c\u53ef\u80fd\u662f\u76ee\u6807\u7f51\u7ad9\u6709\u9650\u5236\uff0c\u5c06\u5c1d\u8bd5\u4e0b\u4e00\u4e2a\u94fe\u63a5\\n\u2019\r\n                os.remove(mp3file)\r\n            else:\r\n                print song,\u2019\u4e0b\u8f7d\u5b8c\u6bd5\uff01\u2019\r\n                break\r\nprint \u2018\u4e0b\u8f7d\u5b8c\u6bd5\uff01\u2019\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#!\/usr\/bin\/env python #-*- coding: utf-8 -*- import sys [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1652","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=1652"}],"version-history":[{"count":2,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1652\/revisions"}],"predecessor-version":[{"id":5154,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1652\/revisions\/5154"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}