Esempio n. 1
0
def new_video(session):
    data = request.get_json()

    if "url" not in data or "forced" not in data:
        return error(errcodes.missing_parameters)

    v = Video.query.filter(Video.session_id == session,
            Video.url == data["url"]).first()
    if v is not None:
        return error(errcodes.resource_exists)

    v = Video(
            url=data["url"],
            forced=data["forced"],
            session_id=session,
    )

    # client validator
    if "client_id" in data:
        c = Client.query.filter(Client.session_id == session,
                Client.id == data["client_id"]).first()

        if c is None:
            return error(errcodes.not_found)

        v.client_id = c.id

        vcnt = Video.query.filter(Video.session_id == session,
                Video.client_id == data["client_id"]).count()
        ccnt = Client.query.filter(Client.session_id == session).count()

        if c.blocked:
            return error(errcodes.forbidden_temp)
        elif ccnt > 1 and vcnt >= 3:
            # block user
            c.blocked = True

            # add changes to db
            db_session.add(c)
            db_session.commit()

            return error(errcodes.forbidden_temp)

    def metafetcher(db_session, v):
        extract = NameExtractor(v.url).extract()
        v.title, v.related_url = extract
        db_session.add(v)
        db_session.commit()
    Thread(target=metafetcher, args=(db_session, v)).start()

    return jsonify(v.to_dict())
Esempio n. 2
0
def new_client(session):
    data = request.get_json()

    if "name" not in data:
        return error(errcodes.missing_parameter)

    c = Client.query.filter(Client.session_id == session, Client.name == data["name"]).first()
    if c is not None:
        return error(errcodes.resource_exists)

    c = Client(
            session_id=session,
            name=data["name"]
    )

    db_session.add(c)
    try:
        db_session.commit()
    except:
        return error(errcodes.internal)

    return jsonify(c.to_dict())