ShaggyZE1

Mabengoogly updater thinger

Aug 4th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. #Mabengoogly updater thinger.
  2. #Lots of Googling needed because I don't know Python but it was the best for this job.
  3. #Inconsistent variable naming scheme ahoy because of lazy copy-pasting.
  4. #I like turtles.  Always remember this.
  5.  
  6. #Get some functions.
  7. #Nomenclature.
  8. import urllib.request
  9. import urllib.error
  10. import zlib
  11. import base64
  12. import json
  13. import os
  14. import sys
  15.  
  16. #Ask the user what version of the game they want to grab info for.
  17. v_version = sys.argv[1]
  18. print("")
  19. #Get the manifest hashy thing for that version of the game (10200 = Mabinogi).
  20. v_url = 'https://download2.nexon.net/Game/nxl/games/10200/10200.' + v_version + 'R.manifest.hash'
  21. print(v_url)
  22.  
  23. #Read that file to see the next filename (actual manifest).
  24. #Yes, the hash that shows up is the actual filename in the same directory.
  25. v_hashfile = urllib.request.urlopen(v_url).read()
  26. v_hashfile = v_hashfile.decode('utf8')
  27.  
  28. #Grab the actual manifest now (that hash filename).
  29. v_url = 'https://download2.nexon.net/Game/nxl/games/10200/' + v_hashfile
  30. print(v_url)
  31. print("Manifest downloaded.")
  32. v_filelist = urllib.request.urlopen(v_url).read()
  33.  
  34. #Now we need to uncompress the manifest.
  35. #It has the list of files, packs, mp3s, dlls, etc.
  36. v_manifestcontents = zlib.decompress(v_filelist)
  37. print("Manifest decompressed.\n")
  38.  
  39. #Ask the user if they want to save the manifest.
  40. v_dumpmanifest = 'y'
  41.  
  42. #If the user said yes, save it.
  43. if v_dumpmanifest in ('y', 'Y', 'yes', 'Yes'):
  44.     with open("manifest.txt", 'wb') as f:
  45.         f.write(v_manifestcontents)
  46.         print("Saved as .\\manifest.txt")
  47.  
  48. #It's JSON, so load it and junk.
  49. v_manifestcontents = json.loads(v_manifestcontents.decode('utf8'))
  50. #Use the encoding specified in the manifest file.
  51. #Because readable text is important.
  52. v_manifestencodingtype = v_manifestcontents['filepath_encoding']
  53.  
  54. #Ask the user what they want to do with this info.
  55.  
  56. print("")
  57.  
  58. #If we're going to dump the filename list...
  59.  
  60. v_filelist = []
  61. for encoded_filename, contents in v_manifestcontents['files'].items():
  62.     #Gotta' decode the names.
  63.     filename = base64.b64decode(encoded_filename).decode(v_manifestencodingtype)
  64.     v_filelist.append(filename)
  65. #Sort the file list because it's in a crazy jumble normally.
  66. with open('filelist.txt', 'w') as f:
  67.     f.write('\n'.join(list(sorted(v_filelist))))
  68. print("Saved as filelist.txt.")
  69.  
  70. #Ask for a specific file, find it, download the parts, merge them and save it.
  71.  
  72. v_goalfile = sys.argv[2]
  73. print("")
  74. #Loop through to get the parts.
  75. for encoded_filename, contents in v_manifestcontents['files'].items():
  76.     #The filenames in the manifest are base64 encoded, gotta' deal with that.
  77.     filename = base64.b64decode(encoded_filename).decode(v_manifestencodingtype)
  78.     #If it's the file the user requested...
  79.     if filename == v_goalfile:
  80.         #Files may be split up.
  81.         v_piecedpatch = b""
  82.         for item in contents['objects']:
  83.             #Construct the download URL for each part of the file.
  84.             v_parturl = 'https://download2.nexon.net/Game/nxl/games/10200/10200/' + item[:2] + "/" + item
  85.             print(v_parturl)
  86.             #Download and uncompress each part, append them together (binary join like copy /b).
  87.             v_piecedpatch += zlib.decompress(urllib.request.urlopen(v_parturl).read())
  88.        
  89.         #Make the directory if needed, save the resulting file.
  90.         os.makedirs(os.path.dirname(filename),exist_ok=True)
  91.         with open(filename, 'wb') as f:
  92.             f.write(v_piecedpatch)
  93.         print("Saved as .\\"+filename)
Add Comment
Please, Sign In to add comment