-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
71 lines (56 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
71
#!/usr/bin/env python
# -* coding: utf-8 -*
# create by cvno on 2018/11/4 12:54
import os
import json
import time
from flask import Flask
from threading import Timer
from datetime import datetime
from gvapi import Voice
app = Flask(__name__)
voice = Voice(os.environ.get('GV_USR'),os.environ.get('GV_PWD'))
to_number = os.environ.get('TO_NUMBER')
while not voice.status['init']: # wait...
time.sleep(3)
continue
@app.route('/sms/<int:number>/<content>', methods=['GET', 'POST'])
def sms(number, content):
# 发送 sms
res = voice.send_sms(number, content)
data = {'number': number,'content': content, 'res': res}
return json.dumps(data, ensure_ascii=False)
class Scheduler(object):
# loop
def __init__(self, sleep_time, function):
self.sleep_time = sleep_time
self.function = function
self._t = None
def start(self):
if self._t is None:
self._t = Timer(self.sleep_time, self._run)
self._t.start()
else:
raise Exception("this timer is already running")
def _run(self):
self.function()
self._t = Timer(self.sleep_time, self._run)
self._t.start()
def stop(self):
if self._t is not None:
self._t.cancel()
self._t = None
def task():
voice.send_sms(to_number,'{}'.format(datetime.now().strftime("%Y年%m月%d日, %H时%M分%S秒, 星期%w")))
if __name__ == '__main__':
try:
# start loop
scheduler = Scheduler(604800, task) # 每隔一周发送一次
scheduler.start()
ENV_API = os.environ.get('GVAPI_IS_DEV')
if ENV_API and json.loads(ENV_API): # dev run
app.run(host='0.0.0.0', debug=False)
exit(0)
app.run()
except KeyboardInterrupt as e:
print('Bye.')