def loginOperation(self): userid = self.request.get("userid") password = self.request.get("password") logging.info(userid) logging.info(password) aRequest = RequestThoughts(userid, password) aRequest.login() aRequest.getThoughts() result = aRequest.parseThoughts() response = {"meta": "", "data": []} if result and len(result) > 0: metaDic = {} metaDic["status"] = "success" metaDic["number"] = len(result) response["meta"] = metaDic response["data"] = result else: response["meta"] = {"status" : "failure"} self.write(json.dumps(response))
def commitUpdateParse(): global parseReUpdateTimes if parseReUpdateTimes > retryTime: logging.error("Update Parse more than 3 times") return False parseReUpdateTimes += 1 logging.info("Re update Parse objects") try: # Clean out old data # 1: Collecting objectIds try: connection = httplib.HTTPSConnection('api.parse.com', 443) params = urllib.urlencode({"keys":"", "limit":1000}) connection.connect() connection.request('GET', '/1/classes/Story?%s' % params, '', { "X-Parse-Application-Id": parseApplicationID, "X-Parse-REST-API-Key": parseRESTApiKey }) result = json.loads(connection.getresponse().read()) objectIdsToBeDeleted = [] for e in result["results"]: objectIdsToBeDeleted.append(e["objectId"]) except: logging.error("Get objectIds failed") return commitUpdateParse() logging.info("To delete " + str(len(objectIdsToBeDeleted)) + " objects") # 2: Construct delete diction requestDictionary = {} requestDictionary["requests"] = [] # restNumberToBeDeleted = len(objectIdsToBeDeleted) while len(objectIdsToBeDeleted) > 0: logging.info(len(objectIdsToBeDeleted)) requestDictionary["requests"] = [] # Delete 50 objects in batch deletedEntryNumber = 0 for i in range(0, 50): try: deleteRequest = {} deleteRequest["method"] = "DELETE" deleteRequest["path"] = "/1/classes/Story/%s" % objectIdsToBeDeleted[i] except: deletedEntryNumber = i break requestDictionary["requests"].append(deleteRequest) deletedEntryNumber = 50; # 3: Delete 50 entries connection = httplib.HTTPSConnection('api.parse.com', 443) connection.connect() connection.request('POST', '/1/batch', json.dumps(requestDictionary), { "X-Parse-Application-Id": parseApplicationID, "X-Parse-REST-API-Key": parseRESTApiKey, "Content-Type": "application/json" }) result = json.loads(connection.getresponse().read()) logging.info(result) objectIdsToBeDeleted = objectIdsToBeDeleted[deletedEntryNumber:] except: logging.error("Delete Parse Object Failed") return commitUpdateParse() logging.info("Delete Parse Object Succeed!") # Store new data sum = 0 aRequest = RequestThoughts("", "") aRequest.login() aRequest.getThoughts() result = aRequest.parseThoughts() print "Old count: " + str(len(objectIdsToBeDeleted)) print "New count: " + str(len(result)) if len(result) > len(objectIdsToBeDeleted): pushNotification("Hey Wattpader, Allen has posted a new thought!") if result and len(result) > 0: for eachStroy in result: if not createParseObject(eachStroy): logging.error("Create Parse Object Failed") return commitUpdateParse() sum += 1 logging.info("Parse updated: %d" % sum) return True else: logging.error("Login Failed") return False