Parent: [233e5b] (diff)

Child: [3be5e9] (diff)

Download this file

recollq.py    73 lines (59 with data), 2.0 kB

 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
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
"""A python version of the command line query tool recollq (a bit simplified)
The input string is always interpreted as a query language string.
This could actually be useful for something after some customization
"""
import sys
from getopt import getopt
import recoll
allmeta = ("title", "keywords", "abstract", "url", "mimetype", "mtime",
"ipath", "fbytes", "dbytes", "relevancyrating")
def Usage():
print >> sys.stderr, "Usage: recollq.py [-c conf] [-i extra_index] <recoll query>"
sys.exit(1);
def doquery(db, q):
# Get query object
query = db.query()
# Parse/run input query string
nres = query.execute(q, stemming = 1, stemlang="english")
# Print results:
print "Result count: ", nres
while query.next >= 0 and query.next < nres:
doc = query.fetchone()
print query.next, ":",
for k in ("title", "mtime", "author"):
print k, ":", getattr(doc, k).encode('utf-8')
#doc.setbinurl(bytearray("toto"))
#burl = doc.getbinurl(); print "Bin URL :", doc.getbinurl()
abs = db.makeDocAbstract(doc, query).encode('utf-8')
print abs
print
if len(sys.argv) < 2:
Usage()
confdir=""
extra_dbs = []
# Snippet params
maxchars = 120
contextwords = 4
# Process options: [-c confdir] [-i extra_db [-i extra_db] ...]
options, args = getopt(sys.argv[1:], "c:i:")
for opt,val in options:
if opt == "-c":
confdir = val
elif opt == "-i":
extra_dbs.append(val)
else:
print >> sys.stderr, "Bad opt: ", opt
Usage()
# The query should be in the remaining arg(s)
if len(args) == 0:
print >> sys.stderr, "No query found in command line"
Usage()
q = ""
for word in args:
q += word + " "
print "QUERY: [", q, "]"
db = recoll.connect(confdir=confdir,
extra_dbs=extra_dbs)
db.setAbstractParams(maxchars=maxchars, contextwords=contextwords)
doquery(db, q)