-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactors.py
57 lines (39 loc) · 1.56 KB
/
actors.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
55
56
57
from pymongo import MongoClient
import json
# Connect to MongoDB localhost
client = MongoClient()
# Use db
db = client['db']
# Select collection
credits = db.credits
# Delete existing actors collection
db.actors.drop()
# Get all movie credits with cast
credits = credits.find({}, {"movie_id": 1, "cast": 1, "_id":0})
# Get number of documents in collection for looping
credits_count = credits.count()
# List to store actor dictionarys with movie information
actors = []
# Dictionary to pair actor_id with index in list for appending movies
actorNum = {}
# Iterate through each movie
for i in range(credits_count):
# cast = JSON document of all actors in movie
# movie_id = Movie_id to find in movies collection
# movie = All information about movie
cast = json.loads(credits[i]['cast'])
movie_id = credits[i]['movie_id']
movie = db.movies.find({"id":movie_id})[0]
# Iterate through each cast member
for j in range(len(cast)):
cast_id = cast[j]['id']
cast_name = cast[j]['name']
# If cast member already exists add movie to their list
if cast_id in actorNum:
actors[actorNum[cast_id]]['movies'].append(movie)
else:
actors.append({'actor_id': cast_id, 'name': cast_name, 'movies': [movie]})
actorNum[cast_id] = len(actors)-1
# Insert actors into Mongo
actorCount = db.actors.insert(actors)
print("Successfully imported {} documents into actorMovie.".format(len(actorCount)))