forked from skvoter/ymcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
81 lines (60 loc) · 1.78 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import requests
import json
import os
import sys
import tty
import termios
import contextlib
import time
from ctypes import CFUNCTYPE, c_char_p, c_int, cdll
TRACK_DOWNLOAD_INFO = ('https://storage.mds.yandex.net/download-info/{}/'
+ '2?format=json')
HANDLERS = {
'TRACK': 'https://music.yandex.ru/handlers/track.jsx?track={}',
'ALBUM': 'https://music.yandex.ru/handlers/album.jsx?album={}',
'ARTIST': 'https://music.yandex.ru/handlers/artist.jsx?artist={}',
}
class _Getch:
def __init__(self):
pass
def __call__(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
getch = _Getch()
def load_json(handler, id):
r = requests.get(handler.format(id))
return json.loads(r.content)
@contextlib.contextmanager
def ignore_stdout():
devnull = os.open(os.devnull, os.O_WRONLY)
old_stdout = os.dup(2)
sys.stderr.flush()
os.dup2(devnull, 2)
os.close(devnull)
try:
yield
finally:
os.dup2(old_stdout, 2)
os.close(old_stdout)
ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int,
c_char_p, c_int, c_char_p)
def py_error_handler(filename, line, function, err, fmt):
pass
c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)
@contextlib.contextmanager
def noalsaerr():
asound = cdll.LoadLibrary('libasound.so')
asound.snd_lib_error_set_handler(c_error_handler)
yield
asound.snd_lib_error_set_handler(None)
def quit(player):
player.state = 'stopped'
print('\nExiting...')
time.sleep(1)
player.current_song = None