def _get_or_create_genre(name): try: genre = Genre.objects.filter(name=name)[0] except IndexError: genre = Genre(name=name) genre.save() return genre
def genres(filename): data = readfile(filename, '|') for line in data: if len(line) == 0: continue genre = Genre() genre.name = line[0] genre.genre_id = line[1] genre.save()
class GenreTest(TestCase): def setUp(self): self.genre = Genre( name='Action' ) self.genre.save() def test_create(self): 'Genre instance may be saved' self.assertEqual(1, self.genre.pk) def test_unicode(self): 'Genre string representation may be the name.' self.assertEqual(u'Action', unicode(self.genre))
def add_data(request): conn = sqlite3.connect('movies.db') cursor = conn.execute("SELECT * from mytable") for row in cursor: m = Movie(name=row[7], rating=row[19]) if row[21] != None: m.cast = row[21] else: m.cast = 'None' id = None if row[23] != None: if Director.objects.filter(name=row[23]).exists(): id = Director.objects.filter(name=row[23]).first().id else: d = Director(name=row[23]) d.save() id = d.id m.director = d else: if not Director.objects.filter(name='None').exists(): Director(name='None').save() d = Director.objects.filter(name='None').first() m.director = d genre = None try: genre = row[2].split() for g in genre: if Genre.objects.filter(genre=g).exists(): gen = Genre.objects.filter(genre=g).first() m.genre.add(gen) else: gen = Genre(genre=g) gen.save() m.genre.add(gen) except: pass m.description = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sed ligula libero. Maecenas ut semper mi. Duis ut tempus urna, sit amet condimentum dolor. Donec sollicitudin lacus ut placerat vestibulum. Nam eu neque ut tellus feugiat cursus non a eros. Fusce gravida nisl ac diam iaculis dignissim. Quisque eleifend tristique enim, semper facilisis nisi rhoncus in. Ut at velit dolor. Etiam placerat est sed sollicitudin pulvinar. Phasellus consectetur arcu et massa porttitor convallis. In posuere a ex sed mattis. Donec vitae dui cursus, tristique nibh a, fringilla dui. Nam ornare in ante nec ornare.' m.save() print(m.name) return JsonResponse({})
def genres(): from movies.models import Movie, Genre, MovieGenreRelation Genre.objects.all().delete() reader = csv.reader(open(datasets_path + "genres.csv")) next(reader) # header skip for row in reader: if not Genre.objects.filter(name=row[1]).exists(): genre = Genre( name=row[1] ) genre.save() else: genre = Genre.objects.get(name=row[1]) relation = MovieGenreRelation( movie_id=row[0], genre_id=genre.id ) relation.save()
def import_dataset(self, dataset: MovieLensDataSet): genres: dict[str, Genre] = { genre.name: genre for genre in Genre.objects.all() } movies_added = 0 genres_added = 0 users_added = 0 ratings_added = 0 tags_added = 0 for raw_movies in chunked(read_csv_dicts(dataset.movies, unit='movie'), 500): movies = [] genres_encountered = set() for raw_movie in raw_movies: raw_title = raw_movie['title'].strip() if match := re.match(r'^(.+) \((\d{4})\)$', raw_title): title, year = match.groups() else: title, year = raw_title, None movie_id = int(raw_movie['movieId']) movie_genres = set(raw_movie['genres'].split('|')) genres_encountered |= movie_genres movie = Movie(id=movie_id, title=title, year=year) movie._raw_genres = movie_genres movies.append(movie) movies_added += len(Movie.objects.bulk_create(movies, ignore_conflicts=True)) if missing_genre_names := genres_encountered - set(genres): missing_genres: list[Genre] = [] for genre_name in missing_genre_names: genre = Genre(name=genre_name) genres[genre_name] = genre missing_genres.append(genre) genres_added += len(Genre.objects.bulk_create(missing_genres))
def save(self): if __DEBUG__: print "title : " + self.title print "title_fr : " + self.title_fr print "title_int : " + self.title_int print "year : " + str(self.year) if self.runtime: print "runtime : " + self.runtime print "poster : " + self.poster if self.rating: print "rating : " + str(self.rating) if self.votes: print "votes : " + str(self.votes) print "plot : " + self.plot print "language : " + self.language m = Movie.objects.create( title=self.title, title_fr=self.title_fr, title_int=self.title_int, year=self.year, runtime=self.runtime, id_imdb=self.id_imdb, poster=self.poster, rating=self.rating, votes=self.votes, plot=self.plot, language=self.language) m.save() for idx, actor in enumerate(self.persons['cast']): p = Person.add(actor) MovieContributors.objects.create( person=p, movie=m, function='A', rank=idx) for idx, director in enumerate(self.persons['director']): p = Person.add(director) MovieContributors.objects.create( person=p, movie=m, function='D', rank=idx) for idx, writer in enumerate(self.persons['writer']): p = Person.add(writer) p.save() MovieContributors.objects.create( person=p, movie=m, function='W', rank=idx) for genre in self.genres: g = Genre.add(genre) m.genres.add(g) return m
def update_dataset(request): movietitles = [] # 장르 항목 요청 response_gens = requests.get(GENRE_URL) response_gens = response_gens.json() print(GENRE_URL) # print(response_gens, '####') for gen in response_gens['genres']: print(gen) genre_instance = Genre() genre_instance.genre_id = gen['id'] genre_instance.name = gen['name'] genre_instance.save() # 요청 보내기 (1, 25페이지 까지) for page in range(1, 25): REQ_URL = MOVIE_URL + f'&page={page}' print(REQ_URL) response = requests.get(REQ_URL) response = response.json() # response에서 20개의 자료 추출하기 for num in range(0, 20): # movie 인스턴스 호출하고 DB에 저장하기 movie = Movie() movie.movie_id = response['results'][num]['id'] movie.popularity = response['results'][num]['popularity'] movie.vote_count = response['results'][num]['vote_count'] movie.poster_path = response['results'][num]['poster_path'] movie.backdrop_path = response['results'][num]['backdrop_path'] movie.original_language = response['results'][num][ 'original_language'] movie.original_title = response['results'][num]['original_title'] movie.title = response['results'][num]['title'] movie.vote_average = response['results'][num]['vote_average'] movie.overview = response['results'][num]['overview'] movie.release_date = response['results'][num]['release_date'] # # movie detail api요청 및 저장(오래걸림) # movie_id = movie.movie_id # DETAIL_URL = BASE_URL + f'movie/{movie_id}?' + API_KEY + '&language=ko-KR' # detail_res = requests.get(DETAIL_URL).json() # # runtime 데이터 저장 # movie.runtime = detail_res['runtime'] movie.save() # # production country data DB에 저장 # for country_data in detail_res['production_countries']: # country = Country() # country.iso = country_data['iso_3166_1'] # country.name = country_data['name'] # country.save() # M2M 관계 중계테이블에 저장(영화-국가) # for country_data in detail_res['production_countries']: # iso = country_data['iso_3166_1'] # country = Country.objects.get(iso=iso) # country.movies.add(movie) # M2M 관계 중계테이블에 저장(영화-장르) for genid in response['results'][num]['genre_ids']: # print(genid) genre = Genre.objects.get(genre_id=genid) genre.movies.add(movie) # 제대로 받아지는지 확인한다. print(movie.title) # movietitles에 담아서 template에 출력해보자. movietitles.append(movie.title) # 완료시 success문구 출력 print('success') context = { 'movietitles': movietitles, } return render(request, 'make_dataset/update_dataset.html', context)
def test_genre_has_str_repr(self): genre = Genre(name='Comedy') self.assertEqual(genre.name, str(genre))
tagline=mov['tagline'], poster='', date=timezone.now(), awards='', youtubeid='', tmdbdata=True, adult=mov['adult'] ) m.save() else: print "Not creating %s" % j['info']['title'] m = Movie.objects.get(imdb_id=j['imdbid']) for genre in mov['genres']: if not Genre.objects.filter(genre=genre['name']): g = Genre(genre=genre['name']) g.save() g = Genre.objects.get(genre=genre['name']) g.movie.add(m) g.save() for actor in j['credits']['cast']: if not Crew.objects.filter(crewid=actor['id']): print "Creating actor: %s:: %s as %s" % (j['info']['title'], actor['name'], actor['character']) a = Crew(crewid=actor['id'], name=actor['name'], profilepath=actor['profile_path'], job='a') a.save() else: a = Crew.objects.get(crewid=actor['id']) if not CrewCredit.objects.filter(movie=m).filter(crew=a): if len(actor['character']) > 50: actor['character'] = actor['character'][0:50]
def handle(self, *args, **options): for gid in movie_genre: self.stdout.write(f"Adding {gid}, {movie_genre[gid]}") g = Genre(genre_id=gid, genre=movie_genre[gid]) g.save()
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" warnings.filterwarnings(action='ignore') import requests from bs4 import BeautifulSoup as bs # https://developers.themoviedb.org/3/discover/movie-discover URL = 'https://api.themoviedb.org/3' API_KEY = os.environ.get('MOVIE_KEY') # ## 장르 url_genres = f'{URL}/genre/movie/list?api_key={API_KEY}' result = requests.get(url_genres) genres = result.json()['genres'] for g in genres: genre = Genre() genre.pk = g['id'] genre.genre_name = g['name'] genre.save() # ## 영화(2000~2021) def store_movies(year): print(f'storing-----------{year}') with open(f'./data/movie_{year}.json', 'r') as fp: movies = json.load(fp) # delete stored movies movies_year = Movie.objects.filter(release_date__year=year) movies_year.delete() # using bulk
def setUp(self): self.genre = Genre( name='Action' ) self.genre.save()