DestinyDevs / BungieNetPlatform

A community run wiki for the Bungie.net Platform APIs.
https://destinydevs.github.io/BungieNetPlatform/
MIT License
108 stars 12 forks source link

Python version (2.X) not specfied for manifest, needed a couple clarifications #4

Closed jam-time closed 7 years ago

jam-time commented 7 years ago

I redid the code to work in 3.X, in the event someone might want it. `#All of these imports used, if the code is broken down into several sections like it is on the wiki,

might not make sense to include all of them at the beginning, but will save time for new devs

import requests import zipfile import os import pickle import json import sqlite3

def get_manifest(): manifest_url = 'http://www.bungie.net/Platform/Destiny/Manifest/'

 #get the manifest location from the json
 r = requests.get(manifest_url)
 manifest = r.json()
 mani_url = 'http://www.bungie.net'+manifest['Response']['mobileWorldContentPaths']['en']

 #Download the file, write it to 'MANZIP'
 r = requests.get(mani_url)
 with open("MANZIP", "wb") as zip:
     zip.write(r.content)
 print("Download Complete!")

 #Extract the file contents, and rename the extracted file
 # to 'Manifest.content'
 with zipfile.ZipFile('MANZIP') as zip:
     name = zip.namelist()
     zip.extractall()
 os.rename(name[0], 'Manifest.content')
 print('Unzipped!')

hashes = { 'DestinyActivityDefinition': 'activityHash', 'DestinyActivityTypeDefinition': 'activityTypeHash', 'DestinyClassDefinition': 'classHash', 'DestinyGenderDefinition': 'genderHash', 'DestinyInventoryBucketDefinition': 'bucketHash', 'DestinyInventoryItemDefinition': 'itemHash', 'DestinyProgressionDefinition': 'progressionHash', 'DestinyRaceDefinition': 'raceHash', 'DestinyTalentGridDefinition': 'gridHash', 'DestinyUnlockFlagDefinition': 'flagHash', 'DestinyHistoricalStatsDefinition': 'statId', 'DestinyDirectorBookDefinition': 'bookHash', 'DestinyStatDefinition': 'statHash', 'DestinySandboxPerkDefinition': 'perkHash', 'DestinyDestinationDefinition': 'destinationHash', 'DestinyPlaceDefinition': 'placeHash', 'DestinyActivityBundleDefinition': 'bundleHash', 'DestinyStatGroupDefinition': 'statGroupHash', 'DestinySpecialEventDefinition': 'eventHash', 'DestinyFactionDefinition': 'factionHash', 'DestinyVendorCategoryDefinition': 'categoryHash', 'DestinyEnemyRaceDefinition': 'raceHash', 'DestinyScriptedSkullDefinition': 'skullHash', 'DestinyGrimoireCardDefinition': 'cardId' }

hashes_trunc = { 'DestinyInventoryItemDefinition': 'itemHash', 'DestinyTalentGridDefinition': 'gridHash', 'DestinyHistoricalStatsDefinition': 'statId', 'DestinyStatDefinition': 'statHash', 'DestinySandboxPerkDefinition': 'perkHash', 'DestinyStatGroupDefinition': 'statGroupHash' }

def build_dict(hash_dict):

connect to the manifest

 con = sqlite3.connect('manifest.content')
 print('Connected')
 #create a cursor object
 cur = con.cursor()

 all_data = {}
 #for every table name in the dictionary
 for table_name in hash_dict.keys():
     #get a list of all the jsons from the table
     cur.execute('SELECT json from '+table_name)
     print('Generating '+table_name+' dictionary....')

     #this returns a list of tuples: the first item in each tuple is our json
     items = cur.fetchall()

     #create a list of jsons
     item_jsons = [json.loads(item[0]) for item in items]

     #create a dictionary with the hashes as keys
     #and the jsons as values
     item_dict = {}
     hash = hash_dict[table_name]
     for item in item_jsons:   
         item_dict[item[hash]] = item

     #add that dictionary to our all_data using the name of the table
     #as a key. 
     all_data[table_name] = item_dict 
 print('Dictionary Generated!')
 return all_data

check if pickle exists, if not create one.

if os.path.isfile(r'path\to\file\manifest.content') == False: get_manifest()
all_data = build_dict(hashes) with open('manifest.pickle', 'wb') as data: pickle.dump(all_data, data) print("'manifest.pickle' created!\nDONE!") else: print('Pickle Exists')

with open('manifest.pickle', 'rb') as data: all_data = pickle.load(data)

hash = 1274330687 ghorn = all_data['DestinyInventoryItemDefinition'][hash]

print('Name: '+ghorn['itemName']) print('Type: '+ghorn['itemTypeName']) print('Tier: '+ghorn['tierTypeName']) print(ghorn['itemDescription'])`

jam-time commented 7 years ago

'insert code' didn't work above, but all but the first line is supposed to be in the code

lowlines commented 7 years ago

which wiki page is this supposed to go on? Might also be cleaner to submit a pastebin code snippet just in case the issue tracker messes with the code formatting.

jam-time commented 7 years ago

It's for the python section of this section:

http://destinydevs.github.io/BungieNetPlatform/docs/Manifest

Really there's only a few differences here and there, mainly, but it's enough of a difference to be annoying to switch it all over.

https://pastebin.com/piPZ0xhg

There's the python 3.X code

lowlines commented 7 years ago

Added this code to the Manifest page. Sorry for the delay, it slipped my mind!