From f6f4319bf417a900844495a0044e1c8191d08bb6 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Fri, 24 Feb 2023 16:19:38 -0500 Subject: [PATCH] Add tagging --- src/api/search.py | 12 +++++++++--- src/s3Client.py | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/api/search.py b/src/api/search.py index 42bbe84..4d235a0 100644 --- a/src/api/search.py +++ b/src/api/search.py @@ -26,15 +26,19 @@ class exactSearch(Resource): return { "found" : True, "url" : "/resource/exact/" + query, - "name" : query + "name" : query, + "tags" : client.getTagsOnMeme(query), } else: return { "found" : False } @api.route('/close/') +@api.route('/', doc={ + "description" : "Alias for /close/{query}" + }) @api.doc(params={ '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): def get(self, query): client = getClientSafely() @@ -48,7 +52,8 @@ class textualClose(Resource): entry = { "found" : True, "url" : "/resource/exact/" + meme, - "name" : meme + "name" : meme, + "tags" : client.getTagsOnMeme(meme), } listToReturn.append(entry) @@ -57,3 +62,4 @@ class textualClose(Resource): "numberOfResults" : len(foundMemes) } + diff --git a/src/s3Client.py b/src/s3Client.py index 2333ca0..ff13790 100644 --- a/src/s3Client.py +++ b/src/s3Client.py @@ -73,6 +73,7 @@ def getClient(): class Client: allMemes = set() memesToMd5 = dict() + memesToTags = dict() lastCheckedAllMemes = datetime.strptime("2000-01-01 01:01:01", "%Y-%m-%d %H:%M:%S") client = None @@ -97,6 +98,7 @@ class Client: self.allMemes.clear() self.memesToMd5.clear() + self.memesToTags.clear() for obj in self.client.list_objects(S3_BUCKET): if not obj.is_dir: @@ -122,3 +124,27 @@ class Client: else: raise Exception("Requested meme '" + memeName + "' not found") 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 + +