示例#1
0
 def add_movie_to_studio():
     try:
         print("Input movie name:")
         movie_name = input()
         print("Input studio name:")
         studio_name = input()
         if movie_name and studio_name:
             s = Session()
             movie = s.query(Movie).filter_by(name=movie_name).first()
             studio = s.query(Studio).filter_by(name=studio_name).first()
             s.add(studio)
             if (movie is None) or (studio is None):
                 View.display_no_results()
                 s.close()
             else:
                 studio.movies.append(movie)
                 View.display_success()
                 s.commit()
                 s.close()
         else:
             print("Incorrect input")
     except SQLAlchemyError as e:
         View.display_error()
示例#2
0
 def add_actor_to_movie():
     try:
         print("Input movie name:")
         movie_name = input()
         print("Input actor name:")
         actor_name = input()
         if movie_name and actor_name:
             s = Session()
             movie = s.query(Movie).filter_by(name=movie_name).first()
             actor = s.query(Actor).filter_by(full_name=actor_name).first()
             s.add(movie)
             if (movie is None) or (actor is None):
                 View.display_no_results()
                 s.close()
             else:
                 movie.cast.append(actor)
                 View.display_success()
                 s.commit()
                 s.close()
         else:
             print("Incorrect input")
     except SQLAlchemyError as e:
         View.display_error()
示例#3
0
 def create_actor():
     try:
         print("Input name:")
         name = input()
         print("Input gender:")
         gender = input()
         print("Input age:")
         age = int(input())
         if name and gender and age:
             s = Session()
             s.add(Actor(
                 full_name=name,
                 gender=gender,
                 age=age,
             ))
             s.commit()
             s.close()
             View.display_success()
         else:
             print("Incorrect input")
     except SQLAlchemyError:
         View.display_error()
     except ValueError:
         print("Incorrect input")
示例#4
0
def add_song_to_playlist(user, playlist_id, song_id):
    session = Session()
    playlist = crud_playlist.get_playlist(session, playlist_id=playlist_id)
    if not playlist:
        session.close()
        abort(404)
    if playlist.user_id != user.id:
        session.close()
        abort(403)

    song = crud_song.get_song(session, song_id=song_id)
    if not song:
        session.close()
        abort(404)

    playlist.songs.append(song)
    playlist.size += 1
    crud_playlist.update_playlist(session, playlist)
    session.close()

    return Response(status=200)
示例#5
0
def delete_playlist(user, playlist_id):
    session = Session()
    playlist = crud_playlist.get_playlist(session, playlist_id=playlist_id)
    if not playlist:
        session.close()
        abort(404)
    if playlist.user_id != user.id:
        session.close()
        abort(403)

    crud_playlist.delete_playlist(session, playlist_id)
    session.close()

    return Response(status=200)
示例#6
0
def update_song(user, song_id):
    form = request.form

    session = Session()
    song = crud_song.get_song(session, song_id=song_id)

    if not song:
        session.close()
        abort(404)
    if song.user_id != user.id:
        session.close()
        abort(403)

    song.title = form['title'] if 'title' in form else song.title
    song.artist = form['artist'] if 'artist' in form else song.artist
    song.album = form['album'] if 'album' in form else song.album
    song.release_year = form[
        'releaseYear'] if 'releaseYear' in form else song.release_year
    if 'file' in request.files:
        song_file = request.files['file']

        import os
        filename, file_extension = os.path.splitext(song_file.filename)

        if file_extension != ".wav" and file_extension != ".mp3":
            session.close()
            abort(400)

        song_new_filename = utils.generate_uuid()

        song.url = aws.upload_song(song_new_filename, file_extension,
                                   song_file)

    crud_song.update_song(session, song)
    session.close()

    return Response(status=200)
示例#7
0
def update_playlist(user, playlist_id):
    data = request.get_json()
    playlist_name = data['name']

    session = Session()
    playlist = crud_playlist.get_playlist(session, playlist_id=playlist_id)
    if not playlist:
        session.close()
        abort(404)
    if playlist.user_id != user.id:
        session.close()
        abort(403)

    if playlist_name:
        playlist.name = playlist_name
        crud_playlist.update_playlist(session, playlist)

    session.close()
    return Response(status=200)
