Advertisement
stream13

vk dympy 2

Jun 17th, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Let's follow GPL:
  4. # I'm using some code by PabloG from Stackoverflow
  5.  
  6. import sys
  7. import os
  8. import urllib2
  9. from os import listdir
  10. from os.path import isfile, join
  11.        
  12. def get_html_name():
  13.     onlyfiles = [ f for f in listdir(os.getcwd()) if isfile(join(os.getcwd(),f)) ]
  14.     for x in xrange(0, len(onlyfiles)):
  15.         if(onlyfiles[x].find(".html") != -1):
  16.             return onlyfiles[x]
  17.     return None
  18.  
  19. def download_file(url, file_name):
  20.     u = urllib2.urlopen(url)
  21.     local_file_size = 0
  22.     if(os.path.isfile(file_name) == True):
  23.         local_file_size = os.stat(file_name).st_size
  24.     meta = u.info()
  25.     file_size = long(meta.getheaders("Content-Length")[0])
  26.     if ( file_size == local_file_size):
  27.         return None
  28.     f = open(file_name, 'wb')
  29.     file_size_dl = 0
  30.     block_sz = 8192
  31.     while True:
  32.         buffer = u.read(block_sz)
  33.         if not buffer:
  34.             break    
  35.         file_size_dl += len(buffer)
  36.         f.write(buffer)
  37.         status = r"%10d Kb  [%3.2f%%] %s" % (file_size_dl/1024, file_size_dl * 100. / file_size, file_name)
  38.         status = status + chr(8)*(len(status)+1)
  39.         print status,    
  40.     f.close()
  41.     print("")
  42.     return None
  43.  
  44. def get_band_name(inc_str):
  45.     pattern1='return false">'
  46.     n=""
  47.     for x in xrange((inc_str.find(pattern1)+len(pattern1)),len(inc_str)):
  48.             if(inc_str[x] != "<"):
  49.                     n+=inc_str[x]
  50.             else:
  51.                     break
  52.     return n
  53.  
  54. def get_song_name(inc_str):
  55.     i=len(inc_str)-1
  56.     found = 0
  57.     n=""
  58.     while i > 0 and found == 0:
  59.         if inc_str[i] == ">":
  60.             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":
  61.                 j=i-8
  62.                 while inc_str[j-1] + inc_str[j] != "\">":
  63.                     n+=inc_str[j]
  64.                     j=j-1
  65.                 found = 1
  66.         i=i-1
  67.     n=n[::-1]
  68.     n1=n.split("</a>")
  69.     return n1[0]
  70.  
  71.     pattern1='return false">'
  72.     for x in xrange((inc_str.find(pattern1)+len(pattern1)),len(inc_str)):
  73.             if(inc_str[x] != "<"):
  74.                     n+=i_str[x]
  75.             else:
  76.                     break
  77.     return n
  78.  
  79. def get_url(inc_str):
  80.     pattern1="https://"
  81.     n=""
  82.     for x in xrange(inc_str.find(pattern1),len(inc_str)):
  83.             if(inc_str[x] != ","):
  84.                     n+=inc_str[x]
  85.             else:
  86.                     break
  87.     return n
  88.  
  89. def get_flag(flag):
  90.     for x in xrange(0,len(sys.argv)):
  91.         if sys.argv[x] == flag:
  92.             return True
  93.     return False
  94.  
  95.    
  96. def get_option(option):
  97.     for x in xrange(0,len(sys.argv)):
  98.         if sys.argv[x] == option:
  99.             if x+1 < len(sys.argv):
  100.                 return sys.argv[x+1]
  101.     return None
  102.  
  103. def filter_name(name):
  104.     result=""
  105.     special="<>:\"/\|?*"
  106.     for x in xrange(0,len(name)):
  107.         if special.find(name[x]) == -1:
  108.             result += name[x]
  109.         else:
  110.             result += "_"
  111.     return result
  112.  
  113. def list_file(url, name):
  114.     print name
  115.     return None
  116.  
  117. def show_help():
  118.     print "\ndum.py is MP3 downloader for vk.com\n"
  119.     print "USAGE:\n\tdum.py [options]"
  120.     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"
  121.     print "\t-h  Show this help\n"
  122.     print "\t-L  List contents.\n\t\tIf flag is set, files will be listed\n\t\tonly, otherwise they will be downloaded.\n"
  123.     print "\t-m [MATCH]  List/download every file, that has MATCH in it's name\n"
  124.     return None
  125.  
  126. # EXECUTION
  127. filename = ""
  128. if len(sys.argv) == 2:
  129.     filename=sys.argv[1]
  130. else:
  131.     filename=get_option("-f")
  132. if not (filename != None and os.path.isfile(filename) and filename.find(".htm") != -1):
  133.     filename = get_html_name()
  134. l_url=""
  135. if filename != None:
  136.     match = get_option("-m")
  137.     only_list = get_flag("-L")
  138.     only_help = get_flag("-h")
  139.     if( only_help == True ):
  140.         show_help()
  141.         sys.exit(0)
  142.     if( only_list ==  True ):
  143.         file_action = list_file
  144.     else:
  145.         file_action = download_file
  146.     # print filename
  147.     vk_file = open(filename, "r")
  148.     line = vk_file.readline()
  149.     while (line != None) and (line != ""):
  150.         line=line.decode('cp1251').encode('utf8')
  151.         if(line.find("<input type=\"hidden\" id=\"audio_info") != -1):
  152.             l_url=get_url(line)
  153.             trash=vk_file.readline()
  154.             trash=vk_file.readline()
  155.             name_line=vk_file.readline()
  156.             name_line=name_line.decode('cp1251').encode('utf8')
  157.             band=filter_name(get_band_name(name_line))
  158.             song=filter_name(get_song_name(name_line))
  159.             if (band != None and song != None and l_url != None):
  160.                 if(match != None):
  161.                     if (band.find(match) != -1 or song.find(match) != -1):
  162.                         # print band + "\t" +  song + "\t" + l_url
  163.                         file_action(l_url, band + " - " + song + ".mp3")
  164.                 else:
  165.                     file_action(l_url, band + " - " + song + ".mp3")
  166.                     #print band + "\t" +  song + "\t" + l_url
  167.         line=vk_file.readline()
  168. else:
  169.     print "ERROR: No html files found"
  170.     show_help()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement