-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
93 lines (76 loc) · 3.08 KB
/
console.py
File metadata and controls
93 lines (76 loc) · 3.08 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
import asyncio
import datetime
import heapq
import json
import climage
from aioconsole import aprint, ainput
from pydub import AudioSegment
from pydub.playback import play
from player import main as player_main
station_name = 'Tambaram'
station_code = 'TBM'
STATIONS_FILE = 'stations.json'
def fetch_station_name(station_code: str) -> str:
station_code = station_code.upper()
stations = json.load(open(STATIONS_FILE))
return stations.get(station_code, {'name': None})['name']
ANNOUNCEMENTS = []
TIME_BETWEEN_ANN_SEC = 70 # 1 minute 10 seconds or 70 seconds
async def async_console_captcha_resolver(sd: str, keys: list[str], error: str, file: str) -> str:
image = climage.convert(file, width=80, is_unicode=True, is_truecolor=True, is_256color=False)
await aprint(image)
key = await ainput(
f"{error}\nPossible keys are: {keys}\n> "
)
print(f"You selected: {key}")
return key.strip()
# Producer function
async def fetch_announcements():
while True:
print('Fetching announcements')
async for (ann_file, dept_time, priority) in player_main(station_name, station_code, captcha_resolver=async_console_captcha_resolver):
await aprint(f'Priority: {priority}, Dept Time: {dept_time}, Announcement: {ann_file}')
heapq.heappush(ANNOUNCEMENTS, (priority, dept_time, ann_file))
await asyncio.sleep(TIME_BETWEEN_ANN_SEC)
# Consumer function
async def play_announcements():
while True:
if len(ANNOUNCEMENTS) > 0:
priority, dept_time, ann_file = heapq.heappop(ANNOUNCEMENTS)
await aprint(f'Priority: {priority}, Dept Time: {dept_time}, Announcement: {ann_file}')
current_time = datetime.datetime.now()
if dept_time is None:
dept_time = current_time
if dept_time < current_time:
print(f"Announcement time is in the past, skipping. {dept_time} < {current_time} - {dept_time - current_time:.2f} seconds; {ann_file}")
continue
ann_seg: AudioSegment = AudioSegment.from_file(ann_file)
try:
task = asyncio.get_event_loop().run_in_executor(None, play, ann_seg)
while True:
await asyncio.sleep(0.5)
if task.done():
break
except KeyboardInterrupt:
task.cancel()
print("Playback interrupted")
break
else:
await asyncio.sleep(1)
async def main():
global station_name, station_code
station_code = (str(await ainput('Select station:> '))).upper().strip()
station_name = fetch_station_name(station_code)
if station_name is None:
await aprint("Station not found")
return
await aprint("Station selected: " + station_name)
fetch = asyncio.create_task(fetch_announcements())
play_ann = asyncio.create_task(play_announcements())
await asyncio.gather(fetch, play_ann)
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
print("Exiting...")
exit(0)