Guest User

Untitled

a guest
Dec 14th, 2013
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import mechanize
  5. import re
  6. import urllib2
  7. import sys
  8. import os
  9.  
  10. def download_file(url, file_name):
  11.     u = urllib2.urlopen(url)
  12.     f = open(file_name, 'wb')
  13.     meta = u.info()
  14.     file_size = int(meta.getheaders("Content-Length")[0])
  15.     print "Downloading: %s Bytes: %s" % (file_name, file_size)
  16.  
  17.     file_size_dl = 0
  18.     block_sz = 8192
  19.     while True:
  20.         buffer = u.read(block_sz)
  21.         if not buffer:
  22.             break
  23.  
  24.         file_size_dl += len(buffer)
  25.         f.write(buffer)
  26.         status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
  27.         status = status + chr(8)*(len(status)+1)
  28.         print status,
  29.  
  30.     f.close()
  31.  
  32. def download_song(url):
  33.     print "Downloading song from %s" % url
  34.  
  35.     browser = mechanize.Browser()
  36.     browser.set_handle_robots(False)
  37.  
  38.     action_regexp = re.compile('/file/.*')
  39.  
  40.     browser.open(url)
  41.  
  42.     formcount=0
  43.     for form in browser.forms():
  44.         if action_regexp.match(form.attrs['action']):
  45.             formcount=formcount+1
  46.             break
  47.  
  48.     file_name = url + '.mp3'
  49.     for link in browser.links(url_regex='\/file\/'):
  50.         file_name = link.text
  51.  
  52.     browser.select_form(nr=formcount)
  53.     browser.submit()
  54.  
  55.     for link in browser.links(url_regex='tempfile\.ru'):
  56.         download_file(link.url, file_name)
  57.  
  58. def download_album(url):
  59.     browser = mechanize.Browser()
  60.     browser.set_handle_robots(False)
  61.  
  62.     browser.open(url)
  63.  
  64.     title_regexp = re.compile('.*Скачать mp3.*', re.IGNORECASE)
  65.  
  66.     for link in browser.links(url_regex='/download/.*'):
  67.         for title in [attr for attr in link.attrs if len(attr) == 2 and attr[0] == 'title']:
  68.             title_utf8 = title[1].decode('cp1251').encode('utf8')
  69.  
  70.             if title_regexp.match(title_utf8):
  71.                 url_arr = link.base_url.split('/')
  72.                 if link.url.startswith('http'):
  73.                     download_song(link.url)
  74.                 else:
  75.                     if len(url_arr) > 3 and url_arr[0] == 'http:':
  76.                         page_url = 'http://' + url_arr[2]
  77.                     else:                    
  78.                         page_url = url_arr[0]
  79.                     download_song(page_url + link.url)
  80.  
  81. def main():
  82.     if len(sys.argv) < 2:
  83.         print "usage %s album_url" % os.path.basename(sys.argv[0])
  84.         sys.exit(1)
  85.  
  86.     download_album(sys.argv[1])
  87.  
  88. if __name__ == "__main__":
  89.     main()
Advertisement
Add Comment
Please, Sign In to add comment