Skip to content

Commit

Permalink
fix media file hosting with cloudinary
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Jul 19, 2019
1 parent 332aa92 commit d480888
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
11 changes: 3 additions & 8 deletions ImageQ/processor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 <prediction_api> is of type None")
return r.content
Expand Down
8 changes: 7 additions & 1 deletion ImageQ/processor/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={}',

}
}

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"
}
1 change: 1 addition & 0 deletions ImageQ/search/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
3 changes: 3 additions & 0 deletions ImageQ/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 35,8 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'cloudinary_storage',
'cloudinary',
'ImageQ.search',
]

Expand Down Expand Up @@ -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'),
Expand Down
5 changes: 4 additions & 1 deletion ImageQ/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 19,7 @@
'default': dj_database_url.parse(DATABASE_URL)
}

PREDICTION_API = "172.104.78.30/predict"
PREDICTION_API = "http://172.104.78.30/predict"

# cloudinary media storage
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d480888

Please sign in to comment.