Skip to content

Commit

Permalink
search by file type
Browse files Browse the repository at this point in the history
  • Loading branch information
cl4u2 committed Oct 27, 2012
1 parent a6a3cb1 commit 11c0c7e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
12 changes: 8 additions & 4 deletions JsonAPI.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ Operation: exactquery
Description: return the search results that match exactly (i.e. performing an
AND between the exact words) <querystring>.
The optional parameter "ip" restricts the search to a given server IP address.
URL: /json.cgi?op=exactquery&q=<querystring>&ip=<IP address>
The optional parameter "filetype" restricts the search to a given filetype/extension.
URL: /json.cgi?op=exactquery&q=<querystring>&ip=<IP address>&filetype=<extension>

Operation: orquery
Description: return the search results that match the single exact words in
<querystring>.
The optional parameter "ip" restricts the search to a given server IP address.
URL: /json.cgi?op=orquery&q=<querystring>&ip=<IP address>
The optional parameter "filetype" restricts the search to a given filetype/extension.
URL: /json.cgi?op=orquery&q=<querystring>&ip=<IP address>&filetype=<extension>

Operation: likequery
Description: return search results by performing variations on the words in
<querystring>, limiting to no more than <word limit> words.
The optional parameter "ip" restricts the search to a given server IP address.
URL: /json.cgi?op=likequery&q=<querystring>&words=<word limit>&ip=<IP address>
The optional parameter "filetype" restricts the search to a given filetype/extension.
URL: /json.cgi?op=likequery&q=<querystring>&words=<word limit>&ip=<IP address>&filetype=<extension>

Operation: query
Description: return search results by applying in order the exactquery, orquery
and likequery operations, trying to retrieve at least <nresults> results.
The optional parameter "ip" restricts the search to a given server IP address.
URL: /json.cgi?op=query&q=<querystring>&nresults=<nresults>&ip=<IP address>
The optional parameter "filetype" restricts the search to a given filetype/extension.
URL: /json.cgi?op=query&q=<querystring>&nresults=<nresults>&ip=<IP address>&filetype=<extension>
NOTES: nresults default is 200

Operation: whatsnew
Expand Down
24 changes: 14 additions & 10 deletions dbmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ def query(self, query, targetresults=200):
else:
badtags.append(tag)
if len(goodtags) >= 2:
qr.addResultList(self.__andquery(cursor, goodtags, server=query.server), goodtags, "+", True)
qr.addResultList(self.__andquery(cursor, goodtags, server=query.server, filetype=query.filetype), goodtags, "+", True)
usedtags = []
if qr.getLen() < targetresults:
for tag in goodtags:
qr.addResultList(self.__orquery(cursor, [tag], server=query.server), [tag], "/", len(goodtags) == 1)
qr.addResultList(self.__orquery(cursor, [tag], server=query.server, filetype=query.filetype), [tag], "/", len(goodtags) == 1)
usedtags += goodtags
if len(badtags) > 0 and qr.getLen() < targetresults:
lim = 1 + self.likes / len(badtags)
Expand All @@ -234,7 +234,7 @@ def query(self, query, targetresults=200):
liketags = list(set(liketags).difference(set(badtags)).difference(set(usedtags)))
for tag in liketags:
if qr.getLen() < targetresults:
qr.addResultList(self.__orquery(cursor, [tag], server=query.server), [tag], "OR", False)
qr.addResultList(self.__orquery(cursor, [tag], server=query.server, filetype=query.filetype), [tag], "OR", False)
usedtags.append(tag)
if len(goodtags) > 0 and qr.getLen() < targetresults:
lim = 1 + self.likes / len(goodtags)
Expand All @@ -244,7 +244,7 @@ def query(self, query, targetresults=200):
liketags = list(set(liketags).difference(set(goodtags)).difference(set(usedtags)))
for tag in liketags:
if qr.getLen() < targetresults:
qr.addResultList(self.__orquery(cursor, [tag], server=query.server), [tag], "OR", False)
qr.addResultList(self.__orquery(cursor, [tag], server=query.server, filetype=query.filetype), [tag], "OR", False)
usedtags.append(tag)
for j in range(1,4):
if qr.getLen() < targetresults:
Expand All @@ -258,12 +258,12 @@ def query(self, query, targetresults=200):
liketags = list(set(liketags).difference(set(tmptags)).difference(set(usedtags)))
for tag in liketags:
if qr.getLen() < targetresults:
qr.addResultList(self.__orquery(cursor, [tag], server=query.server), [tag], "OR", False)
qr.addResultList(self.__orquery(cursor, [tag], server=query.server, filetype=query.filetype), [tag], "OR", False)
usedtags.append(tag)

