forked from karan/slack-overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
70 lines (48 loc) · 1.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
import os
from flask import Flask, request, Response, redirect
from stackexchange import Site, StackOverflow, Sort, DESC
try:
import config
se_key = config.stackexchange['api_key']
except:
se_key = os.environ.get('SE_KEY')
if not se_key:
import sys
print 'No config.py file found. Exiting...'
sys.exit(0)
MAX_QUESTIONS = 5
app = Flask(__name__)
so = Site(StackOverflow, se_key)
def get_response_string(q):
q_data = q.json
check = ' :white_check_mark:' if q.json['is_answered'] else ''
return "|%d|%s <%s|%s> (%d answers)" % (q_data['score'], check, q.url,
q.title, q_data['answer_count'])
@app.route('/overflow', methods=['post'])
def overflow():
'''
Example:
/overflow python list comprehension
'''
text = request.values.get('text')
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[:MAX_QUESTIONS]))
if len(resp_qs) is 1:
resp_qs.append(('No questions found. Please try a broader search or '
'search directly on '
'<https://stackoverflow.com|StackOverflow>.'))
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')
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)