-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdreader.py
145 lines (119 loc) · 5.09 KB
/
mdreader.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
137
138
139
140
141
142
143
144
145
import json
import os
import pika
import unittest
import uuid
# Данные для сверки значений тегов для аудиофайлов 440_hz_mono.*:
# mp3 flac wv dsf значение
# - title + + + test_album_title
# - work
# - composer + + + test_composer
# - recording
# - performer + + + test_performer
# - genres + + + test_genre
# - totaltracks + + + 10
# - tracks
# - position + + + 3
# - title + + + test_track_title
# - artist + + + test_track_artist
# - country + + + test_country
# - label + + + test_label
# - catno + + + test_catno
# - year + + + 2000
# - notes + + + test_notes
# - rutracker, DISCOGS_RELEASE_ID + + + 123456789
# - cover + + +
class RPCClient(object):
def __init__(self, rpc_queue):
self.rpc_queue = rpc_queue
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
self.channel = self.connection.channel()
result = self.channel.queue_declare(queue='', exclusive=True)
self.callback_queue = result.method.queue
self.channel.basic_consume(
queue=self.callback_queue,
on_message_callback=self._on_response,
auto_ack=True)
def close(self):
self.channel.close()
self.connection.close()
def _on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id:
self.response = body
def call(self, payload):
self.response = None
self.corr_id = str(uuid.uuid4())
self.channel.basic_publish(
exchange='',
routing_key=self.rpc_queue,
properties=pika.BasicProperties(
reply_to=self.callback_queue,
correlation_id=self.corr_id,
),
body=json.dumps(payload))
while self.response is None:
self.connection.process_data_events()
return self.response
def ping(self):
return self.call({"cmd": "ping", "params": {}})
class AudioMetadataReader(RPCClient):
def __init__(self):
super().__init__('mdreader')
def release(self, dir):
return self.call({"cmd": "release", "path": dir})
class TestMetadataReader(unittest.TestCase):
def setUp(self):
self.r = AudioMetadataReader()
def tearDown(self):
self.r.close()
def test_ping(self):
self.assertEqual(self.r.ping(), b'')
def release(self, ext):
dir = os.path.abspath(f"file/testdata/{ext}")
self.assertTrue(os.path.exists(dir))
resp = json.loads(self.r.release(dir))
if resp.get("error"):
raise BaseException(resp)
else:
return resp
def check_resp(self, resp):
r = resp["suggestions"][0]["release"]
t = resp["suggestions"][0]["release"]["tracks"][0]
self.assertEqual(r["title"], "test_album_title")
self.assertEqual(list(r["actors"].keys())[0], "test_performer")
self.assertEqual(r["discs"][0]["number"], 1)
self.assertEqual(r["total_tracks"], 10)
self.assertEqual(r["tracks"][0]["position"], "03")
self.assertEqual(list(t["composition"]["actor_roles"].keys())[0], "test_composer")
self.assertEqual(list(t["record"]["actors"].keys())[0], "test_track_artist")
self.assertEqual(t["record"]["genres"][0], "test_genre")
self.assertEqual(t["title"], "test_track_title")
basename, ext = (os.path.splitext(t["file_info"]["file_name"]))
if ext not in (".mp3", ".wv"): # TODO
self.assertEqual(t["duration"], 500)
self.assertEqual(basename, "440_hz_mono")
if ext not in (".dsf", ".wv"): # TODO
self.assertEqual(t["audio_info"]["samplerate"], 44100)
self.assertEqual(t["audio_info"]["sample_size"], 16)
if ext != ".wv": # TODO
self.assertEqual(t["audio_info"]["channels"], 1)
self.assertEqual(
r["pictures"][0]["pict_meta"]["mime_type"],
"image/jpeg")
self.assertEqual(r["pictures"][0]["pict_type"], "cover_front")
self.assertEqual(r["publishing"][0]["name"], "test_label")
self.assertEqual(r["publishing"][0]["catno"], "test_catno")
self.assertEqual(r["country"], "test_country")
self.assertEqual(r["year"], 2000)
self.assertEqual(r["notes"], "test_notes")
def test_mp3(self):
self.check_resp(self.release("mp3"))
def test_flac(self):
self.check_resp(self.release("flac"))
def test_dsf(self):
self.check_resp(self.release("dsf"))
def test_wavpack(self):
self.check_resp(self.release("wavpack"))
if __name__ == '__main__':
unittest.main()