cursor.close()
return qr
def __orquery(self, cursor, tags, server=None, timediff=TIMEDIFF):
def __orquery(self, cursor, tags, server=None, filetype=None, timediff=TIMEDIFF):
if len(tags) <=0:
return []
selectionstring = """
Expand All @@ -272,14 +272,16 @@ def __orquery(self, cursor, tags, server=None, timediff=TIMEDIFF):
WHERE (UNIX_TIMESTAMP(resources.timestamp) >= UNIX_TIMESTAMP(NOW()) - %d) """ % timediff
if server:
selectionstring += " AND resources.server = '%s' " % server
if filetype:
selectionstring += " AND resources.filetype = '%s' " % filetype
selectionstring += " AND (tags.tag = '%s' " % tags[0]
for tag in tags[1:]:
selectionstring += " OR tags.tag = '%s' " % tag
selectionstring += ") ORDER BY resources.uri DESC"
cursor.execute(selectionstring)
r = [Resource(uri=e[0], server=e[1], filetype=e[2]) for e in cursor.fetchall()]
return r
def __andquery(self, cursor, tags, server=None, timediff=TIMEDIFF):
def __andquery(self, cursor, tags, server=None, filetype=None, timediff=TIMEDIFF):
if len(tags) <=0:
return []
selectionstring = """
Expand All @@ -290,6 +292,8 @@ def __andquery(self, cursor, tags, server=None, timediff=TIMEDIFF):
selectionstring += " WHERE (UNIX_TIMESTAMP(resources.timestamp) >= UNIX_TIMESTAMP(NOW()) - %d) " % timediff
if server:
selectionstring += " AND resources.server = '%s' " % server
if filetype:
selectionstring += " AND resources.filetype = '%s' " % filetype
selectionstring += " AND t0.tag = '%s' " % tags[0]
for i in range(1,len(tags)):
selectionstring += "AND t%d.tag = '%s' " % (i, tags[i])
Expand Down Expand Up @@ -369,7 +373,7 @@ def exactquery(self, query):
qr = QueryResultS()
cursor = self.conn.cursor()
alltags = list(query.tags)
qr.addResultList(self.__andquery(cursor, alltags, server=query.server), alltags, "+", True)
qr.addResultList(self.__andquery(cursor, alltags, server=query.server, filetype=query.filetype), alltags, "+", True)
cursor.close()
return qr

Expand All @@ -378,7 +382,7 @@ def orquery(self, query):
qr = QueryResultS()
cursor = self.conn.cursor()
alltags = list(query.tags)
qr.addResultList(self.__orquery(cursor, alltags, server=query.server), alltags, "/", True)
qr.addResultList(self.__orquery(cursor, alltags, server=query.server, filetype=query.filetype), alltags, "/", True)
cursor.close()
return qr

Expand All @@ -392,7 +396,7 @@ def likequery(self, query, limit=3):
liketags += self.__taglike(cursor, t, limit/len(alltags))
liketags = list(set(liketags).difference(set(alltags)))
for t in liketags:
qr.addResultList(self.__orquery(cursor, [t], server=query.server), [t], "/", False)
qr.addResultList(self.__orquery(cursor, [t], server=query.server, filetype=query.filetype), [t], "/", False)
cursor.close()
return qr

Expand Down
12 changes: 12 additions & 0 deletions jsonif.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ def query(self, request):
if request.has_key('ip'):
q.server = request['ip']

if request.has_key('filetype'):
q.filetype = request['filetype'].upper()

tsta = time.time()
resp = self.qm.query(q, nres)
tend = time.time()
Expand All @@ -171,6 +174,9 @@ def exactquery(self, request):
if request.has_key('ip'):
q.server = request['ip']

if request.has_key('filetype'):
q.filetype = request['filetype'].upper()

tsta = time.time()
resp = self.qm.exactquery(q)
tend = time.time()
Expand All @@ -195,6 +201,9 @@ def orquery(self, request):
if request.has_key('ip'):
q.server = request['ip']

if request.has_key('filetype'):
q.filetype = request['filetype'].upper()

tsta = time.time()
resp = self.qm.orquery(q)
tend = time.time()
Expand Down Expand Up @@ -228,6 +237,9 @@ def likequery(self, request):
if request.has_key('ip'):
q.server = request['ip']

if request.has_key('filetype'):
q.filetype = request['filetype'].upper()

tsta = time.time()
resp = self.qm.likequery(q, limit)
tend = time.time()
Expand Down

0 comments on commit 11c0c7e

Please sign in to comment.