Browse Source

Makes the Python script more pythonic (3)

Godzil 6 years ago
parent
commit
ef1ff8deb9
1 changed files with 34 additions and 35 deletions
  1. 34 35
      src/utils/sendromdata.py

+ 34 - 35
src/utils/sendromdata.py

@@ -1,3 +1,4 @@
+#!/usr/bin/python
 """
 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.
 """
 
-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(url, post_data=None, headers=()):
+def get_page(theurl, post_data=None):
     """
-    Helper method that gets the given URL, handling headers
+    Helper method that gets the given URL
     """
-    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()
+    http = urllib.PoolManager()
+
+    req = http.request('POST', theurl, fields=post_data)
+
+    if req.status == 302 or req.status == 200:
+        return req.data
+
+    return "Failure"
+
 
 if __name__ == '__main__':
-    #print "<gamelist>"
     for filename in sys.argv[1:]:
-        f = open(filename)
-    
-        
+        f = io.open(filename)
+
         try:
-        
             fs = f.read()
             if fs[0:4] == "NES%c" % 0x1A:
-                Flags = ord(fs[6]) & 0x0F;
+                Flags = ord(fs[6]) & 0x0F
                 DiskDude = 0
                 if fs[7:16] == "DiskDude!":
                     DiskDude = 1
@@ -57,22 +56,22 @@ if __name__ == '__main__':
                 if Flags & 0x04:
                     Trainer = 1
                 
-                print " <game>"
-                print "  <name>%s</name>" % filename
-                print "  <sha>%s</sha>" % sha.new(fs).hexdigest()
-                print "  <md5>%s</md5>" % md5.new(fs).hexdigest()
-                print "  <mapperID>%d</mapperID>" % mapperID
-                print "  <prgsize>%d</prgsize>" % prgsize
-                print "  <chrsize>%d</chrsize>" % chrsize
-                print "  <miror>%d</miror>" % mirror
-                print "  <sram>%d</sram>" % sram
-                print "  <trainer>%d</trainer>" % Trainer
-                print "  <diskdude>%d</diskdude>" % DiskDude
-                print " </game>"
+                print(" <game>")
+                print("  <name>{filename}</name>".format(filename=filename))
+                print("  <sha>{sha}</sha>".format(sha=sha.new(fs).hexdigest()))
+                print("  <md5>{md5}</md5>".format(md5=md5.new(fs).hexdigest()))
+                print("  <mapperID>{id}</mapperID>".format(id=mapperID))
+                print("  <prgsize>{size}</prgsize>".format(size=prgsize))
+                print("  <chrsize>{size}</chrsize>".format(size=chrsize))
+                print("  <miror>{mirror}</miror>".format(mirror=mirror))
+                print("  <sram>{sram}</sram>".format(sram=sram))
+                print("  <trainer>{trainer}</trainer>".format(trainer=Trainer))
+                print("  <diskdude>{diskdude}</diskdude>".format(diskdude=DiskDude))
+                print(" </game>")
                  
                  
                 #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({
                     'n': filename,
@@ -91,4 +90,4 @@ if __name__ == '__main__':
         finally:
             f.close()
             
-    #print "</gamelist>"
+    #print("</gamelist>")