def jsonInterfaceThreadWorker(self): """This is the entry point for the JSONInterface thread""" Printing.INFOPRINT("JSON API Thread Started!") #We want to constantly get items while self._interfacing: #Make the request stashes = JSONInterface.getNextPayload() #Now we want to build all the information from these new stashes self._updateModel(stashes) #Is there a duplicate change ID? if stashes is None: #Servers are goofed. Let's just wait. Printing.INFOPRINT( "Unable to contact server. Waiting {} seconds".format( REQUEST_FAIL_WAIT_TIME)) time.sleep(REQUEST_FAIL_WAIT_TIME) elif JSONInterface.duplicateNextChangeID: #We want to wait and try again later Printing.INFOPRINT( "Received duplicate NextChangeID. Waiting {} seconds". format(CHANGE_ID_WAIT_TIME)) time.sleep(CHANGE_ID_WAIT_TIME) #We're out of the loop, meaning we were told to stop going. Printing.INFOPRINT( "No longer interfacing with JSON API. Thread Stopping.") return
def getNextPayload(): #We want to get the next payload of JSON information #Create the URL requestURL = JSONInterface._buildURL() #Get the response from the request Printing.INFOPRINT("Getting Next Payload.") Printing.DEBUGPRINT("Requesting at URL: {}".format(requestURL)) response = requests.get(requestURL) Printing.INFOPRINT("Request Complete.") #Retrieve the JSON from it try: responseJSON = response.json() #Get the next change ID nextChangeID = responseJSON[JSONKeys.NEXT_CHANGE_ID] #Did it match the old one? Set the flag to be so duplicateNextChangeID = ( nextChangeID == JSONInterface.nextChangeID) JSONInterface.duplicateNextChangeID = duplicateNextChangeID JSONInterface.nextChangeID = nextChangeID #Update the Database DBInterface.setNextChangeID(nextChangeID) #And return the stashes stashes = responseJSON[JSONKeys.STASHES] return stashes except: #Unable to get the json. We'll try again later return None
def _handleItem(self, itemDictionary, parentStashTab, previousItems): """Handle converting an item dictionary into an actual item object""" name = itemDictionary[JSONKeys.ITEM_NAME] itemID = itemDictionary[JSONKeys.ITEM_ID] typeLine = itemDictionary[JSONKeys.TYPE_LINE] note = itemDictionary.get(JSONKeys.NOTE, None) iconURL = itemDictionary[JSONKeys.ICON] league = itemDictionary.get(JSONKeys.LEAGUE, DEFAULT_LEAGUE) description = itemDictionary.get(JSONKeys.DESCRIPTION, None) Printing.INFOPRINT("Received Item with ID: {}".format(itemID)) #TODO: Handle more complicated items than just nothing Printing.INFOPRINT("Creating new Item with ID {}".format(itemID)) item = BaseItem(parentStashTab, name, itemID, typeLine, note, iconURL, league, description) parentStashTab.items[itemID] = item #Does this item already exist in our stash tab? if item.itemID in previousItems: #We don't need to remove it from the database previousItems.remove(item.itemID) #Add this item to the repo, and remove the old one if it is still there DBInterface.setItem(item) #And we return the list of items to remove from the stash return previousItems
def _handleStashTab(self, stashTabDictionary): """Handle getting all the relevant information from the stash""" accountName = CLEAN_STRING(stashTabDictionary[JSONKeys.ACCOUNT_NAME]) stashTabID = stashTabDictionary[JSONKeys.STASH_ID] items = stashTabDictionary[JSONKeys.ITEMS] lastCharacterName = CLEAN_STRING( stashTabDictionary[JSONKeys.LAST_CHARACTER_NAME]) public = stashTabDictionary[JSONKeys.PUBLIC] stashTabName = CLEAN_STRING(stashTabDictionary[JSONKeys.STASH]) stashTabType = stashTabDictionary[JSONKeys.STASH_TYPE] Printing.INFOPRINT("Received stash tab with ID: {}".format(stashTabID)) #Do we have an account by this name? account = self._accounts.get(accountName, None) if not account: Printing.INFOPRINT( "Adding new Account with name: {}".format(accountName)) account = Account(accountName, lastCharacterName) self._accounts[accountName] = account #Get the stash accountStash = account.stash #Does this tab exist in the stash? stashTab = accountStash.tabs.get(stashTabID, None) if not stashTab: Printing.INFOPRINT( "Adding new Stash Tab with ID: {}".format(stashTabID)) stashTab = StashTab(accountStash, stashTabID, stashTabName) #Check to see if the database had items in it previousItems = DBInterface.getAllStashItemIDs(stashTabID) #Now we have a stash. Let's add it's items remainingItems = [] for item in items: #Do we need to break early? if not POEGetController._interfacing: break #Handle all the items in this stash tab remainingItems = self._handleItem(item, stashTab, previousItems) #We have some remaining items to remove from the stash tab for itemID in remainingItems: #Do we need to break early? if not POEGetController._interfacing: break #Delete items we didn't add or modify DBInterface.deleteItemByID(itemID) #And now put this stash tab into the database DBInterface.setStashTab(stashTab)
def _launchJSONInterfaceThread(self): #Begin creating the model in another thread self._jsonInterfaceThread = threading.Thread( target=self.jsonInterfaceThreadWorker, name=JSON_THREAD_NAME) #self._jsonInterfaceThread.setDaemon(True) Printing.INFOPRINT("Starting JSON API Thread.") self._jsonInterfaceThread.start()
def deleteItemByID(itemID): """When an item no longer exists, we want to delete it from the database""" deleteResult = _items.delete_one({DatabaseKeys.ITEM_ID: itemID}) #Was something deleted? if deleteResult.acknowledged: #We should print something Printing.DEBUGPRINT("Deleted Item with ID: {}".format(itemID))
def _updateModel(self, stashes): """Update the model with some amount of stash information""" #Did we get None? if stashes is None: #We've got nothing to do return Printing.INFOPRINT("Updating Model with new Payload") for stashTab in stashes: #Do we need to break early? if not POEGetController._interfacing: break #We have a stash dictionary. Let's parse it out into relevant information self._handleStashTab(stashTab) Printing.INFOPRINT("Update Complete")
class JSONInterface(object): """This interface assists in retrieving all the information from the API""" #The Next Change ID for getting the next payload try: nextChangeID = DBInterface.getNextChangeID() Printing.INFOPRINT( "Retrieved NextChangeID {} from Database.".format(nextChangeID)) except TypeError: nextChangeID = None Printing.INFOPRINT("Unable to retrieve NextChangeID from Database...") #Did we get a duplicate and should wait? duplicateNextChangeID = False @staticmethod def _buildURL(): #Did we have a next change id? if JSONInterface.nextChangeID: #We want to include it targetURL = '{}?id={}'.format(API_BASE_URL, JSONInterface.nextChangeID) else: #We just want the first one targetURL = API_BASE_URL #And we have our URL return targetURL @staticmethod def getNextPayload(): #We want to get the next payload of JSON information #Create the URL requestURL = JSONInterface._buildURL() #Get the response from the request Printing.INFOPRINT("Getting Next Payload.") Printing.DEBUGPRINT("Requesting at URL: {}".format(requestURL)) response = requests.get(requestURL) Printing.INFOPRINT("Request Complete.") #Retrieve the JSON from it try: responseJSON = response.json() #Get the next change ID nextChangeID = responseJSON[JSONKeys.NEXT_CHANGE_ID] #Did it match the old one? Set the flag to be so duplicateNextChangeID = ( nextChangeID == JSONInterface.nextChangeID) JSONInterface.duplicateNextChangeID = duplicateNextChangeID JSONInterface.nextChangeID = nextChangeID #Update the Database DBInterface.setNextChangeID(nextChangeID) #And return the stashes stashes = responseJSON[JSONKeys.STASHES] return stashes except: #Unable to get the json. We'll try again later return None
__author__ = 'Devin' import time from POEGet.Util import Printing #Launch an instance for now from POEGet.Controller.POEGetController import POEGetController Printing.INFOPRINT("Creating Controller.") controller = POEGetController() Printing.INFOPRINT("Controller created. Waiting for KeyboardInterrupt.") #Wait for a keyboard interrupt try: while True: time.sleep(0) except KeyboardInterrupt: Printing.INFOPRINT("Received KeyboardInterrupt. Stopping!") controller.shutdown()
def shutdown(self): """Ends the thread safely by setting the interface flag to false""" #We want to kill the thread Printing.INFOPRINT("Killing JSON API Thread!") POEGetController._interfacing = False