sendromdata.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/python
  2. """
  3. Simple peTI-NESulator game database importer
  4. This python application will generate a XML file on the output with all information needed to import in the peTI-NESulator
  5. game database.
  6. """
  7. import urllib3 as urllib
  8. import io
  9. import sys
  10. import hashlib.md5 as md5
  11. import hashlib.sha as sha
  12. def get_page(theurl, post_data=None):
  13. """
  14. Helper method that gets the given URL
  15. """
  16. http = urllib.PoolManager()
  17. req = http.request('POST', theurl, fields=post_data)
  18. if req.status == 302 or req.status == 200:
  19. return req.data
  20. return "Failure"
  21. if __name__ == '__main__':
  22. for filename in sys.argv[1:]:
  23. f = io.open(filename)
  24. try:
  25. fs = f.read()
  26. if fs[0:4] == "NES%c" % 0x1A:
  27. Flags = ord(fs[6]) & 0x0F
  28. DiskDude = 0
  29. if fs[7:16] == "DiskDude!":
  30. DiskDude = 1
  31. mapperID = ord(fs[6]) >> 4
  32. if DiskDude == 0:
  33. mapperID = mapperID | (ord(fs[7]) & 0xF0)
  34. prgsize = ord(fs[4]) * 16 * 1024
  35. chrsize = ord(fs[5]) * 8 * 1024
  36. mirror = 0
  37. if Flags & 0x01:
  38. mirror = 1
  39. sram = 0
  40. if Flags & 0x02:
  41. sram = 1
  42. Trainer = 0
  43. if Flags & 0x04:
  44. Trainer = 1
  45. print(" <game>")
  46. print(" <name>{filename}</name>".format(filename=filename))
  47. print(" <sha>{sha}</sha>".format(sha=sha.new(fs).hexdigest()))
  48. print(" <md5>{md5}</md5>".format(md5=md5.new(fs).hexdigest()))
  49. print(" <mapperID>{id}</mapperID>".format(id=mapperID))
  50. print(" <prgsize>{size}</prgsize>".format(size=prgsize))
  51. print(" <chrsize>{size}</chrsize>".format(size=chrsize))
  52. print(" <miror>{mirror}</miror>".format(mirror=mirror))
  53. print(" <sram>{sram}</sram>".format(sram=sram))
  54. print(" <trainer>{trainer}</trainer>".format(trainer=Trainer))
  55. print(" <diskdude>{diskdude}</diskdude>".format(diskdude=DiskDude))
  56. print(" </game>")
  57. #will fill the DB :
  58. url = "http://127.0.0.1/nesstat/add.php"
  59. html = get_page(url, urllib.urlencode({
  60. 'n': filename,
  61. 'md5': md5.new(fs).hexdigest(),
  62. 'sha1': sha.new(fs).hexdigest(),
  63. 'm': mapperID,
  64. 'prg': prgsize,
  65. 'chr': chrsize,
  66. 'mir': mirror,
  67. 'sram': sram,
  68. 't': Trainer,
  69. 'd': DiskDude,
  70. }))
  71. print(html)
  72. finally:
  73. f.close()
  74. #print("</gamelist>")