-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowser_messenger.py
148 lines (116 loc) · 4.36 KB
/
browser_messenger.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import asyncio
import datetime
import json
import logging
import queue
import threading
import websockets
class BrowserMessenger():
def __init__(self, host, port):
self._host = host
self._port = port
self._log_queue = queue.Queue()
def _run_loop(self):
"""Runs the event loop for the websocket
"""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self._server = websockets.serve(self._log_message, self._host, self._port)
loop.run_until_complete(self._server)
loop.run_forever()
def run_in_background(self):
"""Runs the messenger in background
"""
threading.Thread(target=self._run_loop).start()
def _get_time(self):
"""
:returns: String, Local time
"""
curr_time = datetime.datetime.now()
return curr_time.strftime("%Y-%m-%d %H:%M:%S")
def song_found(self, searched_song, result, exact):
"""Log successful song find query to the browser
:param searched_song: Song object, The song that was searched
:param result: JSON Object, The JSON response with the song information
:param exact: Boolean, If the result has matching title, album AND artist
"""
message = {
"timestamp": self._get_time(),
"level": "INFO",
"type": "SONG_FOUND",
"searched_song": json.dumps(searched_song.to_dict()),
"result": json.dumps(result),
"exact": exact,
}
self._log_queue.put(json.dumps(message))
def song_not_found(self, song):
"""Log unsuccessful song search
:param song: Song object, The song that was searched
"""
message = {
"timestamp": self._get_time(),
"level": "ERROR",
"type": "SONG_NOT_FOUND",
"song": json.dumps(song.to_dict()),
}
self._log_queue.put(json.dumps(message))
def songs_added(self, songs, library=True, playlist=None):
"""Log successful song additions to user's spotify account
:param songs: Array of JSON Objects, Songs that were added to Spotify
:param library: Bool, True if songs were added to the library, False if added to a Playlist
:param playlist: JSON Object, Playlist information; ignored if library is True
"""
message = {
"timestamp": self._get_time(),
"level": "INFO",
"type": "SONGS_ADDED",
"songs": songs,
}
if library:
message["library"] = library
else:
message["playlist"] = playlist
self._log_queue.put(json.dumps(message))
def songs_add_failed(self, songs, library=True, playlist=None):
"""Log unsuccessful song additions to user's spotify account
:param songs: Array of JSON Objects, Songs that failed to be added to Spotify
:param library: Bool, True if songs were to be added to the library, False if to be added to a Playlist
:param playlist: JSON Object, Playlist information; ignored if library is True
"""
message = {
"timestamp": self._get_time(),
"level": "ERROR",
"type": "SONGS_ADD_FAILED",
"songs": songs,
}
if library:
message["library"] = library
else:
message["playlist"] = playlist
self._log_queue.put(json.dumps(message))
def playlist_created(self, playlist):
"""Log successful creation of a playlist
:param name: JSON Object, Complete info about the playlist
"""
message = {
"timestamp": self._get_time(),
"level": "INFO",
"type": "PLAYLIST_CREATED",
"playlist": json.dumps(playlist),
}
self._log_queue.put(json.dumps(message))
def playlist_create_failed(self, name):
"""Log unsuccessful attempt at creating a playlist
:param name: Name of the playlist
"""
message = {
"timestamp": self._get_time(),
"level": "ERROR",
"type": "PLAYLIST_CREATE_FAILED",
"name": name,
}
self._log_queue.put(json.dumps(message))
async def _log_message(self, socket, request_uri):
while True:
record = self._log_queue.get()
await socket.send(record)