Merge pull request 'Add tagging to replies' (#12) from add-tagging-to-reply into master

Reviewed-on: tyler/Meme-service#12
This commit is contained in:
Tyler Perkins 2023-02-24 21:21:52 +00:00
commit 3d7618516b
2 changed files with 35 additions and 3 deletions

View File

@ -26,15 +26,19 @@ class exactSearch(Resource):
return { return {
"found" : True, "found" : True,
"url" : "/resource/exact/" + query, "url" : "/resource/exact/" + query,
"name" : query "name" : query,
"tags" : client.getTagsOnMeme(query),
} }
else: else:
return { "found" : False } return { "found" : False }
@api.route('/close/<string:query>') @api.route('/close/<string:query>')
@api.route('/<string:query>', doc={
"description" : "Alias for /close/{query}"
})
@api.doc(params={ @api.doc(params={
'query' : 'Search query to attempt to compare against' 'query' : 'Search query to attempt to compare against'
},description="FInd a meme thats close using levenshtein distance") },description="Find a meme thats close using levenshtein distance")
class textualClose(Resource): class textualClose(Resource):
def get(self, query): def get(self, query):
client = getClientSafely() client = getClientSafely()
@ -48,7 +52,8 @@ class textualClose(Resource):
entry = { entry = {
"found" : True, "found" : True,
"url" : "/resource/exact/" + meme, "url" : "/resource/exact/" + meme,
"name" : meme "name" : meme,
"tags" : client.getTagsOnMeme(meme),
} }
listToReturn.append(entry) listToReturn.append(entry)
@ -57,3 +62,4 @@ class textualClose(Resource):
"numberOfResults" : len(foundMemes) "numberOfResults" : len(foundMemes)
} }

View File

@ -73,6 +73,7 @@ def getClient():
class Client: class Client:
allMemes = set() allMemes = set()
memesToMd5 = dict() memesToMd5 = dict()
memesToTags = dict()
lastCheckedAllMemes = datetime.strptime("2000-01-01 01:01:01", "%Y-%m-%d %H:%M:%S") lastCheckedAllMemes = datetime.strptime("2000-01-01 01:01:01", "%Y-%m-%d %H:%M:%S")
client = None client = None
@ -97,6 +98,7 @@ class Client:
self.allMemes.clear() self.allMemes.clear()
self.memesToMd5.clear() self.memesToMd5.clear()
self.memesToTags.clear()
for obj in self.client.list_objects(S3_BUCKET): for obj in self.client.list_objects(S3_BUCKET):
if not obj.is_dir: if not obj.is_dir:
@ -122,3 +124,27 @@ class Client:
else: else:
raise Exception("Requested meme '" + memeName + "' not found") raise Exception("Requested meme '" + memeName + "' not found")
return None return None
def getTagsOnMeme(self, memeName: str):
"""
Returns the S3 Tags object for a given meme
"""
if not isinstance(memeName, str):
raise Exception("paramater memeName is of improper type, expected a str")
if memeName in self.memesToTags:
return self.memesToTags[memeName]
memeSet = self.getCurrentMemeList()
if memeName in memeSet:
reply = self.client.get_object_tags(bucket_name=S3_BUCKET,
object_name=memeName)
self.memesToTags[memeName] = reply
return reply
else:
raise Exception("Requested meme '" + memeName + "' not found")
return None