-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
139 lines (111 loc) · 4.41 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from flask import Flask, abort, make_response, jsonify, request, render_template
import json
import redis
import os
app = Flask(__name__)
@app.route("/")
def swagger_ui():
return render_template('swaggerui.html')
@app.route('/hashtag', methods=['POST'])
def add_hashtag():
data = json.loads(request.data)
hashtag = data.get("value", None)
if "#" in str(hashtag):
abort(make_response(jsonify(message="Use a word without '#'. Application will take care of that."), 400))
else:
try:
if 'DB_PASS' not in os.environ:
print("'DB_PASS' environment variable does not exist")
exit(1)
elif 'DB_HOST' not in os.environ:
print("'DB_HOST' environment variable does not exist")
exit(1)
elif 'DB_PORT' not in os.environ:
print("'DB_PORT' environment variable does not exist")
exit(1)
else:
db_pass = os.getenv('DB_PASS')
db_host = os.getenv('DB_HOST')
db_port = os.getenv('DB_PORT')
r = redis.StrictRedis(
host=db_host,
port=db_port,
password=db_pass,
charset="utf-8",
decode_responses=True)
r.sadd('hashtags', hashtag)
return "Database updated with hashtag: " + str(hashtag)
except ConnectionError:
abort(make_response(jsonify(message="Unable to connect to the database!"), 502))
except redis.exceptions.ResponseError as ex:
error_message = "Error: " + str(ex)
abort(make_response(jsonify(message=error_message), 500))
@app.route('/tweets/<string:hashtag>', methods=['GET'])
def get_tweets_with_hashtag(hashtag):
try:
if 'DB_PASS' not in os.environ:
print("'DB_PASS' environment variable does not exist")
exit(1)
elif 'DB_HOST' not in os.environ:
print("'DB_HOST' environment variable does not exist")
exit(1)
elif 'DB_PORT' not in os.environ:
print("'DB_PORT' environment variable does not exist")
exit(1)
else:
db_pass = os.getenv('DB_PASS')
db_host = os.getenv('DB_HOST')
db_port = os.getenv('DB_PORT')
r = redis.StrictRedis(
host=db_host,
port=db_port,
password=db_pass,
charset="utf-8",
decode_responses=True)
tweets_data = (r.lrange(hashtag, 0, 9))
if not tweets_data:
abort(make_response(jsonify(message="Database does not contain tweets with this hashtag."), 400))
else:
return jsonify(tweets_data)
except ConnectionError:
abort(make_response(jsonify(message="Unable to connect to the database!"), 502))
except redis.exceptions.ResponseError as ex:
error_message = "Error: " + str(ex)
abort(make_response(jsonify(message=error_message), 500))
@app.route('/vote', methods=['POST'])
def vote_tweet_id():
data = json.loads(request.data)
tweet_id = data.get("value", None)
try:
if 'DB_PASS' not in os.environ:
print("'DB_PASS' environment variable does not exist")
exit(1)
elif 'DB_HOST' not in os.environ:
print("'DB_HOST' environment variable does not exist")
exit(1)
elif 'DB_PORT' not in os.environ:
print("'DB_PORT' environment variable does not exist")
exit(1)
else:
db_pass = os.getenv('DB_PASS')
db_host = os.getenv('DB_HOST')
db_port = os.getenv('DB_PORT')
r = redis.StrictRedis(
host=db_host,
port=db_port,
password=db_pass,
charset="utf-8",
decode_responses=True)
r.incr(tweet_id, 1)
votes = r.get(tweet_id)
if int(votes) > 1:
return "Tweet with id: " + tweet_id + " has " + votes + " votes."
else:
return "Tweet with id: " + tweet_id + " has " + votes + " vote."
except ConnectionError:
abort(make_response(jsonify(message="Unable to connect to the database!"), 502))
except redis.exceptions.ResponseError as ex:
error_message = "Error: " + str(ex)
abort(make_response(jsonify(message=error_message), 500))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)