def __init__(self): logging.info('Remove existing database.') path = Path('./database/movies.db') if path.exists(): path.unlink() engine = db_connect() create_table(engine) self.Session = sessionmaker(bind=engine)
def ingest_data(): movie_request = requests.get( 'https://school.cefalolab.com/assignment/python/movies.csv', verify=False) movie_raw_data = StringIO(movie_request.text) movies = pd.read_csv(movie_raw_data) rating_request = requests.get( 'https://school.cefalolab.com/assignment/python/ratings.csv', verify=False) rating_raw_data = StringIO(rating_request.text) ratings = pd.read_csv(rating_raw_data) engine = db_connect() create_table(engine) Session = sessionmaker(bind=engine) session = Session() for _, row in movies.iterrows(): title = row['title'][:-7] year = row['title'][-7:].replace(' (', '').replace(')', '') movie = session.query(Movie).filter_by(name=title, year=year).first() if movie is not None: genres = row['genres'].split('|') for genre in genres: movie.genre.append(MovieGenre(genre=genre)) filtered_ratings = ratings[ratings['movieId'] == row['movieId']] for _, rating_row in filtered_ratings.iterrows(): movie.rating.append( MovieRating(user_id=rating_row['userId'], rating=rating_row['rating'], timestamp=datetime.datetime.fromtimestamp( rating_row['timestamp']))) try: session.add(movie) session.commit() except: session.rollback() raise else: logging.info( f'{title} ({year}) did not match any movies in the database') session.close()
from flask import Flask, render_template from flask_restful import Api, Resource, abort, request from marshmallow import Schema, fields from sqlalchemy.sql import func from sqlalchemy.orm import scoped_session, sessionmaker from database.models import Movie, MovieRating, db_connect, create_table engine = db_connect() create_table(engine) db_session = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=engine)) app = Flask(__name__) api = Api(app) class MoviePosterSchema(Schema): class Meta: fields = ('src', 'alt_text', 'movie') exclude = ('movie', ) class MovieResultSchema(Schema): class Meta: fields = ('id', 'name', 'year', 'awards', 'nominations', 'image', 'directors', 'producers', 'stars') exclude = ('directors', 'producers', 'stars') image = fields.Nested(MoviePosterSchema)
def __init__(self): engine = db_connect() create_tables(engine) self.Session = sessionmaker(bind=engine)
def session(self): if not self._session: engine = db_connect() create_tables(engine) self._session = sessionmaker(bind=engine)() return self._session