Skip to content

Commit

Permalink
Merge pull request #35 from notAI-tech/develop
Browse files Browse the repository at this point in the history
Release v0.6.1
  • Loading branch information
preetham committed Aug 31, 2020
2 parents ee78038 + 31712bb commit 7469c79
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ Verify Tweet is released under GNU Affero General Public License v3.0.

## Future features

- [ ] Support for Image links
- [x] Support for Image links
- [ ] Support for Tweets with replies
62 changes: 0 additions & 62 deletions requirements-dev.txt

This file was deleted.

20 changes: 13 additions & 7 deletions verifytweet/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,20 @@ def verify_tweet():
"""
logger.info('Received data for processing...')

data_type = request.form['type']
request_image = request.files['data']
if not data_type or not request_image:
return "Missing form fields", 400
try:
file_path = image_uploader.save_to_disk(request_image)
rest_controller = controller.NonAPIApproach()
result, controller_status = rest_controller.exec(file_path)
data_type = request.form['type']
if not data_type:
return "Missing type", 400
if data_type == 'image':
request_image = request.files['data']
if not request_image:
return "Missing form fields", 400
file_path = image_uploader.save_to_disk(request_image)
if data_type == 'link':
request_link = request.form['url']
file_path = image_uploader.save_from_url(request_link)
rest_controller = controller.NonAPIApproach()
result, controller_status = rest_controller.exec(file_path)
except Exception as e:
logger.exception(e)
return jsonify({
Expand Down
5 changes: 4 additions & 1 deletion verifytweet/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def no_of_workers():
twice the cpu count.
"""
if os.getenv('NUM_WORKERS') and os.getenv('NUM_WORKERS').isdigit():
return int(os.getenv('NUM_WORKERS'))

return multiprocessing.cpu_count() * 2 + 1


Expand All @@ -39,7 +42,7 @@ class Config(object):

IMAGEMAGICK_PATH = os.getenv('IMAGEMAGICK_PATH') if os.getenv(
'IMAGEMAGICK_PATH') else "convert"
FILE_DIRECTORY = tempfile.mkdtemp()
FILE_DIRECTORY = os.getenv('FILE_DIRECTORY') if os.getenv('FILE_DIRECTORY') else tempfile.mkdtemp()
TWEET_MAX_STORE = 150
RUN_METHOD = "cli"
LOG_LEVEL = logging.DEBUG if os.getenv('VERIFYTWEET_DEBUG') else logging.INFO
Expand Down
23 changes: 23 additions & 0 deletions verifytweet/util/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import os
import uuid
import shutil

import requests

from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
Expand Down Expand Up @@ -46,6 +49,26 @@ def save_to_disk(file_obj):
return saved_file_path
return None

def save_from_url(image_url: str):
"""Saves image given via url to disk
"""
if not isinstance(image_url, str):
raise TypeError('image_url must be a string')
if not image_url:
raise ValueError('image_url has to be a valid string')
r = requests.get(image_url, stream=True, allow_redirects=True)
if r.status_code != 200:
raise FileNotFoundError()
filename = image_url.split("/")[-1]
if filename and allowed_file(filename):
saved_file_path = os.path.join(app_config.FILE_DIRECTORY, filename)
logger.info('Saving file to path: ' + saved_file_path)
r.raw.decode_content = True
with open(saved_file_path, 'wb') as f:
shutil.copyfileobj(r.raw, f)
logger.info('Image successfully downloaded')
return saved_file_path

def allowed_file(filename):
"""Checks if uploaded file has valid extenstion
"""
Expand Down

0 comments on commit 7469c79

Please sign in to comment.