-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
83 lines (73 loc) · 2.44 KB
/
service.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
from datetime import datetime
from plyer import notification
from time import sleep
from jnius import autoclass
import socket
import ssl
# Restarts the service as soon as the script ends.
PythonService = autoclass('org.kivy.android.PythonService')
PythonService.mService.setAutoRestartService(True)
# Storing temporary ids of messages in array. They are indexed as templates in a dictionary
msg_storage = []
msg_template = {0: '',
1: 'npc is watching you...',
2: 'hey...',
3: 'new update available!',
4: 'you have a new message',
5: 'Asyllion lives forever!',
6: 'your character returned to village',
7: 'resource farming completed',
8: 'level up!',
9: 'please open the game'
}
# Reads user id form secret for later communication
f = open("../secretkey.mon", 'a+')
f.seek(0)
content = f.read()
if len(content) < 1:
f.close()
user_id = ''
notify = False
else:
user_id = content[44:]
notify = True
f.close()
def server_connected():
global user_id, msg_storage
try:
_HOST = socket.gethostbyname('sample.com')
except socket.error:
return False
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
context = ssl.SSLContext()
try:
usr = context.wrap_socket(sock, server_hostname=_HOST)
except socket.error:
return False
try:
usr.settimeout(5)
usr.connect((_HOST, 5005))
usr.settimeout(None)
usr.send('your-token'.encode())
latest_version = usr.recv().decode()
# add this feature later
server_version = 111
if int(latest_version) > server_version:
print(server_version)
# login_status = '...new version is available to download!'
usr.send(str('notify' + user_id).encode())
msg_storage = [int(m) for m in (usr.recv(256).decode()).split()]
print(msg_storage)
return True
except socket.error:
return False
# Continuously performs connection to the server and obtains IDs of notifications to display for a user
while True:
# Adjust sleep depending on duration of your service script
sleep(300)
if notify:
if server_connected():
for msg_id in msg_storage:
if msg_id != 0:
notification.notify(title=msg_template[msg_id])
msg_storage.clear()