Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import mechanize
- import re
- import urllib2
- import sys
- import os
- def download_file(url, file_name):
- u = urllib2.urlopen(url)
- f = open(file_name, 'wb')
- meta = u.info()
- file_size = int(meta.getheaders("Content-Length")[0])
- print "Downloading: %s Bytes: %s" % (file_name, file_size)
- file_size_dl = 0
- block_sz = 8192
- while True:
- buffer = u.read(block_sz)
- if not buffer:
- break
- file_size_dl += len(buffer)
- f.write(buffer)
- status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
- status = status + chr(8)*(len(status)+1)
- print status,
- f.close()
- def download_song(url):
- print "Downloading song from %s" % url
- browser = mechanize.Browser()
- browser.set_handle_robots(False)
- action_regexp = re.compile('/file/.*')
- browser.open(url)
- formcount=0
- for form in browser.forms():
- if action_regexp.match(form.attrs['action']):
- formcount=formcount+1
- break
- file_name = url + '.mp3'
- for link in browser.links(url_regex='\/file\/'):
- file_name = link.text
- browser.select_form(nr=formcount)
- browser.submit()
- for link in browser.links(url_regex='tempfile\.ru'):
- download_file(link.url, file_name)
- def download_album(url):
- browser = mechanize.Browser()
- browser.set_handle_robots(False)
- browser.open(url)
- title_regexp = re.compile('.*Скачать mp3.*', re.IGNORECASE)
- for link in browser.links(url_regex='/download/.*'):
- for title in [attr for attr in link.attrs if len(attr) == 2 and attr[0] == 'title']:
- title_utf8 = title[1].decode('cp1251').encode('utf8')
- if title_regexp.match(title_utf8):
- url_arr = link.base_url.split('/')
- if link.url.startswith('http'):
- download_song(link.url)
- else:
- if len(url_arr) > 3 and url_arr[0] == 'http:':
- page_url = 'http://' + url_arr[2]
- else:
- page_url = url_arr[0]
- download_song(page_url + link.url)
- def main():
- if len(sys.argv) < 2:
- print "usage %s album_url" % os.path.basename(sys.argv[0])
- sys.exit(1)
- download_album(sys.argv[1])
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment