sipp11
10 years ago
5 changed files with 152 additions and 3 deletions
@ -0,0 +1,146 @@ |
|||||||
|
from __future__ import absolute_import, print_function |
||||||
|
from pymongo import MongoClient |
||||||
|
from settings import DATABASE, TMDB_APIKEY |
||||||
|
from datetime import date |
||||||
|
import tmdbsimple as tmdb |
||||||
|
import sys |
||||||
|
import time |
||||||
|
|
||||||
|
|
||||||
|
client = MongoClient(**DATABASE) |
||||||
|
db = client.showtimes |
||||||
|
tmdb.API_KEY = TMDB_APIKEY |
||||||
|
|
||||||
|
|
||||||
|
def update_showtimes(_id, movie): |
||||||
|
key = {'_id': _id} |
||||||
|
update_dict = { |
||||||
|
'movie_id': movie['id'], |
||||||
|
'poster_path': movie['poster_path'], |
||||||
|
} |
||||||
|
result = db.showtimes.update(key, {'$set': update_dict}, True) |
||||||
|
## try adding to movie if not exist |
||||||
|
add_movie(movie) |
||||||
|
return result |
||||||
|
|
||||||
|
|
||||||
|
def add_movie(movie): |
||||||
|
movie_collection = db.movies |
||||||
|
q = { |
||||||
|
'id': movie['id'] |
||||||
|
} |
||||||
|
_m = movie_collection.find_one(q) |
||||||
|
if _m: |
||||||
|
return _m |
||||||
|
''' |
||||||
|
## adding new one with full info |
||||||
|
{ |
||||||
|
u'poster_path': u'/otcv3wWKz0vJyicOZgz2AheJ5UV.jpg', |
||||||
|
u'production_countries': [ |
||||||
|
{u'iso_3166_1': u'US', u'name': u'United States of America'} |
||||||
|
], |
||||||
|
u'revenue': 0, |
||||||
|
u'overview': u'........................', |
||||||
|
u'id': 245891, |
||||||
|
u'genres': [ |
||||||
|
{u'id': 28, u'name': u'Action'}, |
||||||
|
{u'id': 53, u'name': u'Thriller'}], |
||||||
|
u'title': u'John Wick', |
||||||
|
u'tagline': u'', |
||||||
|
u'vote_count': 5, |
||||||
|
u'homepage': u'', |
||||||
|
u'belongs_to_collection': None, |
||||||
|
u'status': u'Released', |
||||||
|
u'spoken_languages': [], |
||||||
|
u'imdb_id': u'tt2911666', |
||||||
|
u'adult': False, |
||||||
|
u'backdrop_path': u'/umC04Cozevu8nn3JTDJ1pc7PVTn.jpg', |
||||||
|
u'production_companies': [], |
||||||
|
u'release_date': u'2014-10-24', |
||||||
|
u'popularity': 3.044875809, |
||||||
|
u'original_title': u'John Wick', |
||||||
|
u'budget': 0, |
||||||
|
u'vote_average': 8.4, |
||||||
|
u'runtime': 0} |
||||||
|
''' |
||||||
|
identity = tmdb.Movies(movie['id']) |
||||||
|
m_info = identity.info() |
||||||
|
new_movie = movie_collection.insert(m_info) |
||||||
|
return new_movie |
||||||
|
|
||||||
|
|
||||||
|
def find_movie(qword): |
||||||
|
''' |
||||||
|
find in db.movies first, then fallback to tmdb |
||||||
|
''' |
||||||
|
q = { |
||||||
|
'title': qword |
||||||
|
} |
||||||
|
_mx = db.movies.find_one(q) |
||||||
|
if _mx: |
||||||
|
print('FOUND') |
||||||
|
print(_mx) |
||||||
|
else: |
||||||
|
print('NOT FOUND') |
||||||
|
|
||||||
|
return [] |
||||||
|
|
||||||
|
''' |
||||||
|
return something like this if found |
||||||
|
{ |
||||||
|
u'poster_path': u'/otcv3wWKz0vJyicOZgz2AheJ5UV.jpg', |
||||||
|
u'title': u'John Wick', |
||||||
|
u'release_date': u'2014-10-24', |
||||||
|
u'popularity': 3.044875809, |
||||||
|
u'original_title': u'John Wick', |
||||||
|
u'backdrop_path': u'/umC04Cozevu8nn3JTDJ1pc7PVTn.jpg', |
||||||
|
u'vote_count': 5, |
||||||
|
u'adult': False, |
||||||
|
u'vote_average': 8.4, |
||||||
|
u'id': 245891 |
||||||
|
} |
||||||
|
''' |
||||||
|
search = tmdb.Search() |
||||||
|
response = search.movie(query=qword) |
||||||
|
if not search.results: |
||||||
|
return [] |
||||||
|
''' |
||||||
|
dang we pick the first match.. |
||||||
|
since I don't know how to deal with multiple result yet |
||||||
|
''' |
||||||
|
return search.results[0] |
||||||
|
|
||||||
|
|
||||||
|
def main(argv): |
||||||
|
''' |
||||||
|
Mainly we are getting movie name from db.showtimes |
||||||
|
& check if there is any info on db.movies. |
||||||
|
If not, we are trying to fetch for new info |
||||||
|
''' |
||||||
|
print('Fetching Movie without information') |
||||||
|
q = { |
||||||
|
'$or': [ |
||||||
|
{'movie_id': {'$exists': False}}, |
||||||
|
{'movie_id': ''}, |
||||||
|
{'poster_path': ''}, |
||||||
|
] |
||||||
|
} |
||||||
|
result = db.showtimes.find(q) |
||||||
|
cannot_find = [] |
||||||
|
for _ in result: |
||||||
|
# skip if we know that we aren't going get any info anyway |
||||||
|
if _['movie'] in cannot_find: |
||||||
|
print('Nah -- you will not find this: ', _['movie']) |
||||||
|
continue |
||||||
|
|
||||||
|
time.sleep(1) |
||||||
|
found = find_movie(_['movie']) |
||||||
|
|
||||||
|
if found: |
||||||
|
update_showtimes(_['_id'], found) |
||||||
|
else: |
||||||
|
cannot_find.append(_['movie']) |
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__': |
||||||
|
main(sys.argv[1:]) |
Loading…
Reference in new issue