def mutate(root, info, input=None): ok = True actor_instance = Actor.objects.get(id=input.actor_id) movie_instance = Movie( title=input.title, year=input.year, actor=actor_instance, ) movie_instance.save() return CreateMovie(ok=ok, movie=movie_instance)
def test_movie_creation(director, genre): initial_count = Movie.objects.all().count() movie = Movie( title='Indiana Jones', genre=genre, director=director, released='1994-11-04', ) movie.save() assert Movie.objects.all().count() == 1
def _upsert_movie(self, tmdb_id): movie = Movie.objects.filter(tmdb_id=tmdb_id).first() if movie is None: j_movie = self._tmdb_client.movie_details(tmdb_id) if j_movie is None: return None movie = Movie.from_json(j_movie) movie.save() # Store genres for j_genre in j_movie['genres']: genre = Genre.from_json(j_genre) count = Genre.objects.filter(id=genre.id).count() if count == 0: genre.save() movie.genres.add(genre) else: genre = Genre.objects.filter(id=genre.id).first() movie.genres.add(genre) # Process cast for j_cast in j_movie['credits']['cast']: crew = Crew() crew.movie_id = movie.id crew.category_id = 2 crew.name = j_cast['name'] crew.character_name = j_cast['character'] crew.picture_url = j_cast['profile_path'] crew.save() # Process directors for j_crew in j_movie['credits']['crew']: if j_crew['job'] == 'Director': crew = Crew() crew.movie_id = movie.id crew.category_id = 1 crew.name = j_crew['name'] crew.picture_url = j_crew['profile_path'] crew.save() # Process pictures if 'backdrop_path' in j_movie and j_movie[ 'backdrop_path'] is not None: picture = Picture() picture.category_id = 2 picture.movie_id = movie.id picture.url = j_movie['backdrop_path'] picture.save() if 'poster_path' in j_movie and j_movie['poster_path'] is not None: picture = Picture() picture.category_id = 1 picture.movie_id = movie.id picture.url = j_movie['poster_path'] picture.save() return movie
def save(cls, data, instance=None): try: movie = instance if instance else Movie() for key, value in data.items(): setattr(movie, key, value) movie.save() return movie except Exception as ex: # todo: handle log raise ex
def seed_movies(self): j_movies = self.tmdb_client.random_movies() for j_movie in j_movies: movie = Movie.from_json(j_movie) count = Movie.objects.filter(tmdb_id=movie.tmdb_id).count() if count == 0: movie.save() self.process_genres(j_movie, movie) self.process_credits(j_movie, movie) self.process_pictures(j_movie, movie)
def movies(director, genre): return Movie.objects.bulk_create( Movie( director=director, genre=genre, title="Title1", releaded='2020-08-20', ), Movie( director=director, genre=genre, title="Title1", releaded='2020-08-20', ), Movie( director=director, genre=genre, title="Title1", releaded='2020-08-20', ), )
def save_subject_detail(id, type): response = requests.get(SUBJECT_API % id).text s = BeautifulSoup(response, 'html.parser') try: movie = Movie() movie.title = s.body.find(attrs={ 'property': 'v:itemreviewed' }).get_text().strip() movie.summary = s.body.find(attrs={ 'property': 'v:summary' }).get_text().strip() movie.director = s.body.find(attrs={ 'rel': 'v:directedBy' }).get_text().strip() movie.actors = ','.join( map(lambda x: x.get_text().strip(), s.body.find_all(attrs={'rel': 'v:starring'}))) movie.rating = s.body.find(attrs={ 'property': 'v:average' }).get_text().strip() movie.rating_count = s.body.find(attrs={ 'property': 'v:votes' }).get_text().strip() movie.cover = s.body.find(attrs={ 'rel': 'v:image' }).attrs['src'].strip() movie.show_at = datetime.strptime( s.body.find(attrs={ 'property': 'v:initialReleaseDate' }).get_text()[0:10], '%Y-%m-%d') movie.crawl_at = datetime.utcnow() movie.genres = ','.join( map(lambda x: x.get_text(), s.body.find_all(attrs={'property': 'v:genre'}))).strip() movie.category = type print 'saving movie => ', movie.title db.session.add(movie) db.session.commit() except StandardError, e: print 'parse subject error', e raise e
def __init__(self, settings): database = TinyDB(settings["DATABASE"], indent=4) self.Movie = Movie(database, TABLE_MOVIES) self.settings = settings
def save_subject_detail(id, type): response = requests.get(SUBJECT_API % id).text s = BeautifulSoup(response, 'html.parser') try: movie = Movie() movie.title = s.body.find(attrs={'property':'v:itemreviewed'}).get_text().strip() movie.summary = s.body.find(attrs={'property':'v:summary'}).get_text().strip() movie.director = s.body.find(attrs={'rel':'v:directedBy'}).get_text().strip() movie.actors = ','.join(map(lambda x: x.get_text().strip(), s.body.find_all(attrs={'rel':'v:starring'}))) movie.rating = s.body.find(attrs={'property':'v:average'}).get_text().strip() movie.rating_count = s.body.find(attrs={'property':'v:votes'}).get_text().strip() movie.cover = s.body.find(attrs={'rel':'v:image'}).attrs['src'].strip() movie.show_at = datetime.strptime(s.body.find(attrs={'property':'v:initialReleaseDate'}).get_text()[0:10], '%Y-%m-%d') movie.crawl_at = datetime.utcnow() movie.genres = ','.join(map(lambda x: x.get_text(), s.body.find_all(attrs={'property':'v:genre'}))).strip() movie.category = type print 'saving movie => ', movie.title db.session.add(movie) db.session.commit() except StandardError, e: print 'parse subject error', e raise e
movie_list_uni = re.findall(r'\[.+\]', schema)[0][1:-1] movie_list = movie_list_uni.replace("},{", "};{").split(';') for item in movie_list: item = json.loads(item) item_position = item["position"] item_movie_url = item["url"] item_request = urllib2.urlopen(url + item_movie_url) item_soup = bs4.BeautifulSoup(item_request, 'html.parser') item_schema = item_soup.findAll("script", {"id": "jsonLdSchema"})[0].get_text() item_dict = json.loads(item_schema) item_title = item_dict["name"] if Movie.objects.filter(title=item_title).exists(): pass else: item_production_company = item_dict["productionCompany"]["name"] item_img_url = item_soup.find("img", {"class": "posterImage"}).attrs["src"] try: item_rating = item_dict["aggregateRating"]["ratingValue"] except KeyError: item_rating = None movie_object = Movie( position=item_position, title=item_title, production_company=item_production_company, movie_url=item_movie_url, image_url=item_img_url, rating=item_rating ) movie_object.save()