Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 4th, 2011  |  syntax: Python  |  size: 4.30 KB  |  views: 426  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # JAD2JAR Creator PRO Third Edition, 0.2.0 ALPHA
  2.  
  3. '''
  4.  
  5. JAD2JAR Creator PRO Third Edition, 0.2.0 ALPHA
  6. Created by Deusdies
  7. Using Python 3 syntax
  8.  
  9. Program downloads JAR file based on the given JAR file
  10.  
  11. TODO:
  12.  
  13. - GUI in Qt
  14. - Opening the default webbrowser if JAR file cannot be found
  15. - Progress bar (if command-line edition retained)
  16. - with GUI do: extract more info from the JAD file, such as size, etc.
  17.  
  18. '''
  19.  
  20. __author__ = 'Deusdies'
  21. __version__ = '0.2ALPHA'
  22.  
  23. import os, sys, urllib.request, urllib.error
  24.  
  25. def find_url(file): # function that extracts the URL from JAD file
  26.     with open(file) as file:
  27.         for line in file:
  28.             infourl = line.find("MIDlet-Info-URL")
  29.             jarurl = line.find("MIDlet-Jar-URL")
  30.             if infourl == 0:
  31.                 k = line.replace("MIDlet-Info-URL: ","")
  32.                 k = k.strip("\n")
  33.             elif jarurl == 0:
  34.                 g = line.replace("MIDlet-Jar-URL: ","")
  35.                 g = g.strip("\n")
  36.         # needs a procedure on what to do in case infourl and jarurl are not actually found (this is rarely the case)
  37.         uri = k + g
  38.         return uri
  39.            
  40. def downloadFile(URL,file='j2jdownload.jar'): # function that actually downloads the file
  41.     metainfo = urllib.request.urlopen(URL).info()
  42.     metainfo = str(metainfo)
  43.     if "java-archive" in metainfo: # detects if the URL extracted is an actual JAR file, rather than HTML (since some
  44.         print("Beginning download... \n") # developers put an address to their web page where JAR URL should be)
  45.         with open(file,'wb') as f:
  46.             r=urllib.request.urlopen(URL).read()
  47.             f.write(r)
  48.             print("Download finished! File saved as " + file + " in the selected directory")
  49.  
  50.     else:
  51.         print("The URL is most likely not pointing to a real JAR file.")
  52.         print("ERROR: Cannot download JAR file. Suggestion: copy/paste the URL provided below. Program now exiting.")
  53.         print(URL)
  54.         # here it'd be awesome if I could code so that the browser is opened automatically
  55.         sys.exit(1) # exits
  56.  
  57. def checkFiles(jadfile): # checks if inputted includes extension and also if the file exists
  58.     if not ".jad" in jadfile:
  59.         r = jadfile + ".jad"
  60.         if not os.path.exists(r):
  61.             print("Could not find JAD file")
  62.             sys.exit(1)
  63.         return r
  64.     else:
  65.         r = jadfile
  66.         return r
  67.  
  68. def validUrl(yorn): # yorn is y or no, questioning the user if the extracted URL looks valid
  69.     if "Y" in yorn:
  70.         return True
  71.     elif "N" or "n" or "no" in yorn:
  72.         return False
  73.     else:
  74.         print("No valid answer given!")
  75.         sys.exit(1)
  76.  
  77. print ('''\n\n\n       ##########################################################
  78.       #WELCOME TO JAD2JAR CREATOR 0.1.1 ALPHA PRO THIRD EDITION#
  79.       ##########################################################
  80.       \n''')
  81.  
  82. if len(sys.argv) < 2 and sys.argv != "help": # parsing arguments given
  83.     jadfile = input("\nInput the name of the JAD file in the current directory\n\n>>> ")
  84.     r = checkFiles(jadfile)
  85.     jarfile = input("\nInput the name of the _JAR_ file (dl'ed file) in the current directory\n\n>>> ")
  86.     if not ".jar" in jarfile:
  87.         jarfile = jarfile + ".jar"
  88.  
  89. elif len(sys.argv) == 3 and sys.argv != "help":
  90.     jadfile = sys.argv[1]
  91.     r = checkFiles(jadfile)
  92.     jarfile = sys.argv[2]
  93.     if not ".jar" in jarfile:
  94.         jarfile = jarfile + ".jar"
  95.  
  96. elif sys.argv[1] == "help":
  97.     print("Usage: j2jarcreator.exe <jadfile> <jarfile>\nExample: j2jarcreator.exe operaMobile.jad operaDownloaded.jar")
  98.     sys.exit(1)
  99.    
  100. else:
  101.     print("Usage: j2jarcreator.exe <jadfile> <jarfile>\nExample: j2jarcreator.exe operaMobile.jad operaDownloaded.jar")
  102.     sys.exit(1)
  103.  
  104. print("This is the URL I extracted:\n")
  105. URL = find_url(r)
  106. print(URL)
  107.  
  108. yorn = input("\nDoes this look like a valid URL to you? Capital Y or N\n\n>>> ")
  109.  
  110. if validUrl(yorn) == True:
  111.     try:
  112.         downloadFile(URL, jarfile)
  113.     except urllib.error.URLError: # raises URLError in case network problems, or similar
  114.         print("Cannot download file, check your internet connection!")
  115.         sys.exit(1)
  116.        
  117. elif validUrl(yorn) == False:
  118.     print("Sorry, but I couldn't extract the URL. :(")
  119.     sys.exit(1)
  120. else:
  121.     pass