LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: aiyi

写了一个从百度下载Mp3的python脚本

[复制链接]
发表于 2006-11-2 18:07:34 | 显示全部楼层
应该不是文件分块,而是多连接,每个连接有不同的偏移量,实现多线程下载。我也只知道这里了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-2 18:58:30 | 显示全部楼层
Post by 三翻领
应该不是文件分块,而是多连接,每个连接有不同的偏移量,实现多线程下载。我也只知道这里了。


昨天晚上写了一个http的多线程下载,下了几个iso测试了下没有问题,有时间的话再写一个ftp的就把axel换掉,呵呵。
回复 支持 反对

使用道具 举报

发表于 2006-11-2 22:19:18 | 显示全部楼层
强人啊!!!
回复 支持 反对

使用道具 举报

发表于 2006-11-3 09:41:13 | 显示全部楼层
下载的代码可否交流一下? :D
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-3 12:08:34 | 显示全部楼层
Post by 三翻领
下载的代码可否交流一下? :D

当然,贴到偶的blog上了:
http://blog.chinaunix.net/u/5017/showart.php?id=194744
回复 支持 反对

使用道具 举报

发表于 2006-11-3 17:47:29 | 显示全部楼层
Post by aiyi
当然,贴到偶的blog上了:
http://blog.chinaunix.net/u/5017/showart.php?id=194744


你的功力比我深厚多了。完全的面向对象,看起来真类。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-3 18:13:17 | 显示全部楼层
Post by 三翻领

你的功力比我深厚多了。完全的面向对象,看起来真类。

写的很挫,见笑了
回复 支持 反对

使用道具 举报

