Makes the Python script more pythonic (3)

This commit is contained in:
Godzil 2018-02-01 15:32:26 +00:00
parent 8f51c4712c
commit ef1ff8deb9

View File

@ -1,3 +1,4 @@
#!/usr/bin/python
""" """
Simple peTI-NESulator game database importer Simple peTI-NESulator game database importer
@ -5,36 +6,34 @@ This python application will generate a XML file on the output with all informat
game database. game database.
""" """
import sys, md5, sha, urllib, urlparse import urllib3 as urllib
import io
import sys
import hashlib.md5 as md5
import hashlib.sha as sha
def get_page(theurl, post_data=None):
"""
Helper method that gets the given URL
"""
http = urllib.PoolManager()
req = http.request('POST', theurl, fields=post_data)
if req.status == 302 or req.status == 200:
return req.data
return "Failure"
def get_page(url, post_data=None, headers=()):
"""
Helper method that gets the given URL, handling headers
"""
opener = urllib.URLopener()
for k, v in headers:
opener.addheader(k, v)
try:
f = opener.open(url, post_data)
except IOError, e:
if e[1] == 302:
return '<html></html>'
else:
raise
return f.read()
if __name__ == '__main__': if __name__ == '__main__':
#print "<gamelist>"
for filename in sys.argv[1:]: for filename in sys.argv[1:]:
f = open(filename) f = io.open(filename)
try: try:
fs = f.read() fs = f.read()
if fs[0:4] == "NES%c" % 0x1A: if fs[0:4] == "NES%c" % 0x1A:
Flags = ord(fs[6]) & 0x0F; Flags = ord(fs[6]) & 0x0F
DiskDude = 0 DiskDude = 0
if fs[7:16] == "DiskDude!": if fs[7:16] == "DiskDude!":
DiskDude = 1 DiskDude = 1
@ -57,22 +56,22 @@ if __name__ == '__main__':
if Flags & 0x04: if Flags & 0x04:
Trainer = 1 Trainer = 1
print " <game>" print(" <game>")
print " <name>%s</name>" % filename print(" <name>{filename}</name>".format(filename=filename))
print " <sha>%s</sha>" % sha.new(fs).hexdigest() print(" <sha>{sha}</sha>".format(sha=sha.new(fs).hexdigest()))
print " <md5>%s</md5>" % md5.new(fs).hexdigest() print(" <md5>{md5}</md5>".format(md5=md5.new(fs).hexdigest()))
print " <mapperID>%d</mapperID>" % mapperID print(" <mapperID>{id}</mapperID>".format(id=mapperID))
print " <prgsize>%d</prgsize>" % prgsize print(" <prgsize>{size}</prgsize>".format(size=prgsize))
print " <chrsize>%d</chrsize>" % chrsize print(" <chrsize>{size}</chrsize>".format(size=chrsize))
print " <miror>%d</miror>" % mirror print(" <miror>{mirror}</miror>".format(mirror=mirror))
print " <sram>%d</sram>" % sram print(" <sram>{sram}</sram>".format(sram=sram))
print " <trainer>%d</trainer>" % Trainer print(" <trainer>{trainer}</trainer>".format(trainer=Trainer))
print " <diskdude>%d</diskdude>" % DiskDude print(" <diskdude>{diskdude}</diskdude>".format(diskdude=DiskDude))
print " </game>" print(" </game>")
#will fill the DB : #will fill the DB :
url = "http://127.0.0.1/~mtrapier/nesstat/add.php" url = "http://127.0.0.1/nesstat/add.php"
html = get_page(url, urllib.urlencode({ html = get_page(url, urllib.urlencode({
'n': filename, 'n': filename,
@ -91,4 +90,4 @@ if __name__ == '__main__':
finally: finally:
f.close() f.close()
#print "</gamelist>" #print("</gamelist>")