-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_movies.py
54 lines (44 loc) · 1.55 KB
/
load_movies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys, os
import pandas as pd
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "movierec.settings")
import django, re
django.setup()
from reviews.models import Movie, Genre
def save_movie_from_row(movie_row):
movie_dur=movie_row[1][movie_row[1].rfind("(") + 1:movie_row[1].rfind(")")].strip()
#if len(movie_dur)==4 and movie_dur>=2000:
movie = Movie()
'''
try:
movie.id = Movie.objects.last().pk + 1 #movie_row[0]
except: #TypeError:
movie.id = 1
'''
movie.id = movie_row[0]
movie.name = movie_row[1].strip()
#movie.year = movie.name[-5:-1]
#if len(movie_dur)==4:
movie.duration = movie_dur #movie.name[movie.name.rfind("(") + 1:movie.name.rfind(")")].strip()
#else: #movie.name.rfind("(") == -1 and movie.name.rfind(")") == -1:
# movie.duration = ''
movie.save()
#movie.duration = .strip("!@#$%^&*()[]{};:,./<>?\|`~-=_+")
# movie_genres = [x.strip() for x in movie_row[2].split("|")]
# movie.genres.add(*Genre.objects.filter(name__in=movie_genres)) # [i]))
'''
for g in movie_genres:
movie.genres.add(Genre.objects.get(name=g)) #[i]))
'''
#movie.save()
if __name__ == "__main__":
if len(sys.argv) == 2:
print "Reading from file " + str(sys.argv[1])
movies_df = pd.read_csv(sys.argv[1])
print movies_df
movies_df.apply(
save_movie_from_row,
axis=1
)
print "There are {} movies".format(Movie.objects.count())
else:
print "Please, provide Movie file path"