-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·68 lines (54 loc) · 2.05 KB
/
app.py
File metadata and controls
executable file
·68 lines (54 loc) · 2.05 KB
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
#!/usr/bin/env python
import atexit
import pickle
from datetime import datetime
from pytz import timezone
from apscheduler.schedulers.background import BackgroundScheduler
from decouple import config
from flask import Flask, request, redirect, render_template
from forms import ReminderForm
import phonenumbers
from phonenumbers import timezone as ptz
from job import send_message
from utils import get_natural_timesince
app = Flask(__name__)
# Debug set to False in development
app.debug = config('DEBUG', default=False, cast=bool)
@app.route('/', methods=['GET', 'POST'])
def index():
form = ReminderForm(request.form)
if request.method == 'POST' and form.validate():
with open('db.bin', 'wb') as f:
# getting timezone with phone number
raw_phone_number = request.form['phone']
phone_number = phonenumbers.parse(raw_phone_number)
time_zone = ptz.time_zones_for_number(phone_number)[0]
pickle.dump({
'phone': request.form['phone'],
'message': request.form['message'],
'timestamp': datetime.now(),
'time_zone': time_zone
}, f)
_time_zone = timezone(time_zone)
time_zone_time = datetime.now(_time_zone)
# timezone helps determining the night hours of user's country
scheduler = BackgroundScheduler()
scheduler.add_job(
send_message,
'cron',
hour='7-23',
second=time_zone_time.second,
minute=time_zone_time.minute,
id="sms_job_id",
timezone=time_zone)
# the above cronjob will run forever every hour from the time it called
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown(wait=False))
return redirect('/info')
return render_template('home.html', form=form)
@app.route('/info')
def info():
return render_template('info.html', timesince=get_natural_timesince())
if __name__ == "__main__":
app.run()