<commit_before>	import json
import youtube
from flask import Blueprint, request, jsonify
from models import Playlist, Video
from database import db_session

api = Blueprint("api", __name__, url_prefix="/api")


class ExistenceError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


@api.route("/create_playlist/", methods=["POST"])
def create_playlist():
    name = request.form.get("name")

    if name.strip() == "":
        return jsonify({"error": "You must specify a name for this playlist"})

    p = Playlist(name)
    db_session.add(p)
    db_session.commit()

    return jsonify({"playlist_id": p.id})


@api.route("/search_videos/")
def search_videos():
    search_term = request.args.get("search_term")
    page = request.args.get("page")

    if search_term.strip() == "":
        return jsonify({"error": "You must specify a search term"})

    videos = youtube.search_for_videos(search_term)

    return jsonify(videos)

@api.route("/add_video/", methods=["POST"])
def add_video():
    playlist_id = request.form.get("playlist_id")
    if playlist_id.strip() == "":
        return jsonify({'error': "You must specify a playlist ID for this video."})

    slug = request.form.get("slug")
    if slug.strip() == "":
        return jsonify({'error': "You must specify a slug for this video."})

    thumbnail_url = request.form.get("thumbnail_url")
    if thumbnail_url.strip() == "":
        return jsonify({'error': "You must specify a thumbnail for this video."})

    title = request.form.get("title")
    if title.strip() == "":
        return jsonify({'error': "You must specify a title for this video."})

    v = Video(playlist_id, slug, thumbnail_url, title)
    db_session.add(v)
    db_session.commit()

    return jsonify({"success": True})

@api.route("/<vote/", methods=["POST"])
def vote(video_id, up_down):
    video = Playlist.query.get(video_id)

    if video is None:
        raise ExistenceError("Video does not exist.")

    if up_down == "up":
        video.rank += 1
    elif up_down == "down":
        video.rank -= 1
    else:
        raise TypeError("Please either upvote or downvote this video.")

    db_session.commit()

@api.route("/delete")
def remove_video(video_id):
    video = Playlist.query.get(video_id)

    if video is None:
        raise ExistenceError("Video does not exist.")

    db_session.delete(video)
    db_session.commit()
<commit_msg>modified api.py for vote method<commit_after>	import json
import youtube
from flask import Blueprint, request, jsonify
from models import Playlist, Video
from database import db_session

api = Blueprint("api", __name__, url_prefix="/api")


class ExistenceError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


@api.route("/create_playlist/", methods=["POST"])
def create_playlist():
    name = request.form.get("name")

    if name.strip() == "":
        return jsonify({"error": "You must specify a name for this playlist"})

    p = Playlist(name)
    db_session.add(p)
    db_session.commit()

    return jsonify({"playlist_id": p.id})


@api.route("/search_videos/")
def search_videos():
    search_term = request.args.get("search_term")
    page = request.args.get("page")

    if search_term.strip() == "":
        return jsonify({"error": "You must specify a search term"})

    videos = youtube.search_for_videos(search_term, page=page)

    return jsonify(videos)


@api.route("/add_video/", methods=["POST"])
def add_video():
    playlist_id = request.form.get("playlist_id")
    if playlist_id.strip() == "":
        return jsonify({'error': "You must specify a playlist ID for this video."})

    slug = request.form.get("slug")
    if slug.strip() == "":
        return jsonify({'error': "You must specify a slug for this video."})

    thumbnail_url = request.form.get("thumbnail_url")
    if thumbnail_url.strip() == "":
        return jsonify({'error': "You must specify a thumbnail for this video."})

    title = request.form.get("title")
    if title.strip() == "":
        return jsonify({'error': "You must specify a title for this video."})

    v = Video(playlist_id, slug, thumbnail_url, title)
    db_session.add(v)
    db_session.commit()

    return jsonify({"success": True})

@api.route("/<vote/", methods=["POST"])
def vote(video_id, up_down):
    video = Playlist.query.get(video_id)

    if video is None:
        raise ExistenceError("Video does not exist.")

    if up_down == "up":
        video.rank += 1
    elif up_down == "down":
        video.rank -= 1
    else:
        raise TypeError("Please either upvote or downvote this video.")

    db_session.commit()

    return jsonify(video.rank)

@api.route("/delete")
def remove_video(video_id):
    video = Playlist.query.get(video_id)

    if video is None:
        raise ExistenceError("Video does not exist.")

    db_session.delete(video)
    db_session.commit()

{'fraction_non_alphanumeric': 0.14337060702875398, 'fraction_numerical': 0.0007987220447284345, 'mean_word_length': 3.3708551483420592, 'pattern_counts': {'":': 24, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4448185', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1568, 'n_words': 415}