コード例 #1
0
ファイル: pipelines.py プロジェクト: imtiaz101325/movie_api
    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)
コード例 #2
0
ファイル: ingest.py プロジェクト: imtiaz101325/movie_api
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()
コード例 #3
0
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)
コード例 #4
0
 def __init__(self):
     engine = db_connect()
     create_tables(engine)
     self.Session = sessionmaker(bind=engine)
コード例 #5
0
 def session(self):
     if not self._session:
         engine = db_connect()
         create_tables(engine)
         self._session = sessionmaker(bind=engine)()
     return self._session