-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_db.py
More file actions
94 lines (76 loc) · 3.26 KB
/
sync_db.py
File metadata and controls
94 lines (76 loc) · 3.26 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
import os
import readline
from pprint import pprint
from flask import *
from app import *
from app.movies.models import Movie
from app.movies.models import Genre
from app.movies.models import MovieGenre
from app.movies.models import Person
from app.movies.models import Cast
import tmdb3
import csv
import tmdbsimple as tmdb
tmdb.API_KEY ='c9a18ea34091a088d00dfa65325c19db'
os.environ['PYTHONINSPECT'] = 'True'
# lets delete the database for now.
db.drop_all()
db.create_all()
db.session.commit()
#exit()
with open('/Users/marktooth/Projects/python-movies2/dvds.csv', 'rb') as csvfile:
movies = csv.DictReader(csvfile)
for mov in movies:
if mov['IMDB_ID'] == '':
pass
# we have friends which crashes tmdb
print 'manual addimg of ' + mov['Title']
dbMov = Movie(mov['Title'], mov['IMDB_ID'],None, None, None, mov['DVD_ID'])
db.session.add(dbMov)
db.session.commit()
else:
external_source = 'imdb_id'
find = tmdb.Find(mov['IMDB_ID'])
response = find.info(external_source=external_source)
if len(response['movie_results']):
# we have a movie
movie = response['movie_results'][0]
movie = tmdb.Movies(movie['id'])
m = movie.info()
dbMov = Movie(m['original_title'], mov['IMDB_ID'], m['tagline'][0:297]+'...', m['release_date'], 'http://image.tmdb.org/t/p/w396' + m['poster_path'], mov['DVD_ID'])
db.session.add(dbMov)
db.session.commit()
print 'added ' + m['original_title']
# now to add the code to add genres...
for genre in m['genres']:
# test if genre exists.
dbGenre = Genre(genre['name'], "tmdb3")
db.session.add(dbGenre)
# we commit to get ids :)
db.session.commit()
dbMovieGenre = MovieGenre(dbMov.id, dbGenre.id)
db.session.add(dbMovieGenre)
db.session.commit()
# need to loop over people now :)
cast = movie.credits()
order = 1
for c in cast['cast']:
# cast._populate_images()
# image = ''
# if 'geturl' in dir(cast.profile):
# image = cast.profile.geturl()
dbPerson = Person(c['name'], '', '', '', '')
db.session.add(dbPerson)
db.session.commit()
dbCast = Cast(dbPerson.id, dbMov.id, c['character'], c['name'], order , 'TMDb3', 'cast')
db.session.add(dbCast)
db.session.commit()
order = mov['DVD_ID']
elif len(response['tv_results']):
tv = response['tv_results'][0]
tv = tmdb.TV(tv['id']).info()
dbMov = Movie(tv['name'], mov['IMDB_ID'], tv['overview'][0:297]+'...', tv['first_air_date'], 'http://image.tmdb.org/t/p/w396' + tv['poster_path'], mov['DVD_ID'])
db.session.add(dbMov)
db.session.commit()
print 'added ' + tv['name']