def post(self): # Write content to temporary file tempfile = os.path.join(TEMP_DIRECTORY, "temp.wav") with open(tempfile, "wb") as fp: fp.write(request.get_data()) # Analyze file with io.open(tempfile, 'rb') as fh: parser = TinyTag.get_parser_class(tempfile, fh) # Check if WAV file print("Tag Parser Name: {}".format(parser.__name__), file=sys.stderr) if (parser.__name__ == "Wave"): tag = TinyTag.get(TEMP_DIRECTORY + '/temp.wav') fid = uuid.uuid1() else: return { 'message': 'Sound not registered. Not a WAVE file.', 'filetype': parser.__name__ }, 400 if (tag.filesize < 12): return { 'message': 'Sound not registered. Empty WAVE file.', 'size': tag.filesize }, 400 # Save file print("Tag Title: {}".format(tag.title), file=sys.stderr) print("Filename: {}".format(UPLOAD_DIRECTORY + fid.hex + tag.title + ".wav")) filename = os.path.join(UPLOAD_DIRECTORY, fid.hex + tag.title + ".wav") with open(filename, "wb") as fp: fp.write(request.get_data()) # Store in database shelf = get_db() shelf[fid.hex] = { 'id': fid.hex, 'name': tag.title + ".wav", 'duration': tag.duration, 'size': tag.filesize } return { 'message': 'Sound registered', 'id': fid.hex, 'name': tag.title + ".wav", 'size': tag.filesize, 'duration': tag.duration }, 201
def test_detect_magic_headers(testfile, expected): filename = os.path.join(testfolder, testfile) with io.open(filename, 'rb') as fh: parser = TinyTag.get_parser_class(filename, fh) assert parser == expected
import markdown import os import io import shelve import werkzeug from tinytag import TinyTag from pprint import pprint tempfile = os.path.join("myfile.wav") # Analyze file with io.open(tempfile, 'rb') as fh: parser = TinyTag.get_parser_class(tempfile, fh) # pprint((parser)) # pprint(vars(parser)) # pprint(vars(vars(parser)['_parse_tag'])) # pprint(dir(vars(parser)['_parse_tag'])) # pprint(type(vars(parser)['_parse_tag'])) pprint(id(vars(parser)['_parse_tag'])) # pprint(id(vars(parser)['_parse_tag'])) # pprint(getattr(vars(parser)['_parse_tag'], "Wave")) pprint((parser).__name__) # pprint(getattr((parser), "tinytag.tinytag.Wave")) # print("Parser: {}" .format(parser)) # print("Vars: {}" .format(vars(parser))) # print("Type: {}" .format(type(parser))) # print("Dir: {}" .format(dir(parser))) # print("Vars(_parse_tag): {}" .format((vars(parser))['_parse_tag']))