def exampleWriteMultiple(writer): #Create an Eslastic Search connection esConnect = EsConnect('api.exiletools.com', 80, 'apikey:DEVELOPMENT-Indexer') client = esConnect.connect() res = client.search("index", body={ "query" : { "filtered" : { "filter" : { "bool" : { "must" : [ { "term" : { "attributes.equipType": "Ring" } } ] } } } }, "size": 100 } ) result = "Exile Tools Index Result: Got %d Hits\n" % res['hits']['total'] ''' Here is an example of writing multiple strings to a file with one call. You can concatenate strings together using the "+" operator. Here is an example using a variable: concatResult = '**** Start of Output ****\n\n' + 'Writing output on one write call\n\n' + result + '\n\n**** End of Output****\n\n' json.dumps(res, indent=4) - This takes the JSON response and formats it nicely into a string. ''' writer.write('**** Start of Output ****\n\n' + 'Writing output on one write call\n\n' + result + '\n**** End of Output****\n\n') ''' Here is an example of writing multiple strings separately. ''' writer.append("**** Start of Output ****\n\n") writer.append("Writing output on multiple write calls.\n\n") writer.append( result) writer.append("Raw JSON Result:\n\n") writer.append(json.dumps(res, indent=4)) writer.append('\n**** End of Output****\n\n') return 0
def exile_search(): ''' Email Settings sender_address = "*****@*****.**" recepient_address = "*****@*****.**" email_password = "******" ''' sender_address = None recepient_address = "" email_password = None #Create an Eslastic Search connection esConnect = EsConnect('api.exiletools.com', 80, 'apikey:DEVELOPMENT-Indexer') client = esConnect.connect() #date.fromtimestamp(time.time()) startTime = time.time() hourMili = 3600000 dayMili = 86400 * 1000.0 weekMili = dayMili * 7 monthMili = weekMili * 4 currentMili = time.time() * 1000.0 ''' Time query ranges ''' #queryLastHour = int(currentMils - hourMili) #queryLast12Hours = int(currentMils - (hourMili * 12)) queryLastDay = int(currentMili - dayMili) #queryLastWeek = int(currentMils - weekMili) #queryLastMonth = int(currentMils - monthMili) localtime = time.asctime( time.localtime(time.time()) ) print('Starting Query at: %s' % localtime) #Main Query res = client.search( index="index", body={ "query": { "filtered": { "query": { "bool": { "must": { "term" : { "attributes.baseItemType": { "value" : "Jewel" } } } } }, "filter": { "bool": { "must" : [ { "term" : { "attributes.league" : "Talisman" } }, { "range": { "shop.chaosEquiv" : { "lte": 50 } } }, { "range": { "shop.chaosEquiv" : { "gt": 0 } } }, { "range": { "shop.updated" : { "gte": queryLastDay } } }, { "term" : { "shop.verified" : "YES" } } ], "should" : [ { "range": { "modsTotal.#% increased Critical Strike Chance with One Handed Melee Weapons": { "gte": 14 } } }, { "range": { "modsTotal.#% increased maximum Life": { "gte": 5 } } }, { "range": { "modsTotal.#% increased maximum Energy Shield": { "gte": 6 } } }, { "range": { "modsTotal.#% increased Critical Strike Chance for Spells": { "gte": 10 } } }, { "range": { "modsTotal.#% increased Global Critical Strike Chance": { "gte": 6 } } }, { "range": { "modsTotal.#% increased Melee Critical Strike Chance": { "gte": 10 } } }, { "range": { "modsPseudo.+#% Total to Elemental Resistances": { "gte": 20 } } }, { "range": { "modsTotal.+# Mana gained for each Enemy hit by your Attacks": {"gte": 2 } } }, { "range": { "modsTotal.#% increased Critical Strike Chance with Cold Skills": {"gte": 10 } } }, { "range": { "modsTotal.#% increased Critical Strike Chance with Elemental Skills": {"gte": 14 } } }, { "range": { "modsTotal.#% increased Critical Strike Chance with Fire Skills": {"gte": 10 } } }, { "range": { "modsTotal.#% increased Critical Strike Chance with lightning Skills": {"gte": 10 } } }, { "range": { "modsTotal.#% increased Critical Strike Multiplier with Cold Skills": {"gte": 10 } } }, { "range": { "modsTotal.#% increased Critical Strike Multiplier with Elemental Skills": {"gte": 10 } } }, { "range": { "modsTotal.#% increased Critical Strike Multiplier with Fire Skills": {"gte": 10 } } }, { "range": { "modsTotal.#% increased Critical Strike Multiplier with lightning Skills": {"gte": 10 } } }, { "range": { "modsTotal.#% increased Critical Strike Multiplier for Spells": { "gte": 10 } } }, { "range": { "modsTotal.#% increased Spell Damage while holding a Shield": { "gte": 14 } } }, { "range": { "modsTotal.#% increased Attack Speed with Daggers": { "gte": 6 } } }, { "range": { "modsTotal.#% increased Attack Speed while holding a Shield": { "gte": 6 } } }, ], "minimum_should_match" : 4 } } } }, "size": 100 } ) localtime = time.asctime( time.localtime(time.time()) ) print ('Query completed at: %s \n' % localtime) endTime = time.time() duration = endTime - startTime result = "Exile Tools Index Result: Got %d Hits and took: %d seconds." % (res['hits']['total'], duration) exceptLimit = 10 writeToFile(os.curdir, "result.txt", result) print result, "\n" for hit in res['hits']['hits']: try: updtimestamp = datetime.datetime.fromtimestamp((hit["_source"]["shop"]["updated"]) / 1000).strftime('%Y-%m-%d %H:%M:%S') line = 'Item {fullName} is for sale for {chaosEquiv} by seller {sellerAccount}, last updated on {updated}\n {source}'.format( fullName = hit["_source"]["info"]["fullName"], chaosEquiv=hit["_source"]["shop"]["chaosEquiv"], sellerAccount=hit["_source"]["shop"]["sellerAccount"], updated=updtimestamp, source=hit["_source"]["modsTotal"]) print line print appendToFile(os.curdir, "result.txt", line) appendToFile(os.curdir, "result.txt", "\r\n") except: print "Unexpected Error:", sys.exc_info()[0] exceptLimit -= 1 if exceptLimit == 0: raise #Print the result to the console #Write the result to a file. writeToFile(os.curdir, "result2.json", json.dumps(res, indent=4)) #Email the result as both the body and as an attached document. #The email is commented out by default. if sender_address != None and recepient_address != None and email_password != None: mail = Mail("smtp.gmail.com") mail.connect(sender_address, email_password) print("Sending mail") mail.send(recepient_address, "test", result, result, "doc.txt") print("Mail sent OK") return 0
def exile_search(): ''' Email Settings sender_address = "*****@*****.**" recepient_address = "*****@*****.**" email_password = "******" ''' sender_address = None recepient_address = None email_password = None #Create an Eslastic Search connection esConnect = EsConnect('api.exiletools.com', 80, 'apikey:DEVELOPMENT-Indexer') client = esConnect.connect() res = client.search("index", body={ "aggs" : { "leagues" : { "terms" : { "field" : "attributes.league", "min_doc_count":5000, "size":0 } } }, "query" : { "filtered" : { "filter" : { "bool" : { "must" : [ { "term" : { "attributes.equipType": "Ring" } } ] } } } }, "sort": [ { "shop.chaosEquiv": { "order": "asc" } } ], "size": 100 } ) result = "Exile Tools Index Result: Got %d Hits" % res['hits']['total'] #Print the result to the console print(result) #Write the result to a file. writeToFile(os.curdir, "result.txt", result) #Email the result as both the body and as an attached document. #The email is commented out by default. if sender_address != None and recepient_address != None and email_password != None: mail = Mail("smtp.gmail.com") mail.connect(sender_address, email_password) print("Sending mail") mail.send(recepient_address, "test", result, result, "doc.txt") print("Mail sent OK") return 0