diff --git a/app.py b/app.py index fd72296..1bfedfc 100644 --- a/app.py +++ b/app.py @@ -18,7 +18,7 @@ sys.exit(0) -MAX_QUESTIONS = 5 +MAX_QUESTIONS = 5 # Default number of questions app = Flask(__name__) @@ -60,6 +60,38 @@ def overflow(): content_type='text/plain; charset=utf-8') +@app.route('/overflow_dynamic', methods=['post']) +def overflow_dynamic(): + data = request.get_json() + text = data.get('text') + num_questions = data.get('num_questions', MAX_QUESTIONS) + + try: + num_questions = int(num_questions) + if num_questions <= 0: + raise ValueError("Number of questions must be greater than zero") + except ValueError: + return Response('Invalid number of questions. Must be a positive integer.', + content_type='text/plain; charset=utf-8') + + try: + qs = so.search(intitle=text, sort=Sort.Votes, order=DESC) + except UnicodeEncodeError: + return Response(('Only English language is supported. ' + '%s is not valid input.' % text), + content_type='text/plain; charset=utf-8') + + resp_qs = ['Stack Overflow Top Questions for "%s"\n' % text] + resp_qs.extend(map(get_response_string, qs[:num_questions])) + + if len(resp_qs) == 1: + resp_qs.append(('No questions found. Please try a broader search or ' + 'search directly on ' + '.')) + + return Response('\n'.join(resp_qs), + content_type='text/plain; charset=utf-8') + @app.route('/') def hello(): return redirect('https://github.com/karan/slack-overflow') diff --git a/config.py.example b/config.py.example deleted file mode 100644 index 3f13309..0000000 --- a/config.py.example +++ /dev/null @@ -1,3 +0,0 @@ -stackexchange = dict( - api_key = '' -)