发表于 2006-11-7 23:06:40 | 显示全部楼层
非常感谢楼主的程序,稍微修改了一下,能在windows上运行了。
所做的修改主要有3处:
1.编码的处理,将汉字都处理成unicode,包括artist, title,这样,不管在什么样的Locale下都能正确显示。
2.调用wget代替axel,因为我没有找到win32 native的axel.只是wget是单线程的,速度慢了点。
3.在下载top100的时候,如果出现某首歌无法下载的时候,不会退出,而是继续下载下一首。
patch如下:

  1. --- DownloadSong        Mon Oct 30 21:54:34 2006
  2. +++ DownloadSong-zsx.py        Tue Nov 07 22:54:54 2006
  3. @@ -59,6 +59,8 @@
  4.          else:
  5.              title = v
  6.              artist = ''
  7. +        artist = artist.decode('utf-8')
  8. +        title = title.decode('utf-8')
  9.          info = {'artist':artist, 'title':title}
  10.          songs[k] = info
  11.          
  12. @@ -80,16 +82,17 @@
  13.      """Search results in baidu can't be downloaded directly,
  14.         this function get top 30(or less) urls from the results.
  15.         arguments:
  16. -           artist: artist
  17. -           title: title/song name
  18. +           artist: artist, unicode
  19. +           title: title/song name, unicode
  20.         return values:
  21. -           urls: urls got from search results.
  22. +           urls: urls got from search results. in GBK encoding
  23.      """
  24.      baseurl = 'http://mp3.baidu.com/m?f=ms&tn=baidump3&ct=134217728&lf=&rn=&lm=0&word='
  25.      #multi artist are seperated by '_', replace it with space here
  26.      artist = artist.replace('_', ' ')
  27.      keyword = '%s %s' %(artist, title)
  28. -    keyword = keyword.decode('utf8', 'ignore').encode('gbk')
  29. +    #keyword = keyword.decode('utf8', 'ignore').encode('gbk')
  30. +    keyword = keyword.encode('gbk')
  31.      url = baseurl + urllib.quote(keyword, string.punctuation)
  32.      
  33.      html = urllib2.urlopen(url).read()
  34. @@ -153,14 +156,18 @@
  35.      return (length, etime)
  36.      
  37. def DownloadSong(artist, title):
  38. -    filename = '%s-%s.mp3' %(artist, title)
  39. +    ''' artist: unicode
  40. +            title: unicode
  41. +    '''
  42. +    filename = u'%s-%s.mp3' % (artist, title)
  43.      if title == '':
  44.          return
  45.      if artist == '':
  46. -        filename = '%s.mp3' %title
  47. -    print "准备下载《%s - %s》..." %(artist, title)
  48. +        filename = u'%s.mp3' % title
  49. +    filename = filename.encode(sys.getfilesystemencoding())
  50. +    print u"准备下载《%s - %s》..." %(artist, title)
  51.      if os.path.exists(filename) and not os.path.exists(filename + '.st'):
  52. -        print "已经成功下载《%s - %s》"%(artist, title)
  53. +        print u"已经成功下载《%s - %s》"%(artist, title)
  54.          print
  55.          return
  56.      fakeurls = _getFakeURLs(artist, title)
  57. @@ -175,7 +182,7 @@
  58.                  except:
  59.                      pass
  60.              if os.path.exists(filename):
  61. -                print "已经成功下载《%s - %s》"%(artist, title)
  62. +                print u"已经成功下载《%s - %s》"%(artist, title)
  63.                  print
  64.                  return
  65.              try:
  66. @@ -184,11 +191,12 @@
  67.                  continue
  68.              if length<2*1024*1024 or etime>5000:
  69.                  continue
  70. -            os.system('axel "%s" -o "%s" -a' %(url, filename))
  71. +            os.system('e:/tools/wget/wget.exe "%s" -O "%s"' %(url, filename))
  72. +            #os.system('axel "%s" -o "%s" -a' %(url, filename))
  73.              continue
  74.      #所有的url都已尝试完毕,仍没有下载到
  75.      if not os.path.exists(filename):
  76. -        print "Sorry, 目前并没有为(%s - %s)找到合适的下载资源,\n您可以手动下载或稍候再试。" %(artist, title)
  77. +        print u"Sorry, 目前并没有为(%s - %s)找到合适的下载资源,\n您可以手动下载或稍候再试。" %(artist, title)
  78.          print


  79. @@ -197,8 +205,13 @@
  80.      for rank, info in songs.items():
  81.          artist = info['artist']
  82.          title = info['title']
  83. -        print "正在下载第%d首(共%d首) 歌手:%s 曲名:%s" %(rank, len(songs), artist, title)
  84. -        DownloadSong(artist, title)
  85. +        print u"正在下载第%d首(共%d首) 歌手:%s 曲名:%s" %(rank, len(songs), artist, title)
  86. +        try:
  87. +            DownloadSong(artist, title)
  88. +        except KeyboardInterrupt:
  89. +            break
  90. +        except :
  91. +            print u'出现了未知错误导致下载第%d首(共%d首) 歌手:%s 曲名:%s 失败了。' %(rank, len(songs), artist, title)

  92. def Help():
  93.      helpstr = """Usage: %s [OPTION]
复制代码

好久没弄python了,改得非常仓促,都没仔细看代码,发现可以使用了就停手了。;)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-8 00:31:17 | 显示全部楼层
Post by pupilzeng

好久没弄python了,改得非常仓促,都没仔细看代码,发现可以使用了就停手了。;)


感谢pupilzeng对这个小程序做了修改,稍后我会根据你的补丁进行改进、内置多线程下载后再行发布。我对python还不是很熟,比如使用unicode处理中文会更方便这点我原来就没有想到,以后有什么问题再向你请教;)
回复 支持 反对

使用道具 举报

发表于 2006-11-8 15:22:55 | 显示全部楼层
Post by aiyi
感谢pupilzeng对这个小程序做了修改,稍后我会根据你的补丁进行改进、内置多线程下载后再行发布。我对python还不是很熟,比如使用unicode处理中文会更方便这点我原来就没有想到,以后有什么问题再向你请教;)

嗯,等待你内建多线程下载的版本。
另外有个建议,能不能对下载镜像根据速度进行排序?然后选取最快的进行下载?现在选取的有时太慢了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表