-
Notifications
You must be signed in to change notification settings - Fork 0
/
nts-tracklist-pirate.py
executable file
·58 lines (47 loc) · 1.7 KB
/
nts-tracklist-pirate.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
#!/bin/python
import os, sys, time, json, ast
import requests, subprocess
from datetime import datetime
max_chunk_factor = 1024
stream_url = 'http://stream-relay-geo.ntslive.net/stream' # NTS Radio 1
running = True
# Returns timestamp as string
def get_ts():
dt = datetime.now()
return str(datetime.timestamp(dt))
if __name__ == '__main__':
print("nts-tracklist-pirate")
r = requests.get(stream_url, stream=True) # Connect to audio stream
outputf = 'nts-tracklist-pirate.txt'
fn = '/tmp/nts-stream-' + get_ts() + '.mp3'
file = open(fn, 'wb')
i = 0
last = None
with open(outputf, "r") as f:
last = f.readlines()[-1]
print("Recording...")
for block in r.iter_content(1024):
file.write(block)
i += 1024
if i >= (1024 * max_chunk_factor):
file.close()
print("Sending chunk for scan...")
data = ""
process = subprocess.Popen("songrec audio-file-to-recognized-song " + fn, shell=True, stdout=subprocess.PIPE)
for line in process.stdout:
data += line.decode('utf-8')
process.wait()
try:
json = json.loads(data)
mname = json['track']['title'] + " " + json['track']['subtitle']
print(mname)
if last is not None:
if last.strip() == mname.strip():
print("Match last request! Cancelling...")
break
with open(outputf, "a") as f:
f.write(mname+"\n")
except:
print('No data found on chunk.')
break
os.remove(fn)