diff --git a/flasky.py b/flasky.py index 5965fcc..8d02c30 100644 --- a/flasky.py +++ b/flasky.py @@ -7,6 +7,7 @@ import bson from datetime import timedelta from functools import update_wrapper +from settings import DATABASE def crossdomain(origin=None, methods=None, headers=None, @@ -52,7 +53,7 @@ def crossdomain(origin=None, methods=None, headers=None, app = Flask(__name__) -client = MongoClient(**{'host': 'localhost', 'port': 27017}) +client = MongoClient(**DATABASE) db = client.showtimes miscObjHandler = lambda obj: ( diff --git a/modules/__init__.py b/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/movies.py b/movies.py new file mode 100644 index 0000000..a528a30 --- /dev/null +++ b/movies.py @@ -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:]) diff --git a/pip_requirements.txt b/pip_requirements.txt index 3b5ed4c..e1c16af 100644 --- a/pip_requirements.txt +++ b/pip_requirements.txt @@ -1,4 +1,5 @@ requests==2.4.0 tornado>=4.0 flask -python-dateutil \ No newline at end of file +python-dateutil +tmdbsimple \ No newline at end of file diff --git a/settings.py b/settings.py index c78f46e..56cb4d2 100644 --- a/settings.py +++ b/settings.py @@ -5,7 +5,8 @@ from tornado.options import define, options import os import logconfig - +TMDB_APIKEY = '4885bf6b6de87bc56599ad0147a0818f' +DATABASE = {'host': 'localhost', 'port': 27017} # Make filepaths relative to settings. path = lambda root, *a: os.path.join(root, *a) ROOT = os.path.dirname(os.path.abspath(__file__))