diff --git a/ImageQ/processor/base.py b/ImageQ/processor/base.py index cb0c5ac..b303376 100644 --- a/ImageQ/processor/base.py +++ b/ImageQ/processor/base.py @@ -34,7 +34,7 @@ import requests from bs4 import BeautifulSoup -from ImageQ.processor.consts import FS, IMAGE_TYPES, SEARCH_QUERY +from ImageQ.processor.consts import FS, IMAGE_TYPES, SEARCH_QUERY, HEADERS from ImageQ.search.models import Prediction @@ -122,13 +122,8 @@ def get_source(url): import requests # headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0'} # prevent caching - headers = { - "Cache-Control": 'no-cache', - "Connection": "keep-alive", - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" - } try: - response = requests.get(url, headers=headers) + response = requests.get(url, headers=HEADERS) html = response.text except Exception as e: raise Exception('ERROR: {}\n'.format(e)) @@ -186,7 +181,7 @@ def predict(self): """ files = {'image': open(self.image_path, 'rb')} if not isinstance(self.prediction_api, type(None)): - r = requests.post(self.prediction_api, files=files) + r = requests.post(self.prediction_api, files=files, headers=HEADERS) else: raise AttributeError("Attribute is of type None") return r.content diff --git a/ImageQ/processor/consts.py b/ImageQ/processor/consts.py index 783018d..3a828bb 100644 --- a/ImageQ/processor/consts.py +++ b/ImageQ/processor/consts.py @@ -164,4 +164,10 @@ def listdir(path: str, exclude= None, "Yahoo": 'https://search.yahoo.com/search?p={}&b={}', "Bing": 'https://www.bing.com/search?q={}&count=10&offset=0&first={}', -} \ No newline at end of file +} + +HEADERS = { + "Cache-Control": 'no-cache', + "Connection": "keep-alive", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" + } \ No newline at end of file diff --git a/ImageQ/search/forms.py b/ImageQ/search/forms.py index 7a96bdc..aa230b0 100644 --- a/ImageQ/search/forms.py +++ b/ImageQ/search/forms.py @@ -106,6 +106,7 @@ def predict(self): try: _predictions = json.loads(bytes.decode(urlpredictor.predict())) except: + print("failed to retrieve prediction from API, using dummy") _predictions = { "predictions": [ { diff --git a/ImageQ/settings/common.py b/ImageQ/settings/common.py index b99446f..0005411 100644 --- a/ImageQ/settings/common.py +++ b/ImageQ/settings/common.py @@ -35,6 +35,8 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'cloudinary_storage', + 'cloudinary', 'ImageQ.search', ] @@ -129,6 +131,7 @@ MEDIA_ROOT = os.path.join(BASE_DIR, 'media') MEDIA_URL = '/media/' + # Extra places for collectstatic to find static files. STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), diff --git a/ImageQ/settings/production.py b/ImageQ/settings/production.py index 813e92b..0c88e00 100644 --- a/ImageQ/settings/production.py +++ b/ImageQ/settings/production.py @@ -19,4 +19,7 @@ 'default': dj_database_url.parse(DATABASE_URL) } -PREDICTION_API = "172.104.78.30/predict" \ No newline at end of file +PREDICTION_API = "http://172.104.78.30/predict" + +# cloudinary media storage +DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 9fcf048..d10ad00 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,6 +18,7 @@ Click==7.0 cryptography==2.5 dj-database-url==0.5.0 Django==2.1.5 +django-cloudinary-storage==0.2.3 docutils==0.14 first==2.0.1 Flask==1.0.2