예제 #1
0
    def post(self, category, command):
        settings = Settings.get_instance()
        items = settings[category]

        if command == "add":
            value = self.must_get("value")
            new_id = items.add(value)
            self.response.write(str(new_id))

        elif command == "edit":
            id = int(self.must_get("id"))
            value = self.must_get("value")
            success = items.edit(id, value)
            if success:
                self.response.write("OK")
            else:
                self.abort(400, "ID %d doesn't exist." % id)

        elif command == "remove":
            id = int(self.must_get("id"))
            success = items.remove(id)
            if success:
                self.response.write("OK")
            else:
                self.abort(400, "ID %d doesn't exist." % id)

        else:
            self.abort(500, "Invalid command. Should not occur.")

        Settings.save()
예제 #2
0
class Library():
    def __init__(self):
        self.films = []
        self.settings = Settings()
        global portable_paths
        portable_paths = self.settings.portable_paths
        self._load()

    def _load(self):
        content = json.read(_filename)
        if content:
            for dictionary in content:
                self.films.append(Film(**dictionary))

    def save(self):
        json.write(self.films, _filename)
        self.settings.save()

    def update(self):
        from os import walk

        found = []

        for directory in self.settings.directories():
            if path.exists(directory):
                for root, dirnames, filenames in walk(directory):
                    for filename in filenames:

                        # Get around the 256 character limit by prepending \\\\?\\
                        absolute = path.join("\\\?\\", root, filename)

                        if filename.lower().endswith(tuple(self.settings.file_extensions)):

                            mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = stat(absolute)
                            if size > self.settings.minimum_size_byte:

                                found.append(Film(abspath=absolute, last_modification=get_datetime(mtime), size=size))
            else:
                self.settings.directories().remove(directory)

        # Film has been removed from disk - remove from library
        for film in self.films:
            if film not in found:
                #print('Removed: {}'.format(film.print()))
                self.films.remove(film)
                #del film

        # New film - add to library
        for film in found:
            if film not in self.films:
                #print('Found: {}'.format(film.print()))
                self.films.append(film)

    def random_movie(self):
        import random

        if self.films:
            found = []

            for film in self.films:
                if not film.watched:
                    found.append(film)

            if found:
                return random.choice(found)
            else:
                print("There are no unwatched films.")
                return None
        else:
            print("There are no films added.")
            return None