Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Let's follow GPL:
- # I'm using some code by PabloG from Stackoverflow
- import sys
- import os
- import urllib2
- from os import listdir
- from os.path import isfile, join
- def get_html_name():
- onlyfiles = [ f for f in listdir(os.getcwd()) if isfile(join(os.getcwd(),f)) ]
- for x in xrange(0, len(onlyfiles)):
- if(onlyfiles[x].find(".html") != -1):
- return onlyfiles[x]
- return None
- def download_file(url, file_name):
- u = urllib2.urlopen(url)
- local_file_size = 0
- if(os.path.isfile(file_name) == True):
- local_file_size = os.stat(file_name).st_size
- meta = u.info()
- file_size = long(meta.getheaders("Content-Length")[0])
- if ( file_size == local_file_size):
- return None
- f = open(file_name, 'wb')
- 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 Kb [%3.2f%%] %s" % (file_size_dl/1024, file_size_dl * 100. / file_size, file_name)
- status = status + chr(8)*(len(status)+1)
- print status,
- f.close()
- print("")
- return None
- def get_band_name(inc_str):
- pattern1='return false">'
- n=""
- for x in xrange((inc_str.find(pattern1)+len(pattern1)),len(inc_str)):
- if(inc_str[x] != "<"):
- n+=inc_str[x]
- else:
- break
- return n
- def get_song_name(inc_str):
- i=len(inc_str)-1
- found = 0
- n=""
- while i > 0 and found == 0:
- if inc_str[i] == ">":
- if inc_str[i-7]+inc_str[i-6]+inc_str[i-5]+inc_str[i-4]+inc_str[i-3]+inc_str[i-2]+inc_str[i-1] == " </span":
- j=i-8
- while inc_str[j-1] + inc_str[j] != "\">":
- n+=inc_str[j]
- j=j-1
- found = 1
- i=i-1
- n=n[::-1]
- n1=n.split("</a>")
- return n1[0]
- pattern1='return false">'
- for x in xrange((inc_str.find(pattern1)+len(pattern1)),len(inc_str)):
- if(inc_str[x] != "<"):
- n+=i_str[x]
- else:
- break
- return n
- def get_url(inc_str):
- pattern1="https://"
- n=""
- for x in xrange(inc_str.find(pattern1),len(inc_str)):
- if(inc_str[x] != ","):
- n+=inc_str[x]
- else:
- break
- return n
- def get_flag(flag):
- for x in xrange(0,len(sys.argv)):
- if sys.argv[x] == flag:
- return True
- return False
- def get_option(option):
- for x in xrange(0,len(sys.argv)):
- if sys.argv[x] == option:
- if x+1 < len(sys.argv):
- return sys.argv[x+1]
- return None
- def filter_name(name):
- result=""
- special="<>:\"/\|?*"
- for x in xrange(0,len(name)):
- if special.find(name[x]) == -1:
- result += name[x]
- else:
- result += "_"
- return result
- def list_file(url, name):
- print name
- return None
- def show_help():
- print "\ndum.py is MP3 downloader for vk.com\n"
- print "USAGE:\n\tdum.py [options]"
- print "\n\tIf no options given, all files from instance\n\tof vk.com/audio.html found in current directory\n\twill be downloaded.\n\tThis means, you have to save an HTML page vk.com/audio to directory.\n"
- print "\t-h Show this help\n"
- print "\t-L List contents.\n\t\tIf flag is set, files will be listed\n\t\tonly, otherwise they will be downloaded.\n"
- print "\t-m [MATCH] List/download every file, that has MATCH in it's name\n"
- return None
- # EXECUTION
- filename = ""
- if len(sys.argv) == 2:
- filename=sys.argv[1]
- else:
- filename=get_option("-f")
- if not (filename != None and os.path.isfile(filename) and filename.find(".htm") != -1):
- filename = get_html_name()
- l_url=""
- if filename != None:
- match = get_option("-m")
- only_list = get_flag("-L")
- only_help = get_flag("-h")
- if( only_help == True ):
- show_help()
- sys.exit(0)
- if( only_list == True ):
- file_action = list_file
- else:
- file_action = download_file
- # print filename
- vk_file = open(filename, "r")
- line = vk_file.readline()
- while (line != None) and (line != ""):
- line=line.decode('cp1251').encode('utf8')
- if(line.find("<input type=\"hidden\" id=\"audio_info") != -1):
- l_url=get_url(line)
- trash=vk_file.readline()
- trash=vk_file.readline()
- name_line=vk_file.readline()
- name_line=name_line.decode('cp1251').encode('utf8')
- band=filter_name(get_band_name(name_line))
- song=filter_name(get_song_name(name_line))
- if (band != None and song != None and l_url != None):
- if(match != None):
- if (band.find(match) != -1 or song.find(match) != -1):
- # print band + "\t" + song + "\t" + l_url
- file_action(l_url, band + " - " + song + ".mp3")
- else:
- file_action(l_url, band + " - " + song + ".mp3")
- #print band + "\t" + song + "\t" + l_url
- line=vk_file.readline()
- else:
- print "ERROR: No html files found"
- show_help()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement