forked from callsmusic/vcpb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.py
More file actions
122 lines (98 loc) · 2.95 KB
/
player.py
File metadata and controls
122 lines (98 loc) · 2.95 KB
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
import os
import threading
import queue
from subprocess import Popen, PIPE
from helpers import run, State
q = queue.Queue()
currently_playing = {}
process = None
STATE = State.NothingSpecial
def worker():
global process, STATE, currently_playing
while True:
item = q.get()
currently_playing = item
log = None
if "stream_url" in item:
STATE = State.Streaming
if "log" in item:
if item["log"]:
log = run(item["log"])
process = Popen(["mplayer", "-novideo", item["stream_url"]], stdin=PIPE)
process.wait()
else:
if "on_start" in item:
if item["on_start"]:
run(item["on_start"], quote=True)
if "log" in item:
if item["log"]:
caption = item["log"]["kwargs"]["caption"]
caption = caption.format(
item["url"],
item["title"],
item["duration"],
item["sent_by_id"],
item["sent_by_name"],
)
log = run(item["log"], caption=caption)
STATE = State.Playing
process = Popen(["mplayer", "-novideo", item["file"]], stdin=PIPE)
process.wait()
if STATE == State.Playing:
if "on_end" in item:
if item["on_end"]:
run(item["on_end"], quote=True)
elif STATE == State.Skipped:
if "on_skip" in item:
if item["on_skip"]:
run(item["on_skip"], quote=True)
process = None
STATE = State.NothingSpecial
if log:
log.delete()
if q:
q.task_done()
threading.Thread(target=worker, daemon=True).start()
def play(
file,
title,
duration,
url,
sent_by_id,
sent_by_name,
log=None,
on_start=None,
on_end=None,
on_skip=None
) -> int:
q.put(
{
"file": file,
"on_start": on_start,
"on_end": on_end,
"title": title,
"url": url,
"sent_by_id": sent_by_id,
"sent_by_name": sent_by_name,
"log": log,
"duration": duration,
"on_skip": on_skip,
}
)
return q.qsize()
def stream(stream_url, log) -> int:
q.put({"stream_url": stream_url, "log": log})
return q.qsize()
def is_currently_playing() -> bool:
return STATE in (State.Playing, State.Paused)
def abort() -> bool:
if process:
process.terminate()
return True
return False
def pause_resume():
if process:
process.stdin.write(b"p")
process.stdin.flush()
return True
return False