9
9
10
10
log = logging .getLogger (__name__ )
11
11
12
+
12
13
class AsyncLoop :
13
14
def __init__ (self , timeout , callback ):
14
15
self ._timeout = timeout
@@ -23,11 +24,13 @@ async def _job(self):
23
24
def cancel (self ):
24
25
self ._task .cancel ()
25
26
27
+
26
28
def _ensure_coro (func ):
27
29
if not asyncio .iscoroutinefunction (func ):
28
30
func = asyncio .coroutine (func )
29
31
return func
30
32
33
+
31
34
class Poster (EventHandler ):
32
35
"""
33
36
A class that posts server count to listing sites.
@@ -66,9 +69,9 @@ class Poster(EventHandler):
66
69
67
70
def __init__ (
68
71
self , client_id , server_count , user_count ,
69
- voice_connections , on_custom_post = None , ** options
72
+ voice_connections , on_custom_post = None , ** options
70
73
):
71
- super ().__init__ (loop = options .pop ('loop' , None ))
74
+ super ().__init__ (loop = options .pop ('loop' , None ))
72
75
73
76
self ._loop = None
74
77
self ._client_id = client_id
@@ -81,7 +84,7 @@ def __init__(
81
84
82
85
proxy = options .pop ('proxy' , None )
83
86
proxy_auth = options .pop ('proxy_auth' , None )
84
- self .http = HTTPClient (proxy = proxy , proxy_auth = proxy_auth )
87
+ self .http = HTTPClient (proxy = proxy , proxy_auth = proxy_auth )
85
88
self .api_keys = options .pop ('api_keys' , {})
86
89
87
90
setattr (self , 'server_count' , _ensure_coro (server_count ))
@@ -98,26 +101,26 @@ def __repr__(self):
98
101
('shard_count' , self .shard_count ),
99
102
]
100
103
return '<%s %s>' % (self .__class__ .__name__ , ' ' .join ('%s=%r' % t for t in attrs ))
101
-
104
+
102
105
# property fill-ins
103
-
106
+
104
107
@property
105
108
def client_id (self ) -> str or None :
106
109
"""The client ID of the poster."""
107
110
return self ._client_id
108
-
111
+
109
112
@property
110
113
def shard_id (self ) -> int or None :
111
114
"""The shard ID of the poster."""
112
115
return self ._shard_id
113
-
116
+
114
117
@property
115
118
def shard_count (self ) -> int or None :
116
119
"""The shard count of the poster."""
117
120
return self ._shard_count
118
121
119
122
# api key management
120
-
123
+
121
124
def set_key (self , service : str , key : str ) -> str :
122
125
"""
123
126
Sets an API key.
@@ -155,10 +158,10 @@ def remove_key(self, service: str) -> str:
155
158
key = self .api_keys [service ]
156
159
del self .api_keys [service ]
157
160
return key
158
-
161
+
159
162
# loop management
160
-
161
- def start_loop (self , interval = 1800 ) -> AsyncLoop :
163
+
164
+ def start_loop (self , interval = 1800 ) -> AsyncLoop :
162
165
"""
163
166
Creates a loop that posts to all services every `n` seconds.
164
167
@@ -171,14 +174,14 @@ def start_loop(self, interval = 1800) -> AsyncLoop:
171
174
self ._loop = AsyncLoop (interval , self .__on_loop )
172
175
log .debug ('Started loop %s' , interval )
173
176
return self ._loop
174
-
177
+
175
178
def kill_loop (self ):
176
179
"""Cancels the current posting loop."""
177
180
if self ._loop :
178
181
log .debug ('Ending loop' )
179
182
self ._loop .cancel ()
180
183
self ._loop = None
181
-
184
+
182
185
async def __on_loop (self ):
183
186
log .debug ('Loop ran' )
184
187
try :
@@ -193,7 +196,7 @@ async def __on_loop(self):
193
196
194
197
# post management
195
198
196
- async def post (self , service = None ) -> HTTPResponse :
199
+ async def post (self , service = None ) -> HTTPResponse :
197
200
"""
198
201
Posts the current clients server count to a service.
199
202
@@ -206,10 +209,10 @@ async def post(self, service = None) -> HTTPResponse:
206
209
users = await self .user_count ()
207
210
connections = await self .voice_connections ()
208
211
return await self .manual_post (servers , service , users , connections )
209
-
212
+
210
213
async def manual_post (
211
- self , server_count , service = None ,
212
- user_count = None , voice_connections = None
214
+ self , server_count , service = None ,
215
+ user_count = None , voice_connections = None
213
216
) -> HTTPResponse :
214
217
"""
215
218
Manually posts a server count to a service.
@@ -227,9 +230,9 @@ async def manual_post(
227
230
"""
228
231
if service == 'custom' and hasattr (self , 'on_custom_post' ):
229
232
return await self .on_custom_post (
230
- self , server_count = server_count ,
231
- user_count = user_count ,
232
- voice_connections = voice_connections
233
+ self , server_count = server_count ,
234
+ user_count = user_count ,
235
+ voice_connections = voice_connections
233
236
)
234
237
if len (self .api_keys ) == 0 :
235
238
raise APIKeyException ('No API Keys available' )
@@ -241,9 +244,9 @@ async def manual_post(
241
244
for key in keys :
242
245
try :
243
246
responses .append (await self .manual_post (
244
- server_count = server_count ,
245
- service = key , user_count = user_count ,
246
- voice_connections = voice_connections
247
+ server_count = server_count ,
248
+ service = key , user_count = user_count ,
249
+ voice_connections = voice_connections
247
250
))
248
251
except Exception as error :
249
252
responses .append (error )
@@ -270,6 +273,7 @@ async def manual_post(
270
273
self .dispatch ('post_fail' , error )
271
274
raise error
272
275
276
+
273
277
class ClientPoster (Poster ):
274
278
"""
275
279
A class that posts certain client values to listing sites.
@@ -317,15 +321,15 @@ def __repr__(self):
317
321
('sharding' , self ._sharding ),
318
322
]
319
323
return '<%s %s>' % (self .__class__ .__name__ , ' ' .join ('%s=%r' % t for t in attrs ))
320
-
324
+
321
325
@property
322
326
def client_id (self ) -> str or None :
323
327
return self .filler .client_id
324
-
328
+
325
329
@property
326
330
def shard_id (self ) -> int or None :
327
331
return self ._shard_id or self .filler .shard_id if self ._sharding else None
328
-
332
+
329
333
@property
330
334
def shard_count (self ) -> int or None :
331
335
return self ._shard_count or self .filler .shard_count if self ._sharding else None
0 commit comments