/
stream_identify.py
136 lines (109 loc) · 4.19 KB
/
stream_identify.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
workspace = "/home/monitor/Workspace/"
import sys
sys.path.insert(0, workspace+"echoprint-server/API")
import MySQLdb
import os
import subprocess32
import time
from glob import glob
import fp
import traceback
codegen_path = os.path.abspath(workspace + "echoprint-codegen/echoprint-codegen")
import simplejson as json
import simplejson.scanner
def codegen(file, start=0, duration=40):
proclist = [codegen_path, os.path.abspath(file), "%d" % start, "%d" % duration]
p = subprocess32.Popen(proclist, stdout=subprocess32.PIPE)
r = p.communicate()
try:
code = json.loads(r[0])
except simplejson.scanner.JSONDecodeError:
logfile.write(getNowDateTime()+":Json cannot be decoded "+str(r[0])+"\n")
return None
return code
def process_file(filename):
codes = codegen(filename)
if codes is None:
return -2
if len(codes) == 0:
logfile.write(getNowDateTime()+":Codegen returned empty list\n")
return -3
if "code" not in codes[0]:
logfile.write(getNowDateTime()+":No code is returned by codegen\n")
return -4
db = conn.cursor()
track_id = None
decoded = fp.decode_code_string(codes[0]["code"])
result = fp.best_match_for_query(decoded)
if result.TRID and result.TRID not in ignored_songs:
#Melody is recognized
track_id = result.TRID
global last
#Insert tracks only once
if (last == 0) or (last != track_id):
last=track_id
try:
logfile.write(getNowDateTime()+":Inserting track_id " + track_id + '\n')
db.execute("""INSERT INTO played_melody(track_id,radio,date_played,time_played,radio_id) VALUES (%s,%s,%s,%s,%s)""",(track_id,radio,getNowDate(),getNowTime(),radio_id))
conn.commit()
except db.Error, e:
logfile.write(getNowDateTime())
logfile.write(":Error %d: %s\n" % (e.args[0],e.args[1]))
conn.rollback()
else:
logfile.write(getNowDateTime()+":Track "+track_id+" is already recognized\n")
db.close()
return 0
elif radio_id not in ignored_fp_radios and result.TRID is None:
#Insert unknown fingerprints with status 'N'
logfile.write(getNowDateTime()+":No match found for the file, inserting unknown melody to fingerprint table\n")
db.execute("""INSERT INTO fingerprint(fp,radio,date_played,time_played,time_identified,status,radio_id,track_id) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)""",(decoded,radio,getNowDate(),getNowTime(),None,'N',radio_id,None))
conn.commit()
db.close()
return -1
def getNowTime():
return time.strftime('%H:%M:%S')
def getNowDate():
return time.strftime('%Y-%m-%d')
def getNowDateTime():
return time.strftime('%Y-%m-%d %H:%M:%S')
if __name__ == "__main__":
import urllib2
from datetime import datetime
from datetime import timedelta
if len(sys.argv) < 4:
print "Usage: python stream_identify.py radio radio_id stream"
exit()
ignored_songs =['TROWTHB14D0E92A254']
ignored_fp_radios = ['10','11','12','13']
last=0
radio = sys.argv[1]
radio_id = sys.argv[2]
stream = sys.argv[3]
try:
url=urllib2.urlopen(stream)
logfile = open(workspace+"PyMusic/logs/radio"+radio+"LogStreamIdentify"+getNowDateTime(), 'w',1)
conn = MySQLdb.connect(host= "localhost",user="root", passwd="ulut123", db="pymusic",charset='utf8')
while True:
filename = workspace+"PyMusic/wavs/"+radio+getNowDateTime()+'.mp3'
f=file(filename, 'wb')
# Basically a timer
t_start = datetime.now()
t_end = datetime.now()
t_end_old = t_end
# Record in chunks until
while t_end-t_start < timedelta(seconds=40):
f.write(url.read(1024))
t_end = datetime.now()
f.close()
result = process_file(filename)
os.remove(filename)
except KeyboardInterrupt:
logfile.close()
conn.close()
url.close()
f.close()
exit()
except:
logfile.write(getNowDateTime()+":Unexpected error:" + str(traceback.format_exc()))
raise