Add random, amnesiac method #10

Merged
tyler merged 3 commits from memecount-random-meme into master 2023-02-24 16:42:02 +00:00
Showing only changes of commit f903f4e10c - Show all commits

View File

@ -43,3 +43,31 @@ class getRandomFile(Resource):
choice = random.choice(tuple(client.getCurrentMemeList()))
return make_response(client.getMeme(choice))
@api.route('/psuedorandom')
@api.doc(description="Returns a psuedorandom meme. Will not return the same meme for a set number of requests")
class getRandomFile(Resource):
cache = []
maxSize = 100
@api.doc('get')
@api.response(200, 'Sucess')
@api.response(500, 'S3 Error')
def get(self):
client = getClientSafely()
if client is None:
return {
"message" : "Error connecting to S3"
}, 500
choice = random.choice(tuple(client.getCurrentMemeList()))
while choice in self.cache:
choice = random.choice(tuple(client.getCurrentMemeList()))
self.cache.append(choice)
if len(self.cache) > self.maxSize:
self.cache.pop()
logging.debug("Contents of cache : " + str(self.cache))
return make_response(client.getMeme(choice))