def __init__(self, provider, pid, name, slug=None, person_type=ACTOR_TYPE, name_rus=None, poster=None): parser = ParserFactory.get_parser(provider) if isinstance(parser, IMDBParser): self.imdb_id = pid elif isinstance(parser, KinopoiskParser): self.kp_id = pid else: raise TypeError('Parser %s does not supported' % type(parser)) self.name = name if slug is None: self.slug = slugify(self.name) else: self.slug = slug if not name_rus is None: self.name_rus = name_rus self.person_type = person_type self.status = Person.ACTIVE_STATUS if not person_type is None: self.poster = poster
def parse_key(cls, **kwargs): key = kwargs['key'] make_parser = kwargs.get('make_parser', False) valid = cls.validate_keys(keys=(key, )) if valid: key_type, movie_id, provider = key.split(':') if make_parser: provider = ParserFactory.get_parser(provider) return movie_id, provider return None, None
def parse_key(cls, **kwargs): key = kwargs['key'] make_parser = kwargs.get('make_parser', False) try: key_type, item_id, provider, person_type = key.split(':') except ValueError: pass else: if key_type == 'person': if make_parser: provider = ParserFactory.get_parser(provider) return item_id, provider, person_type return None, None, None
def group_keys_by_provider(cls, **kwargs): keys = kwargs.get('keys', []) make_parser = kwargs.get('make_parser', False) keys = cls.validate_keys(keys=keys) result = imap(lambda key: cls.parse_key(key=key), keys) result = sorted(result, key=lambda key: key[1]) result = groupby(result, lambda key: key[1]) groups = [] for k, group in result: groups.append({ 'provider': k, 'keys': [item[0] for item in group], 'parser': ParserFactory.get_parser(k) if make_parser else None }) return groups
def save_poster(self, provider, load_original=False): path = self.get_poster_path() parser = ParserFactory.get_parser(provider) with open(path, 'w') as fp: url = getattr(self, 'poster') if load_original: url = re.sub(ur'(\.[\w\d_,]+)\.', '.', getattr(self, 'poster')) r = parser.load(url, make_doc=False, raw=True) for chunk in r.iter_content(): fp.write(chunk) self.poster = path try: db.session.commit() except IntegrityError: db.session.rollback() return path
def save_relations(self): missing_movies = set() for provider in self.providers: key = Person.make_key(provider=provider, item_id=getattr(self, '%s_id' % provider), person_type=self.person_type) sections = ['best_works', 'actor', 'director', 'writer', 'producer'] for section in sections: section_key = '%s_%s' % (key, section) keys = redis.smembers(section_key) if not keys: continue movie_keys = ParserFactory.group_keys_by_parser(keys) movies = getattr(self, section, []) for k, value in movie_keys.iteritems(): field = getattr(Movie, '%s_id' % k, None) if not field is None: existed = db.session.query(Movie, field).filter( field.in_(value)).all() existed_movies = set() for movie, movie_key in existed: if not movie in movies: movies.append(movie) existed_key = Movie.make_key( provider=k, item_id=movie_key) existed_movies.add(existed_key) redis.srem(section_key, existed_key) try: db.session.commit() except IntegrityError: db.session.rollback() missing_movies = missing_movies.union( set(keys).difference(existed_movies)) if not redis.exists(section_key): redis.srem(Person.not_saved_relations_key, key) redis.sadd(Person.saved_relations_key, key) return missing_movies
def __init__(self, provider, mid, title, year, rating=0.0, title_rus=None, slug=None, poster=None, is_series=False, status=ACTIVE_STATUS, world_premiere=None, rus_premiere=None, dvd_premiere=None): if not isinstance(provider, (IMDBParser, KinopoiskParser)): parser = ParserFactory.get_parser(provider) else: parser = provider if isinstance(parser, IMDBParser): self.imdb_id = mid self.imdb_rating = rating elif isinstance(parser, KinopoiskParser): self.kp_id = mid self.kp_rating = rating else: raise TypeError('Parser %s does not supported' % type(parser)) self.title = title self.year = year self.status = status if not title_rus is None: self.title_rus = title_rus if slug is None: self.slug = slugify(self.title) if poster: self.poster = poster if is_series: self.is_series = is_series if world_premiere: self.world_premiere_date = world_premiere if rus_premiere: self.rus_premiere_date = rus_premiere if dvd_premiere: self.dvd_premiere_date = dvd_premiere