Package deefuzzer :: Package tools :: Module mediabase
[hide private]
[frames] | no frames]

Source Code for Module deefuzzer.tools.mediabase

  1  __author__ = 'Dennis Wallace' 
  2   
  3  import tempfile 
  4   
5 -class MediaBase(object):
6 """Base Media class. All media objects should inherit from this class 7 to allow common functions to be used in core code. See MP3 and OGG classes 8 for examples on how to configure a subclass.""" 9
10 - def __init__(self):
11 object.__init__(self) 12 13 # Set the following five values in an inherited subclass. 14 15 # A text string describing this media type 16 self.description = '' 17 18 # A text string declaring the MIME Type for this media type 19 self.mime_type = '' 20 21 # A text string declaring the common file extension for this media type 22 self.extension = '' 23 24 # A text string declaring the media format. The self.format property 25 # should be unique across all subclasses inherited from MediaBase. 26 self.format = '' 27 28 # tagdata contains a dictionary of tags to use to gather metadata from the sourceobj 29 self.tagdata = {} 30 31 self.media = '' 32 self.item_id = '' 33 self.source = '' 34 self.options = {} 35 self.bitrate_default = 0 36 self.info = {} 37 self.bitrate = 0 38 self.length = 0 39 40 # sourceobj contains the metadata information for the referenced object 41 self.sourceobj = {} 42 43 self.media_info = [] 44 self.file_name = '' 45 self.file_title = '' 46 self.file_ext = '' 47 self.size = 0 48 self.metadata = {} 49 50 # A more cross-platform way to do this 51 self.cache_dir = tempfile.gettempdir()
52
53 - def get_format(self):
54 """Gets the format string of the media type""" 55 return self.format
56
57 - def get_file_extension(self):
58 """Gets the actual file extension string of the media""" 59 return self.file_ext
60
61 - def get_mime_type(self):
62 """Gets the MIME Type string for this media type""" 63 return self.mime_type
64
65 - def get_description(self):
66 """Gets the description string for this media type""" 67 return self.description
68
69 - def set_cache_dir(self, path):
70 """Sets an alternate location for temporary cache files used in this media object""" 71 self.cache_dir = path
72
73 - def get_file_metadata(self, clear_cache=False):
74 """Returns the metadata for the media, filtered by the tagdata dictionary for this media type. Return value is 75 read from cache if possible (or unless clear_cache is set to True)""" 76 if not self.metadata or clear_cache: 77 self.read_file_metadata() 78 return self.metadata
79
80 - def read_file_metadata(self):
81 """Reads the metadata for the media, filtered by the tagdata dictionary for this media type""" 82 self.metadata = {} 83 for key in self.tagdata.keys(): 84 self.metadata[key] = '' 85 try: 86 self.metadata[key] = self.sourceobj[key][0] 87 except: 88 pass 89 90 try: 91 if self.tagdata[key] != '' and self.metadata[key] == "": 92 self.metadata[key] = self.sourceobj[self.tagdata[key]][0] 93 except: 94 pass
95
96 - def get_metadata_value(self, key, clean=False, clear_cache=False):
97 """Returns a metadata value for a give key. If clean is True, then the resulting string will 98 be cleaned before it is returned. If the key does not exist, an empty string is returned. Return 99 value is read from cache if possible (or unless clear_cache is set to True)""" 100 if not self.metadata or clear_cache: 101 self.read_file_metadata() 102 103 if key not in self.metadata: 104 return '' 105 r = self.metadata[key] 106 if not r: 107 r = ""; 108 if clean: 109 r = r.replace('_',' ').strip() 110 return r.encode('utf-8')
111
112 - def get_title(self):
113 """Returns the cleaned title for this media""" 114 return self.get_metadata_value('title', True)
115
116 - def get_artist(self):
117 """Returns the cleaned artist for this media""" 118 return self.get_metadata_value('artist', True)
119
120 - def get_song(self, usefn=True):
121 """Returns a string in the form "artist - title" for this media. If either artist or title are blank, 122 only the non-blank field is returned. If both fields are blank, and the usefn parameter is True, then 123 the filename is returned instead. Otherwise, an empty string is returned.""" 124 a = self.get_metadata_value('artist', True) 125 t = self.get_metadata_value('title', True) 126 if len(a) == 0 and len(t) == 0 and usefn: 127 if self.file_name: 128 a = self.file_name.encode('utf-8') 129 r = a 130 if len(a) > 0 and len(t) > 0: 131 r += ' - ' 132 r += t 133 return r
134