Пример #1
0
def main():
    paths = ["mavenize/lib/db/movies"]

    movie_info_url = "http://api.rottentomatoes.com/api/public/v1.0/movies/%s.json?apikey=mz7z7f9zm79tc3hcaw3xb85w"
    movie_cast_url =  "http://api.rottentomatoes.com/api/public/v1.0/movies/%s/cast.json?apikey=mz7z7f9zm79tc3hcaw3xb85w"
    for path in paths:
        dirList = os.listdir( path )
        for fn in dirList:
            json_file = open( path + "/" + fn ).read()
            json_data = json.loads( json_file )

            print fn
            if json_data.has_key( "error" ):
                print "continuing on: " + str(fn)
                continue
            movies = json_data['movies']

            for movie in movies:
                # Data Pre-Processing
                mpaa_rating = process_mpaa_rating(movie)
                theater_date, dvd_date = process_date(movie) 

                # Insert Movie
                print "Trying to add %s..." % movie['title']
                try:
                    instance = LoadMovie(
                        title=movie['title'],
                        mpaa_rating=mpaa_rating,
                        runtime=movie['runtime'],
                        critic_score=movie['ratings']['critics_score'],
                        synopsis=movie['synopsis'],
                        theater_date=theater_date,
                        dvd_date=dvd_date
                    )

                    # Insert Genres, Directors, Actors, and Images
                    movie_url = movie_info_url % movie[ 'id' ]
                    movie_profile = call_api(movie_url)
                    instance.insert_genres(movie_profile['genres'])
                    directors = process_cast(
                        movie_profile['abridged_directors'])
                    instance.insert_directors(directors)

                    cast_url = movie_cast_url % movie[ 'id' ]
                    movie_cast = call_api(cast_url)
                    actors = process_cast(movie_cast['cast'])
                    instance.insert_actors(actors)
                    
                    image_url = movie['posters']['original']
                    instance.insert_image(image_url)

                except Exception, e:
                    print e
                    continue
Пример #2
0
def save_movie(movie):
    # FILL IN WITH PRINT STATEMENTS
    #print "These are movie keys passed on -- BEG"
   # instance=LoadMovie(
    #    movie[''],
    print('Adding movie '+ movie['names']['name'])

    try:
        keywordstring = ''
        for keyword in movie['keywords']:
            keywordstring = keywordstring + keyword + ','
        imdb = movie['imdb_id'][2:]
        instance = LoadMovie(
            title=movie['names']['name'],
            imdb_id=imdb,
            runtime=movie['runtime'],
            synopsis=movie['overview'],
            theater_date=movie['released'],
            keywords=keywordstring,
        )
        print('...loaded movie')
        #Inserting via LoadMovie helper methods
        #Genres, Directors, Actors, and then Images
        instance.insert_genres(movie['genres'])
        print('...loaded genres')
        #Sort into two arrays of actors and directors
        actors = []
        directors = []
        for person in movie['cast']:
            if (person['job'] == 'Actor') and (person['department'] == 'Actors'):
                actors.append(person['name'])
            elif (person['job'] == 'Director') and (person['department'] == 'Directing'):
                directors.append(person['name'])
        #Call methods to add actors and directors
        instance.insert_actors(actors)
        instance.insert_directors(directors)
        print('...loaded actors & directors')


        #insert_image only takes one image at a time, so traverse movie['pictures']
        if len(movie['pictures']) != 0:
            foundMid = False
            tempLargeImg = {}
            for image in movie['pictures']:
                if image['size']=='mid':
                    instance.insert_image(image['url'])
                    foundMid = True
                elif image['size']=='original':
                    tempLargeImg = image
            if foundMid == False:
                instance.insert_image(tempLargeImg['url'])
            print('...loaded image')
        else:
            print('...no pictures')

        print(movie['trailer'])
        if type(movie['trailer']) != type(None):
            if movie['trailer'].find('youtube') != -1:
                instance.insert_trailer(movie['trailer'])
                print('...loaded trailer')
            else:
                print('Not a YouTube link, did not insert.')
        else:
            print('No trailer link available.')
        print('Sucessfully loaded movie ' + movie['names']['name'])
    except Exception, e:
        print e