示例#1
0
文件: deluge.py 项目: opmuse/opmuse
    def update_torrents(self):
        torrents = self.client.call('core.get_torrents_status', {},
                                    ['name', 'files', 'save_path', 'time_added',
                                     'total_size', 'paused', 'is_finished', 'progress'])

        all_torrents = set()

        for torrent_id, in get_database().query(Torrent.torrent_id).all():
            all_torrents.add(torrent_id)

        for torrent_id, data in torrents.items():
            torrent_id = torrent_id.decode('utf-8')

            if torrent_id in all_torrents:
                all_torrents.remove(torrent_id)
                torrent = get_database().query(Torrent).filter(Torrent.torrent_id == torrent_id).one()
            else:
                torrent = Torrent()

            for file in data[b'files']:
                if Library.is_supported(file[b'path']):
                    has_supported_files = True
                    break
            else:
                has_supported_files = False

            torrent.torrent_id = torrent_id
            torrent.name = data[b'name'].decode('utf-8')
            torrent.has_supported_files = has_supported_files
            torrent.added = datetime.datetime.fromtimestamp(data[b'time_added'])
            torrent.size = data[b'total_size']
            torrent.paused = data[b'paused']
            torrent.finished = data[b'is_finished']
            torrent.progress = data[b'progress']

            get_database().add(torrent)
            get_database().commit()

        if len(all_torrents) > 0:
            (get_database().query(Torrent).filter(Torrent.torrent_id.in_(all_torrents))
                .delete(synchronize_session='fetch'))

        get_database().commit()
示例#2
0
文件: library.py 项目: opmuse/opmuse
    def _dir_tracks(self, tracks):
        dir_tracks = {}

        artist_covers = set()
        album_covers = set()

        for track in tracks:
            if track.artist is not None:
                artist_covers.add(track.artist.cover_path)

            if track.album is not None:
                album_covers.add(track.album.cover_path)

            if len(track.paths) == 0:
                continue

            for path in track.paths:
                dir = path.dir

                if dir not in dir_tracks:
                    dir_tracks[dir] = {
                        'paths': [],
                        'tracks': [],
                        'paths_and_tracks': [],
                        'pretty_dir': path.pretty_dir,
                        'files': [],
                    }

                dir_tracks[dir]['paths'].append(path.path)
                dir_tracks[dir]['tracks'].append(track)
                dir_tracks[dir]['paths_and_tracks'].append((path, track))

        for dir, dir_track in dir_tracks.items():
            dir_tracks[dir]['paths_and_tracks'] = sorted(dir_track['paths_and_tracks'], key=lambda pat: pat[0].path)

        for dir, item in dir_tracks.items():
            if not os.path.exists(dir):
                continue

            for file in os.listdir(dir):
                file = os.path.join(dir, file)

                if file not in item['paths']:
                    isdir = os.path.isdir(file)

                    if not isdir and LibraryService.is_supported(file):
                        track = library_dao.get_track_by_path(file)
                    else:
                        track = None

                    pretty_file = file[(len(dir) + 1):].decode("utf8", "replace")

                    if isdir:
                        pretty_file = "%s/" % pretty_file

                    if not isdir:
                        try:
                            stat = os.stat(file)
                            modified = datetime.datetime.fromtimestamp(stat.st_mtime)
                            size = stat.st_size
                        except FileNotFoundError:
                            continue
                    else:
                        size = modified = None

                    library_path = library_dao.get_library_path()

                    relative_file = file[len(library_path):]

                    dir_tracks[dir]['files'].append({
                        "file": file,
                        "relative_file": relative_file.decode('utf8', 'replace'),
                        "modified": modified,
                        "size": size,
                        "track": track,
                        "isdir": isdir,
                        "pretty_file": pretty_file,
                        "is_album_cover": file in album_covers,
                        "is_artist_cover": file in artist_covers
                    })

            dir_tracks[dir]['files'] = sorted(dir_tracks[dir]['files'],
                                              key=lambda item: "%d%s" % (not item["isdir"], item["file"]))

        dir_tracks = sorted(dir_tracks.items(), key=lambda d: d[0])

        return dir_tracks