Example #1
0
 def synchronise_tmdb_images(self):
     """
     add ukrainian images if it exists in tmdb
     """
     from images.models import Image
     tmdb_movie = tmdb.Movies(self.tmdb_id)
     images = tmdb_movie.images()
     images_ids = []
     poster = False
     for item in images['posters']:
         if item['iso_639_1'] == LANG:
             image = Image.upload_image_from_tmdb(item['file_path'])
             # make a poster if exists at least one language specific image
             if not poster:
                 self.poster = image
                 self.save()
             images_ids.append(image.id)
     self.images.add(*images_ids)
     return self
Example #2
0
def handle_info(tmdb_id):
    """
    add information about movie from tmdb to database
    information only from tmdb_movie.info() request
    :param tmdb_id: id from tmdb
    :return: Movie object
    """
    tmdb_movie = tmdb.Movies(tmdb_id)
    try:
        response = tmdb_movie.info(language=LANG)
    except HTTPError as e:
        print(e)
        return
    if len(response['title']) > 128:
        return
    # add main movie data
    main_data = {
        'title':
        response['title'],
        'release_date':
        response['release_date'],
        'release_status':
        RELEASE_STATUSES.get(response['status'], response['status']),
        'tmdb_id':
        response['id']
    }
    movie = Movie(**main_data)
    movie.save()

    info_data = {
        'original_title': response['original_title'],
        'budget': response['budget'],
        'revenue': response['revenue'],
        'homepage': response['homepage'],
        'original_language': response['original_language'],
        'runtime': response['runtime'],
        'imdb_id': response['imdb_id'],
        'overview_ua': response['overview'],
        'tagline': response['tagline'],
        'translated': False
    }
    if info_data['overview_ua']:
        info_data['overview'], info_data['translated'] = info_data[
            'overview_ua'], True
    else:
        # additional query
        response = tmdb_movie.info()
        info_data['overview'], info_data['tagline'] = response[
            'overview'], response['tagline']
    info = Info(movie=movie, **info_data)
    info.save()

    genres_ids = [item['id'] for item in response['genres']]
    movie.genres.add(*genres_ids)

    # add production companies
    companies_ids = []
    for item in response['production_companies']:
        try:
            company = ProductionCompany.objects.get(id=item['id'])
        except ProductionCompany.DoesNotExist:
            company = ProductionCompany.objects.create(name=item['name'],
                                                       id=item['id'])

        companies_ids.append(company.id)
    movie.production_companies.add(*companies_ids)

    # add countries
    countries_ids = []
    for item in response['production_countries']:
        try:
            country = Country.objects.get(iso_3166_1=item['iso_3166_1'])
        except Country.DoesNotExist:
            country = Country.objects.create(name=item['name'],
                                             iso_3166_1=item['iso_3166_1'])
        countries_ids.append(country.id)
    movie.production_countries.add(*countries_ids)

    # main poster (english)
    poster = response.get('poster_path', '')
    if poster:
        image = Image.upload_image_from_tmdb(poster)
        movie.poster = image
        movie.save()
        movie.images.add(*[image.id])

    return movie