walker.py 2.06 KB
Newer Older
1
2
3
import os
import mutagen
import threading
Gerion Entrup's avatar
Gerion Entrup committed
4
import logging
5
6
7

from sqlalchemy.orm import Session

8
from model import Recording
9
from utils import pairwise
10
from collector import Paths, NEW, NewData
11
12
13
14

class Walker(threading.Thread):
    def __init__(self, session_fac, sourcedir):
        super().__init__(target=self)
Gerion Entrup's avatar
Gerion Entrup committed
15
        self.log = logging.getLogger('walker')
16
17
18
19
20
21
22
23
24
25
26
        self._path = sourcedir
        session = session_fac()
        self._mbids = set(e.gid for e in session.query(Recording).all())
        session.commit()
        session.close()

    def run(self):
        for root, dirs, files in os.walk(self._path):
            for file in files:
                mbid = None
                filepath = os.path.join(root, file)
Gerion Entrup's avatar
Gerion Entrup committed
27
                self.log.debug('Add file to queue: {}'.format(filepath))
28
29
30
                try:
                    mut = mutagen.File(filepath)
                except AttributeError:
Gerion Entrup's avatar
Gerion Entrup committed
31
                    self.log.info('Unknown file type: {}'.format(filepath))
32
33
                    continue
                except Exception as err:
Gerion Entrup's avatar
Gerion Entrup committed
34
                    self.log.error('Unexpected exception: {}, Path: {}'.format(err, filepath))
35
36
37
38
                    continue
                if mut is not None:
                    mbid = self.get_mbid(mut)
                if mbid is not None and mbid not in self._mbids:
39
                    Paths.put((NEW, NewData(mbid=mbid, path=os.path.abspath(filepath))))
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
                    self._mbids.add(mbid)

    def get_mbid(self, mut):
        for key in mut:
            # ogg, flac
            if key == 'musicbrainz_trackid':
                return mut[key][0]

            # mp3
            if key == 'UFID:http://musicbrainz.org':
                return mut[key].data.decode('ascii')

            # mp4
            if key == '----:com.apple.iTunes:MusicBrainz Track Id':
                if mut[key][0].dataformat == mutagen.mp4.AtomDataType.UTF8:
                    return mut[key][0].decode('utf8')
                else:
Gerion Entrup's avatar
Gerion Entrup committed
57
                    raise Exception('MP4 could not be parsed correctly. Dataformat is not UTF8')
58
59
                    return None