aboutsummaryrefslogtreecommitdiffstats
path: root/backend/zmusic/picard/file.py
blob: 0e246eb8192179319723f86bb2a617abd89cb792 (plain) (blame)
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
import logging
import os
import hashlib

MIMETYPES = {
		"mp3": ["audio/mpeg", "audio/mp3", "audio/x-mp3", "audio/mpg", "audio/x-mpeg"],
		"wav": ["audio/wav"],
		"mpc": ["audio/x-musepack", "audio/x-mpc"],
		"ogg": ["audio/ogg", "audio/vorbis", "audio/x-vorbis", "application/ogg", "application/x-ogg"],
		"wma": ["audio/x-ms-wma", "video/x-ms-asf", "audio/x-wma"],
		"m4a": ["audio/mp4", "audio/x-m4a", "audio/mpeg4", "audio/aac"],
		"flac": ["audio/flac", "audio/x-flac", "application/x-flac"],
		"webm": ["audio/webm" ]
}

class File(dict, object):
	log = logging.getLogger()


	def __init__(self, filename):
		self["filename"] = filename
		self["filestat"] = os.stat(filename)
		self._load(filename)

	def _info(self, metadata, file):
		if "title" not in metadata or metadata["title"] is None or len(metadata["title"].strip()) == 0:
			metadata["title"] = os.path.splitext(os.path.basename(self["filename"]))[0]

		self.update(metadata)

		for mimetype in file.mime:
			for extension, mimeset in MIMETYPES.items():
				if mimetype in mimeset:
					self["mimetype"] = mimeset[0]
					self["extension"] = extension
					break
			if self["mimetype"] != None:
				break
		if self["mimetype"] == None and len(file.mime) != 0:
			self["mimetype"] = file.mime[0]

		self["length"] = file.info.length

		hash = hashlib.new("sha1")
		hash.update(str(metadata) + self["filename"])
		self["id"] = hash.hexdigest()
	
	def __missing__(self, key):
		return None