| 9 | |
| 10 | |
| 11 | class PhotoMetadata(object): |
| 12 | def __init__(self, path): |
| 13 | self.data = {} |
| 14 | try: |
| 15 | # exiftool produces data such as MIME Type for non-photos too |
| 16 | result = Popen(['exiftool', path], stdout=PIPE, stdin=PIPE, stderr=PIPE).communicate()[0].decode('utf-8', 'ignore') |
| 17 | except UnicodeDecodeError: |
| 18 | result = '' |
| 19 | for line in str(result).split('\n'): |
| 20 | if line: |
| 21 | try: |
| 22 | k, v = line.split(':', 1) |
| 23 | self.data[k.strip()] = v.strip() |
| 24 | except ValueError: |
| 25 | pass |
| 26 | |
| 27 | # Some file MIME Types can not be identified by exiftool so we fall back to Python's mimetypes library so the get_mimetype() funciton below is universal |
| 28 | if not self.data.get('MIME Type'): |
| 29 | self.data['MIME Type'] = mimetypes.guess_type(path)[0] |
| 30 | |
| 31 | def get(self, attribute, default=None): |
| 32 | return self.data.get(attribute, default) |
| 33 | |
| 34 | def get_all(self): |
| 35 | return self.data |
| 36 | |
| 37 | |
| 38 | def parse_datetime(date_str): |
no outgoing calls