Пример #1
0
    def __init__(self, tag_dict):
        """Create a Submission object from a dict of tags.

        Args:
          tag_dict: a dictionary as returned by Playlist.get_current_tags(),
            i.e., containing 'Title', 'Artist', etc.
        """
        if not all(tag_dict[x] for x in ['Title', 'Artist', 'Length']):
            raise self.RequiredDataMissing()
        elif tag_dict['Length'] < TRACK_MIN_LENGTH:
            raise self.TrackTooShort()

        self.path = None
        self.length = tag_dict['Length']
        self.start_time = int(time.time())

        self.param = {
            'm': '',
            'r': '',
            'o': SOURCE_USER,
            'l': str(self.length),
            'i': str(self.start_time),
            'n': util.ensure_utf8(tag_dict['Track']),
            't': util.ensure_utf8(tag_dict['Title']),
            'b': util.ensure_utf8(tag_dict['Album']),
            'a': util.ensure_utf8(tag_dict['Artist']),
        }
Пример #2
0
    def load_from_file(cls, path):
        with open(path) as f:
            try:
                param = json.load(f)
            except ValueError:
                return None
            else:
                # TODO: could it be possible to get json to give us str objects?
                param = dict((k, util.ensure_utf8(param[k])) for k in param)

        if set(param.keys()) == set('mrolintba'):
            obj = cls.__new__(cls)
            obj.path = path
            obj.param = param
            obj.length = int(param['l'])
            obj.start_time = int(param['i'])
            return obj
        else:
            return None