示例#8
0
def get_token():
    data = request.get_json()

    session = Session()
    user = crud_user.get_user_by_email(session, data['email'])

    if not user:
        session.close()
        abort(401)

    input_hash = utils.hash_password(data['password'], user.password_salt)
    right_hash = user.password_hashed

    if input_hash != right_hash:
        session.close()
        abort(401)

    result = {'token': user.auth_token}
    session.close()

    return jsonify(result)
示例#9
0
def login():
    """
    Gives an anonymous user the login form and checks their credentials against users in the database.
    """
    if current_user.is_authenticated:
        return redirect(url_for('index'))

    s = Session()
    form = LoginForm()

    if form.validate_on_submit():
        user = s.query(User).filter_by(username=form.username.data).first()
        if user is None or not user.check_password(form.password.data):
            flash('Invalid username or password')
            s.close()
            return redirect(url_for('login'))
        login_user(user, remember=form.remember_me.data)
        s.close()
        return redirect(url_for('index'))

    s.close()
    return render_template('login.html', form=form)
示例#10
0
def run():

    start_time = datetime.now()

    # turn on the program flag
    global program_flag
    program_flag = True

    # program kill switch
    def signal_handler(number, frame):
        global program_flag
        program_flag = False

    # kill signal handler
    signal.signal(signal.SIGTERM, signal_handler)

    # Create Reddit Instance
    reddit = praw.Reddit(
        user_agent=config('REDDIT_USER_AGENT'),
        client_id=config('REDDIT_CLIENT_ID'),
        client_secret=config('REDDIT_CLIENT_SECRET'),
        username=config('REDDIT_USERNAME'),
        password=config('REDDIT_PASSWORD'),
    )
    subreddit = reddit.subreddit("wallstreetbets")

    # Parse XML File
    tree = ET.parse("StockListComplete.xml")
    root = tree.getroot()

    Base = declarative_base()
    s = Session()

    comment_stream = subreddit.stream.comments(skip_existing=True)

    for comment in comment_stream:

        if (program_flag == False):
            comment_stream.close()

        cut_off = datetime.now() - timedelta(hours=24)
        s.execute(delete(Stock).where(Stock.date < cut_off))

        records = root.findall('record')
        if len(records) > 0:
            for record in root.findall('record'):
                tickers = list(record.iter('StockTicker'))
                fullnames = list(record.iter('StockFullname'))
                sectors = list(record.iter('StockSector'))
                if len(tickers) > 0:
                    ticker = tickers[0].text
                else:
                    ticker = ''
                if len(fullnames) > 0:
                    fullname = fullnames[0].text
                else:
                    fullname = ''
                if len(sectors) > 0:
                    sector = sectors[0].text
                else:
                    sector = ''
                if (ticker != '' and ticker in comment.body.split()) or (
                        fullname != '' and fullname in comment.body):
                    new_stock = generateStock(ticker, fullname, sector)
                    s.add(new_stock)

        s.commit()
    s.close()

    end_time = datetime.now()

    print('Started: ', start_time)
    print('Ended: ', end_time)
示例#11
0
    letters = string.ascii_lowercase
    ticker = ''.join(
        random.choice(letters) for i in range(random.randint(0, 4)))

    if len(ticker) == 1 or len(ticker) == 2:
        ticker = ''.join(
            random.choice(letters) for i in range(random.randint(3, 4)))

    name = r.get_random_word()

    stockList.append({
        "ticker":
        ticker,
        "name":
        name,
        "sector":
        sectorList[random.randint(0,
                                  len(sectorList) - 1)]
    })

max_num_of_days_old = 7
for x in range(8000):
    r = random.randint(0, len(stockList) - 1)
    s.add(
        generateStock(stockList[r]["ticker"], stockList[r]["name"],
                      stockList[r]["sector"],
                      random.randint(0, max_num_of_days_old)))

s.commit()
s.close()