-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
82 lines (65 loc) · 2.16 KB
/
monitor.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
#!/usr/bin/env python3
# coding=utf-8
import argparse
import os
import tornado.ioloop
import tornado.gen
import tornado.web
import tornado.httpclient
import common
import json
watchlist = common.watchlist
"""
curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/streams/sc2rain
"""
existslist = []
@tornado.gen.coroutine
def start():
try:
_existslist = []
for i in watchlist:
try:
http_client = tornado.httpclient.AsyncHTTPClient()
r = yield http_client.fetch(
'https://api.twitch.tv/kraken/streams/'
+ i,
headers={'Accept': 'application/vnd.twitchtv.v3+json',
'User-Agent': 'Safari/537.36'})
bd = r.body
if json.loads(bd.decode('utf-8')).get('stream'):
_existslist.append(i)
except:
pass
global existslist
existslist = _existslist
except Exception as e:
raise e
finally:
pass
class MonitorHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
self.write(json.dumps(existslist))
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'-t', '--looptime', type=int, default=60, help='loop query time')
parser.add_argument(
'-p', '--port', type=int, default=9400, help='set the listened port')
parser.add_argument(
'-d', '--logdir', default='log', help='set the listened port')
parser.add_argument(
'-n', '--logname', default='monitor.log', help='set the listened port')
args = parser.parse_args()
if not os.path.isdir(args.logdir):
os.makedirs(args.logdir)
common.config_log(args.logdir, args.logname, 'DEBUG',
enable_stream_handler=True)
tornado.web.Application([('/monitor', MonitorHandler)]).listen(args.port)
start()
tornado.ioloop.PeriodicCallback(start, args.looptime*1000).start()
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()