forked from geektime-geekbang/geektime-ELK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_tmdb.py
61 lines (55 loc) · 2.26 KB
/
query_tmdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import requests
import json
import os
import sys
indexName ="tmdb"
queryFolder ="./query"
headers={"Content-Type":"application/json","Accept":"application/json"}
def search(query, printHighlight):
print query
url = "http://localhost:9200/%s/_search" %indexName
resp = requests.get(url,headers=headers,data=json.dumps(query))
hits = json.loads(resp.text)["hits"]
print "\r\n######################## Results ################################\r\n"
print "No\tScore\t\t\tTitle"
for idx, hit in enumerate(hits["hits"]):
print "%s\t%s\t\t\t%s" %(idx+1, hit["_score"], hit["_source"]["title"])
print "---------------------------------------------------------------"
if printHighlight:
if (("highlight" in hit.keys()) and ("title" in hit["highlight"].keys())):
hl = ";".join(hit["highlight"]["title"]).replace("<em>","\033[1;31;40m").replace("</em>","\033[0m")
print "title: \033[0;32;40m%d hit(s)\033[0m \r\n%s\r\n--" %(len(hit["highlight"]["title"]) ,hl)
if (("highlight" in hit) and ("overview" in hit["highlight"])):
hl = ";".join(hit["highlight"]["overview"]).replace("<em>","\033[1;31;40m").replace("</em>","\033[0m")
print "overview: \033[0;32;40m%d hit(s)\033[0m \r\n%s\r\n--" %( len(hit["highlight"]["overview"]), hl)
def select_query():
print "\r\n>> Please select the query file.\r\n"
queryList=os.listdir(queryFolder)
for idx, queryItem in enumerate(queryList):
print "[%d] %s" %(idx,queryItem)
userInput = raw_input()
try:
selectIndex = int(userInput)
except ValueError:
selectIndex =-1
if(selectIndex==-1 or selectIndex > len(queryList)):
print '\033[31mPlease provide a valid integer \033[0m'
msg = "from 0 to %d." %(len(queryList))
print msg
exit()
queryName = queryList[selectIndex]
fileName="%s/%s" %(queryFolder,queryName)
f = open(fileName)
query = {}
if f:
query = json.loads(f.read());
return query
def main():
highlight = False
for arg in sys.argv:
if arg == "h" or arg == "hl" or arg == "highlight":
highlight = True
query = select_query()
search(query, highlight)
if __name__== "__main__":
main()