-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
64 lines (52 loc) · 1.84 KB
/
utils.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
# -*- coding: utf-8 -*-
# SourceSeeker – utilites
# Author: LulzLoL231
from logging import getLogger, getLevelName, basicConfig, INFO, DEBUG
from a2s.info import ainfo, SourceInfo
from typing import Union
from config import Config
def log(msg: str, level: str = 'INFO', func: Union[str, None] = None) -> None:
'''log: make log message.
Args:
msg (str): Log message.
level (str): Log level. Defaults to 'INFO'.
func (Union[str, None]): Function name. Defaults to None.
'''
basicConfig(level=DEBUG if Config.DEBUG else INFO,
format='[%(asctime)s] %(levelname)s | %(name)s | %(message)s')
log = getLogger('SourceSeeker')
if func:
log = getLogger(f'SourceSeeker::{func}')
log.log(getLevelName(level.upper()), msg)
async def getServerInfo(address: tuple) -> Union[SourceInfo, None]:
'''getServerInfo: returns SourceInfo about SourceServer or None if connection unsuccessfull.
Args:
address (tuple): server ip & port in tuple.
Returns:
SourceInfo: Information about SourceServer.
'''
info = None
iter = 0
while True:
try:
info = await ainfo(address)
except Exception as e:
log('Error while trying get ServerInfo from '
f'{address[0]}:{address[1]} with error: {str(e)}',
'error', 'getServerInfo')
if iter >= 5:
return None
else:
iter += 1
continue
else:
break
return info
def getFastConnectURI(address: tuple) -> str:
'''getFastConnectURI: returns steam fast connect URI in format `steam://connect/IP:PORT`.
Args:
address (tuple): server ip & port in tuple.
Returns:
str: steam fast connect URI
'''
return f'steam://connect/{address[0]}/{address[1]}'