Пример #1
0
def save_ratings(user, ratings, overwrite):
    """
        Saves a list of imported ratings for the selected user
    """
    movies_rated_list = []
    movies_already_rated_list = []
    titles_rated = []
    titles_already_rated = []
    titles_not_rated = []
    f = lambda title, year: title if not year else title+" ("+str(year)+")"
    f_imdb = lambda title, year, imdb_id: "#"+str(imdb_id)+":"+ (title if not year else title+" ("+str(year)+")")
    #import codecs
    #dest = codecs.open('c:/temp/import.html', 'w', 'utf-8')
    #dest.write('<html><body>')

    def rate_film(film, film_title, year, score, review, overwrite):
        was_rated = save_rating(film, user, score, review, overwrite)
        if was_rated:
            movies_already_rated_list.append(film)
            titles_already_rated.append(f(film_title, year))
        if overwrite or not was_rated:
            movies_rated_list.append(film)
            titles_rated.append(f(film_title, year))
    
    for record in ratings:
        film_title = record['title']
        year = record['year'] if 'year' in record else None
        score = record['score']        
        imdb_id = record['imdb_id'] if 'imdb_id' in record else None
        criticker_id = record['criticker_id'] if 'criticker_id' in record else None
        review = record['review'] if 'review' in record else None        
        aka = None

        if criticker_id is not None:
            fetch_film_info_from_criticker(record)
            imdb_id = record['imdb_id']
            year = record['year']
            film_title = record['title']
            aka = record['aka']

        film = None
        if aka is not None:
            for title in aka:
                logger.debug("try to search film %s by alternative title: %s (%s): %s" % (film_title, title, str(year), imdb_id))
                film = search_film(film_title=title, year=year, imdb_id=imdb_id)
                if film:
                    break
        else:
            logger.debug("try to search %s (%s): %s" % (film_title, str(year), imdb_id))
            film = search_film(film_title=film_title, year=year, imdb_id=imdb_id)

        if film:
            logger.info("found movie %s: rated at %s" % (film, score))
            rate_film(film, film_title, year, score, review, overwrite)
        else:
            logger.debug("film %s not found" % film_title)
            if imdb_id:
                logger.info("try to search by imdb_id: %s" % imdb_id)
                movie = imdb_fetcher.get_movie_by_id(imdb_id, "http")

                if movie:
                    film, status = imdb_fetcher.save_movie_to_db(movie)
                else:
                    logger.error("Probably given IMDB_ID: %s is not a movie" % imdb_id)
                
                if film:
                    if status == FilmToImport.ACCEPTED:
                        importedfilm = ImportedFilm(user=user, film=film)
                        importedfilm.save()
                        logger.info("imported movie %s" % film)
                    logger.info("found movie %s: rated at %s" % (film, score))
                    rate_film(film, film_title, year, score, review, overwrite)
                else:
                    logger.error("Failed to import movie!. Continuing import anyway...")

            if not film:
                logger.info("Film %s not rated" % film_title)
                titles_not_rated.append(f(film_title, year))
                #dest.write("<a href='http://filmaster.pl/dodaj-film/?%s'>%s</a><br/>\n" \
                #    % (urllib.urlencode({'title':f(film_title, year).encode('utf-8'), 'imdb_id':str(imdb_id) if imdb_id else ''}), f(film_title, year)))
                    
            
    movies_not_rated = "; ".join(titles_not_rated)
    rating_import_log = ImportRatingsLog(user=user, 
                                         movies_rated="; ".join(titles_rated),
                                         movies_already_rated = "; ".join(titles_already_rated),
                                         movies_not_rated= movies_not_rated)
    #dest.write('</body></html>')
    #dest.close()
    rating_import_log.save()
    return movies_rated_list, movies_already_rated_list, movies_not_rated
def save_ratings(user, ratings, overwrite):
    """
        Saves a list of imported ratings for the selected user
    """
    movies_rated_list = []
    movies_already_rated_list = []
    titles_rated = []
    titles_already_rated = []
    titles_not_rated = []
    f = lambda title, year: title if not year else title + " (%s)" % str(year)

    def rate_film(film, film_title, year, score, review, overwrite):
        was_rated = save_rating(film, user, score, review, overwrite)
        if was_rated:
            movies_already_rated_list.append(film)
            titles_already_rated.append(f(film_title, year))
        if overwrite or not was_rated:
            movies_rated_list.append(film)
            titles_rated.append(f(film_title, year))

    for record in ratings:
        film_title = record['title']
        year = record['year'] if 'year' in record else None
        score = int(record['score'])
        imdb_id = record['imdb_id'] if 'imdb_id' in record else None
        criticker_id = record['criticker_id'] if 'criticker_id' in record else None
        filmweb_id = record['filmweb_id'] if 'filmweb_id' in record else None
        review = record['review'] if 'review' in record else None
        aka = None
        
        if 0 < score < 11:
            if criticker_id is not None:
                fetch_film_info_from_criticker(record)
                imdb_id = record['imdb_id']
                year = record['year']
                film_title = record['title']
                aka = record['aka']
    
            film = None
            if aka is not None:
                for title in aka:
                    logger.debug("try to search film %s by alternative title: %s (%s): %s" % (film_title, title, str(year), imdb_id))
                    film = search_film(film_title=title, year=year, imdb_id=imdb_id)
                    if film:
                        break
            else:
                logger.debug("try to search %s (%s): %s" % (film_title,
                                                            str(year), imdb_id))
                film = search_film(film_title=film_title, year=year,
                                   imdb_id=imdb_id)
    
            if film:
                logger.info("found movie %s: rated at %s" % (film, score))
                rate_film(film, film_title, year, score, review, overwrite)
            else:
                logger.debug("film %s not found" % film_title)
                if imdb_id:
                    logger.info("try to search by imdb_id: %s" % imdb_id)
                    movie = imdb_fetcher.get_movie_by_id(imdb_id, "http")
    
                    if movie:
                        film, status = imdb_fetcher.save_movie_to_db(movie)
                    else:
                        logger.error("Probably given IMDB_ID: %s is not a movie" %
                                     imdb_id)
    
                    if film:
                        if status == FilmToImport.ACCEPTED:
                            importedfilm = ImportedFilm(user=user, film=film)
                            importedfilm.save()
                            logger.info("imported movie %s" % film)
                        logger.info("found movie %s: rated at %s" % (film, score))
                        rate_film(film, film_title, year, score, review, overwrite)
                    else:
                        logger.error("Failed to import movie!. Continuing import anyway...")
    
                if not film:
                    logger.info("Film %s not rated" % film_title)
                    titles_not_rated.append(f(film_title, year))
        else:
            titles_not_rated.append(f(film_title, year))
        
    movies_not_rated = "; ".join(titles_not_rated)
    rating_import_log = ImportRatingsLog(user=user,
                                         movies_rated="; ".join(titles_rated),
                                         movies_already_rated="; ".join(titles_already_rated),
                                         movies_not_rated=movies_not_rated)
    rating_import_log.save()
    return movies_rated_list, movies_already_rated_list, movies_not_rated