Last active
August 29, 2015 14:20
-
-
Save moritzmhmk/68c08a7ce37d1257fcd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import base64 | |
| import math | |
| import threading | |
| import argparse | |
| metadata = {} | |
| lock = threading.Lock() | |
| def update_metadata(type, code, length, data): | |
| global metadata | |
| if code == "mdst" and length == 0: | |
| lock.acquire() | |
| metadata = {} | |
| if code == "mden" and length == 0: | |
| lock.release() | |
| print(metadata) | |
| if type == "core": | |
| if code == "asal": | |
| metadata["album"] = data | |
| if code == "asar": | |
| metadata["artist"] = data | |
| if code == "minm": | |
| metadata["title"] = data | |
| xml_pattern = re.compile("<type>(.*)<\/type><code>(.*)<\/code><length>(\d*)<\/length>(?:<data encoding=\"(.*)\">(.*)<\/data>){0,1}") | |
| def parse_metadata(xml): | |
| m = xml_pattern.match(xml) | |
| type = m.group(1) | |
| type = bytes.fromhex(type).decode('ascii') | |
| code = m.group(2) | |
| code = bytes.fromhex(code).decode('ascii') | |
| length = int(m.group(3)) | |
| encoding = m.group(4) | |
| data = m.group(5) | |
| if encoding == "base64": | |
| data = base64.b64decode(data) | |
| update_metadata(type, code, length, data) | |
| def read_fifo(path): | |
| fifo = open(path, "r") | |
| info_tag_pattern = re.compile("<type>(.*)<\/type><code>(.*)<\/code><length>(\d*)<\/length>") | |
| line_group = next(fifo).rstrip('\r\n') | |
| while True: | |
| line = next(fifo).rstrip('\r\n') | |
| info_tag = info_tag_pattern.match(line) | |
| if not info_tag: | |
| line_group += line | |
| else: | |
| parse_metadata(line_group) | |
| line_group = line | |
| fifo.close() | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description='Process metadata from shairport-sync.') | |
| parser.add_argument('fifo', metavar='f', type=str, help='path of the metadata fifo file') | |
| args = parser.parse_args() | |
| read_fifo(args.fifo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment