# JAD2JAR Creator PRO Third Edition, 0.2.0 ALPHA

'''

JAD2JAR Creator PRO Third Edition, 0.2.0 ALPHA

Created by Deusdies

Using Python 3 syntax

Program downloads JAR file based on the given JAR file

TODO:

- GUI in Qt

- Opening the default webbrowser if JAR file cannot be found

- Progress bar (if command-line edition retained)

- with GUI do: extract more info from the JAD file, such as size, etc.

'''

__author__ = 'Deusdies'

__version__ = '0.2ALPHA'

import os , sys , urllib . request , urllib . error

def find_url ( file ) : # function that extracts the URL from JAD file

with open ( file ) as file :

for line in file :

infourl = line. find ( "MIDlet-Info-URL" )

jarurl = line. find ( "MIDlet-Jar-URL" )

if infourl == 0 :

k = line. replace ( "MIDlet-Info-URL: " , "" )

k = k. strip ( "

" )

elif jarurl == 0 :

g = line. replace ( "MIDlet-Jar-URL: " , "" )

g = g. strip ( "

" )

# needs a procedure on what to do in case infourl and jarurl are not actually found (this is rarely the case)

uri = k + g

return uri

def downloadFile ( URL , file = 'j2jdownload.jar' ) : # function that actually downloads the file

metainfo = urllib . request . urlopen ( URL ) . info ( )

metainfo = str ( metainfo )

if "java-archive" in metainfo: # detects if the URL extracted is an actual JAR file, rather than HTML (since some

print ( "Beginning download...

" ) # developers put an address to their web page where JAR URL should be)

with open ( file , 'wb' ) as f:

r = urllib . request . urlopen ( URL ) . read ( )

f. write ( r )

print ( "Download finished! File saved as " + file + " in the selected directory" )

else :

print ( "The URL is most likely not pointing to a real JAR file." )

print ( "ERROR: Cannot download JAR file. Suggestion: copy/paste the URL provided below. Program now exiting." )

print ( URL )

# here it'd be awesome if I could code so that the browser is opened automatically

sys . exit ( 1 ) # exits

def checkFiles ( jadfile ) : # checks if inputted includes extension and also if the file exists

if not ".jad" in jadfile:

r = jadfile + ".jad"

if not os . path . exists ( r ) :

print ( "Could not find JAD file" )

sys . exit ( 1 )

return r

else :

r = jadfile

return r

def validUrl ( yorn ) : # yorn is y or no, questioning the user if the extracted URL looks valid

if "Y" in yorn:

return True

elif "N" or "n" or "no" in yorn:

return False

else :

print ( "No valid answer given!" )

sys . exit ( 1 )

print ( '''





##########################################################

#WELCOME TO JAD2JAR CREATOR 0.1.1 ALPHA PRO THIRD EDITION#

##########################################################



''' )

if len ( sys . argv ) < 2 and sys . argv != "help" : # parsing arguments given

jadfile = input ( "

Input the name of the JAD file in the current directory



>>> " )

r = checkFiles ( jadfile )

jarfile = input ( "

Input the name of the _JAR_ file (dl'ed file) in the current directory



>>> " )

if not ".jar" in jarfile:

jarfile = jarfile + ".jar"

elif len ( sys . argv ) == 3 and sys . argv != "help" :

jadfile = sys . argv [ 1 ]

r = checkFiles ( jadfile )

jarfile = sys . argv [ 2 ]

if not ".jar" in jarfile:

jarfile = jarfile + ".jar"

elif sys . argv [ 1 ] == "help" :

print ( "Usage: j2jarcreator.exe <jadfile> <jarfile>

Example: j2jarcreator.exe operaMobile.jad operaDownloaded.jar" )

sys . exit ( 1 )

else :

print ( "Usage: j2jarcreator.exe <jadfile> <jarfile>

Example: j2jarcreator.exe operaMobile.jad operaDownloaded.jar" )

sys . exit ( 1 )

print ( "This is the URL I extracted:

" )

URL = find_url ( r )

print ( URL )

yorn = input ( "

Does this look like a valid URL to you? Capital Y or N



>>> " )

if validUrl ( yorn ) == True :

try :

downloadFile ( URL , jarfile )

except urllib . error . URLError : # raises URLError in case network problems, or similar

print ( "Cannot download file, check your internet connection!" )

sys . exit ( 1 )

elif validUrl ( yorn ) == False :

print ( "Sorry, but I couldn't extract the URL. :(" )

sys . exit ( 1 )

else :