From 445019d204e7e931acc1d4c3b1b478df2803c4b4 Mon Sep 17 00:00:00 2001 From: Ben Busby <33362396+benbusby@users.noreply.github.com> Date: Tue, 12 May 2020 00:45:56 -0600 Subject: [PATCH] Fixed RAM usage bug Pushing straight to master since this is an extremely simple fix, with a pretty large performance benefit. The Phyme library used for generating a User Agent rhyme was consuming an absolute unit of memory. Now that it's removed, it's using about 10x less memory, at the cost of User Agents being not as funny anymore. --- app/request.py | 8 ++++---- app/rhyme.py | 25 ------------------------- requirements.txt | 1 - 3 files changed, 4 insertions(+), 30 deletions(-) delete mode 100644 app/rhyme.py diff --git a/app/request.py b/app/request.py index 471f4f4..aebc716 100644 --- a/app/request.py +++ b/app/request.py @@ -1,6 +1,6 @@ -from app import rhyme from io import BytesIO import pycurl +import random import urllib.parse as urlparse # Base search url @@ -16,9 +16,9 @@ VALID_PARAMS = ['tbs', 'tbm', 'start', 'near'] def gen_user_agent(normal_ua): is_mobile = 'Android' in normal_ua or 'iPhone' in normal_ua - mozilla = rhyme.get_rhyme('Mo') + rhyme.get_rhyme('zilla') - firefox = rhyme.get_rhyme('Fire') + rhyme.get_rhyme('fox') - linux = rhyme.get_rhyme('Lin') + 'ux' + mozilla = random.choice(['Moo', 'Woah', 'Bro', 'Slow']) + 'zilla' + firefox = random.choice(['Choir', 'Squier', 'Higher', 'Wire']) + 'fox' + linux = random.choice(['Win', 'Sin', 'Gin', 'Fin', 'Kin']) + 'ux' if is_mobile: return MOBILE_UA.format(mozilla, firefox) diff --git a/app/rhyme.py b/app/rhyme.py deleted file mode 100644 index 23b9137..0000000 --- a/app/rhyme.py +++ /dev/null @@ -1,25 +0,0 @@ -import itertools -from Phyme import Phyme -import random -import sys -import time - -random.seed(time.time()) - -ph = Phyme() - - -def get_rhyme(word): - # Get all rhymes and merge to one list (normally separated by syllable count) - rhymes = ph.get_perfect_rhymes(word) - rhyme_vals = list(itertools.chain.from_iterable(list(rhymes.values()))) - - # Pick a random rhyme and strip out any non alpha characters - rhymed_word = rhyme_vals[random.randint(0, len(rhyme_vals) - 1)] - rhymed_word = ''.join(letter for letter in rhymed_word if letter.isalpha()) - - return rhymed_word.capitalize() - - -if __name__ == '__main__': - print(get_rhyme(sys.argv[1])) diff --git a/requirements.txt b/requirements.txt index 02e43ce..5a6a937 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,6 @@ Flask==1.1.1 itsdangerous==1.1.0 Jinja2==2.10.3 MarkupSafe==1.1.1 -Phyme==0.0.9 pycparser==2.19 pycurl==7.43.0.4 pyOpenSSL==19.1.0