forked from Ginoning/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_server.py
58 lines (43 loc) · 1.69 KB
/
push_server.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
import requests
import xmltodict
import time
import sys
import os
import django
from urllib.parse import urlencode
from pyfcm import FCMNotification
sys.path.append('D:\shelter_run')
os.environ['DJANGO_SETTINGS_MODULE'] = 'shelter_run.settings'
django.setup()
from app_receiver.models import User
def send_fcm(title, key):
push_service = FCMNotification(api_key=key)
registration_ids = [str(i) for i in User.objects.all() if i is not None]
message_title = title
message_body = "달리자대피소를 켜서 가까운 대피소로 달리세요!"
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title,
message_body=message_body)
print(result)
if __name__ == '__main__':
url = 'http://newsky2.kma.go.kr/service/WetherSpcnwsInfoService/WeatherWarningList?'
f = open('keys.txt')
param = {
'ServiceKey': f.readline()[:-1],
'stnId': '143',
'fromTmFc': '20170101'}
firebase_api_key = f.readline()
pre = [{'tmSeq': '0', 'title': 'FIRST'}]
while True:
param['toTmFc'] = time.strftime('%Y%m%d', time.localtime())
query_params = urlencode(param, safe='%')
r = requests.get(url + query_params)
current = xmltodict.parse(r.text)['response']['body']['items']['item']
for emergency in current:
print(emergency)
if int(emergency['tmSeq']) > int(pre[0]['tmSeq']) and '발표' in emergency['title']:
send_fcm(emergency['title'].split(' / ')[1].split('(')[0], firebase_api_key)
else:
break
pre = current
print()
time.sleep(60)