-
Notifications
You must be signed in to change notification settings - Fork 0
/
onigiri.py
executable file
·108 lines (79 loc) · 3.18 KB
/
onigiri.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import logging
import logging.config
import configparser
import urllib.request
import urllib.parse
import json
import time
CONFIG_FILE = os.path.dirname(os.path.abspath(__file__)) + '/onigiri.config'
TWITCASTING_API_LIVE_STATUS = 'http://api.twitcasting.tv/api/livestatus'
PARSE_API_PUSH = 'https://api.parse.com/1/push'
POLLING_INTERVAL = 3
# DEBUG_FORCE_PUSH = True
DEBUG_FORCE_PUSH = False
class OnigiriAlert(object):
# magic methods
def __init__(self, user, parse_application_id, parse_rest_api_key):
self.user = user
self.parse_application_id = parse_application_id
self.parse_rest_api_key = parse_rest_api_key
# public methods
def listen(self):
logging.debug(u'OnigiriAlert.listen() started.')
url = TWITCASTING_API_LIVE_STATUS + '?type=json&user=' + self.user
last_is_live = False
while True:
try:
request = urllib.request.urlopen(url)
encoding = request.headers.get_content_charset()
response = request.read().decode(encoding)
# logging.debug(response)
parsed = json.loads(response)
logging.debug(parsed)
is_live = parsed["islive"]
if last_is_live is False and is_live is True or DEBUG_FORCE_PUSH:
self.notify(parsed)
if DEBUG_FORCE_PUSH:
os.sys.exit()
last_is_live = is_live
# raise Exception('test exception')
except Exception as error:
logging.error("caught exception in polling loop, error: [{}]".format(error))
# os.sys.exit()
time.sleep(POLLING_INTERVAL)
logging.debug(u'OnigiriAlert.listen() ended.')
def notify(self, parsed):
message = parsed["title"] + ' ' + parsed["subtitle"]
headers = {'X-Parse-Application-Id': self.parse_application_id,
'X-Parse-REST-API-Key': self.parse_rest_api_key,
'Content-Type': 'application/json'}
parameters = {'where': {'deviceType': 'ios'},
'data': {'alert': message}}
dumped_parameters = json.dumps(parameters).encode('utf-8')
request = urllib.request.Request(PARSE_API_PUSH, dumped_parameters, headers)
response = urllib.request.urlopen(request).read()
logging.debug(response)
def main():
config_file = CONFIG_FILE
if len(sys.argv) == 2:
config_file = sys.argv[1]
logging.config.fileConfig(config_file)
config = configparser.ConfigParser()
config.read(config_file)
section = 'application'
user = config[section]['user']
section = 'parse'
parse_application_id = config[section]['application_id']
parse_rest_api_key = config[section]['rest_api_key']
logging.debug("user:{} parse_application_id:{} parse_rest_api_key:{}".format(
user, parse_application_id, parse_rest_api_key))
logging.info(u'started.')
onigiri = OnigiriAlert(user, parse_application_id, parse_rest_api_key)
onigiri.listen()
logging.info(u'ended.')
if __name__ == "__main__":
main()