senseetive api modification.
--- a/src/egonomy/views.py Wed Mar 13 18:27:37 2013 +0100
+++ b/src/egonomy/views.py Thu Mar 14 15:04:03 2013 +0100
@@ -1,4 +1,3 @@
-from base64 import b64encode
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
@@ -14,6 +13,7 @@
from egonomy.search_indexes.paginator import SearchPaginator
from egonomy.search_indexes.query import ModelRelatedSearchQuerySet
from haystack.query import RelatedSearchQuerySet
+from unicodedata import normalize
import json
import os
import uuid
@@ -244,7 +244,8 @@
</svg>'
# We save the svg file
uid = str(uuid.uuid1())
- svg_file = open(os.path.join(settings.BATIK_RASTERIZER_TEMP_FOLDER, uid + '.svg'),'w')
+ svg_file_path = os.path.join(settings.BATIK_RASTERIZER_TEMP_FOLDER, uid + '.svg')
+ svg_file = open(svg_file_path,'w')
svg_file.write(svg)
svg_file.close()
# We execute the batik command
@@ -268,32 +269,52 @@
else:
return HttpResponseForbidden("Failed to connect to Senseetive API (1)")
- # We create the contents.read request and load the picture
+ # We create the contents.read request and load the picture
+ png_file_path = os.path.join(settings.BATIK_RASTERIZER_TEMP_FOLDER, uid + '.png')
# (queryid 50d0574a03000043102a5177 for AS algorythm,
# 50d0574a0300000f102a5177 pigment)
# 50d0574a03000021102a5177 Gabor
files = {
- 'image0':open(os.path.join(settings.BATIK_RASTERIZER_TEMP_FOLDER, uid + '.png'), 'rb').read(),
+ 'image0':open(png_file_path, 'rb').read(),
}
params = {
"xaction":"contents.read",
"xsessionid":session_id,
"queryid":"50d0574a03000043102a5177",
- "_order_by_":"score",
+ "_order_by_":"score[:desc]",
"_row_first_":"0",
"_row_last_":"10",
"requestfieldname":"image0"
}
-
+ # We make the request
req = requests.post(url=settings.SENSEETIVE_API_URL, data=params, files=files, verify=False)
- #logger.debug(req.text)
resp = req.json()
+ # Now that we have a response, we can remove the svg and png files
+ try:
+ os.remove(svg_file_path)
+ except OSError:
+ pass
+ try:
+ os.remove(png_file_path)
+ except OSError:
+ pass
+ # We parse the response
if "contentlist" not in resp:
return HttpResponseForbidden("Failed to connect to Senseetive API (3) : " + str(resp))
+ # We get the content list
contents = resp["contentlist"]
- keywords = []
+ keywords = {}
for c in contents:
- keywords += c["keywordlist"]
+ # For each content's keyword, we associate or add its score.
+ score = c["contentimagelist"][0]["score"]
+ for kw in c["keywordlist"]:
+ kw = normalize('NFC',kw)
+ if kw in keywords:
+ keywords[kw] = keywords[kw] + score
+ else:
+ keywords[kw] = score
+ # We sort the keywords by descending score
+ keywords = sorted(keywords, key=keywords.get, reverse=True)
- return HttpResponse(json.dumps(sorted(set(keywords))))
+ return HttpResponse(json.dumps(keywords))