Advertisement
xIntangible

manifest_destiny.py

Jun 17th, 2015
3,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. import requests, zipfile, os
  2. import json, sqlite3,
  3. import pickle #Optional
  4.  
  5. #Reading the Destiny API Manifest in Python. [How I Did It, Anyway]
  6.  
  7. weapon_types = ['Rocket Launcher', 'Scout Rifle', 'Fusion Rifle', 'Sniper Rifle',
  8.                 'Shotgun', 'Machine Gun', 'Pulse Rifle', 'Auto Rifle', 'Hand Cannon', 'Sidearm']
  9.  
  10. #dictionary that tells where to get the hashes for each table
  11. #FULL DICTIONARY
  12. hash_dict = {'DestinyActivityDefinition': 'activityHash',
  13.              'DestinyActivityTypeDefinition': 'activityTypeHash',
  14.              'DestinyClassDefinition': 'classHash',
  15.              'DestinyGenderDefinition': 'genderHash',
  16.              'DestinyInventoryBucketDefinition': 'bucketHash',
  17.              'DestinyInventoryItemDefinition': 'itemHash',
  18.              'DestinyProgressionDefinition': 'progressionHash',
  19.              'DestinyRaceDefinition': 'raceHash',
  20.              'DestinyTalentGridDefinition': 'gridHash',
  21.              'DestinyUnlockFlagDefinition': 'flagHash',
  22.              'DestinyHistoricalStatsDefinition': 'statId',
  23.              'DestinyDirectorBookDefinition': 'bookHash',
  24.              'DestinyStatDefinition': 'statHash',
  25.              'DestinySandboxPerkDefinition': 'perkHash',
  26.              'DestinyDestinationDefinition': 'destinationHash',
  27.              'DestinyPlaceDefinition': 'placeHash',
  28.              'DestinyActivityBundleDefinition': 'bundleHash',
  29.              'DestinyStatGroupDefinition': 'statGroupHash',
  30.              'DestinySpecialEventDefinition': 'eventHash',
  31.              'DestinyFactionDefinition': 'factionHash',
  32.              'DestinyVendorCategoryDefinition': 'categoryHash',
  33.              'DestinyEnemyRaceDefinition': 'raceHash',
  34.              'DestinyScriptedSkullDefinition': 'skullHash',
  35.              'DestinyGrimoireCardDefinition': 'cardId'}
  36.  
  37. def get_manifest():
  38.     manifest_url = 'http://www.bungie.net/Platform/Destiny/Manifest/'
  39.     #get the manifest location from the json
  40.     r = requests.get(manifest_url)
  41.     manifest = r.json()
  42.     mani_url = 'http://www.bungie.net'+manifest['Response']['mobileWorldContentPaths']['en']
  43.     #Download the file, write it to MANZIP
  44.     r = requests.get(mani_url)
  45.     with open("MANZIP", "wb") as zip:
  46.         zip.write(r.content)
  47.     print "Download Complete!"
  48.    
  49.     #Extract the file contents, and rename the extracted file
  50.     # to 'Manifest.content'
  51.     with zipfile.ZipFile('MANZIP') as zip:
  52.         name = zip.namelist()
  53.         zip.extractall()
  54.     os.rename(name[0], 'Manifest.content')
  55.     print 'Unzipped!'
  56.                
  57. def build_dict(hash_dict):
  58.     #connect to the manifest
  59.     con = sqlite3.connect('Manifest.content')
  60.     print 'Connected'
  61.     #create a cursor object
  62.     cur = con.cursor()
  63.    
  64.     all_data = {}
  65.     #for every table name in the dictionary
  66.     for table_name in hash_dict.keys():
  67.         #get a list of all the jsons from the table
  68.         cur.execute('SELECT json from '+table_name)
  69.         print 'Generating '+table_name+' dictionary....'
  70.        
  71.         #this returns a list of tuples: the first item in each tuple is our json
  72.         items = cur.fetchall()
  73.        
  74.         #create a list of jsons
  75.         item_jsons = [json.loads(item[0]) for item in items]
  76.        
  77.         #create a dictionary with the hashes as keys
  78.         #and the jsons as values
  79.         item_dict = {}
  80.         hash = hash_dict[table_name]
  81.         for item in item_jsons:  
  82.             item_dict[item[hash]] = item
  83.    
  84.         #add that dictionary to our all_data using the name of the table
  85.         #as a key.
  86.         all_data[table_name] = item_dict
  87.     print 'Dictionary Generated!'
  88.     return all_data
  89.  
  90.  
  91. #check if pickle exists, if not create one.
  92. if os.path.isfile('manifest.pickle') == False:
  93.     get_manifest()        
  94.     all_data = build_dict(hash_dict)
  95.     with open('manifest.pickle', 'wb') as data:
  96.         pickle.dump(all_data, data)
  97.     print "'manifest.pickle' created!\nDONE!"
  98. else:
  99.     print 'Pickle Exists'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement