-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
178 lines (129 loc) · 4.16 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from flask import Flask, render_template, request, send_from_directory, redirect
from db.operations import Operations
from notifier.crawler import Crawler
from notifier.email import Email
from notifier.preparations import Preparations
from db.tv_series import TvSeries
from celery import Celery
from util.config import config
prep = Preparations()
cr = Crawler()
em = Email()
op = Operations()
app = Flask(__name__)
app.secret_key = 'random string'
host = config['redis'].get('host')
port = config['redis'].get('port')
db = config['redis'].get('db')
app.config['CELERY_BROKER_URL'] = f'redis://{host}:{port}/{db}'
app.config['CELERY_RESULT_BACKEND'] = f'redis://{host}:{port}/{db}'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
@app.route('/css/<path:path>')
def send_css(path):
return send_from_directory('static/css', path)
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('static/js', path)
@app.route('/images/<path:path>')
def send_images(path):
return send_from_directory('static/images', path)
@celery.task()
def update_imdb():
df = prep.get_imdb_data()
op.insert(df=df, table='imdb_dataset', method='replace')
@celery.task()
def send_mail(mail_name):
df = op.get_dataframe()
message = cr.crawl_airtimes(df=df)
em.send_email(mail=mail_name, message=message)
@app.route('/')
def home():
update_imdb.apply_async()
return render_template('index.html')
@app.route('/add_record', methods=['POST'])
def add_record():
"""
Adding new record to tv_series
:return: tv series list
"""
try:
name = request.form['name']
season = request.form['season']
episode = request.form['episode']
df = prep.merge_entries(name, season, episode)
op.insert(df, table='tv_series', method='append')
return redirect('/list')
except Exception as e:
return render_template('index.html', error=str(e))
@app.route('/list')
def show_list():
"""
Tv show list page
:return: tv series list
"""
try:
rows = op.get_list()
return render_template('list.html', rows=rows)
except Exception as e:
return render_template('list.html', error=str(e))
@app.route('/list/edit', methods=['GET'])
def get_list_edit():
"""
Response for get request to history edit
:return: return history if user logged in ago, else return sign in page
:rtype: str(render_template) or werkzeug.wrappers.Response(redirect)
"""
try:
imdb_id = request.args['imdb_id']
res = op.get_by_id(imdb_id=imdb_id)
return render_template('edit.html', item=res, title='List', active=2)
except Exception as e:
return render_template('list.html', error=str(e))
@app.route('/edit', methods=['POST'])
def edit():
"""
Edit operation in database
:return: status of edit operation
:rtype: str
"""
imdb_id = str(request.form['imdb_id'])
try:
season = request.form['season']
episode = request.form['episode']
record = TvSeries(imdb_id=str(imdb_id), season=season, episode=episode)
op.update(record)
return redirect('/list')
except Exception as e:
item = op.get_by_id(imdb_id)
return render_template('edit.html', error=str(e), active=2, title='Edit', item=item)
@app.route('/delete', methods=['GET'])
def delete():
"""
Delete operation in database
:return: status of delete operation
:rtype: str
"""
try:
imdb_id = str(request.args['imdb_id'])
op.delete_by_id(imdb_id)
return redirect('/list')
except Exception as e:
return render_template('list.html', error=str(e))
@app.route('/send')
def enter_mail():
return render_template('mail.html')
@app.route('/sendmail', methods=['POST'])
def mail():
"""
Crawls airtimes and sends mail in the background
:return: thank you page
"""
mail_name = request.form['mail']
send_mail.apply_async([mail_name])
return redirect('/done')
@app.route('/done')
def final_page():
return render_template('done.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)