示例#1
0
def api_stats_general(stat):
    if stat not in stats.ALL_STATS and stat != "all":
        error = {
            "message": "This stat does not exist"
        }
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )

    # Force lookup only last 4 months
    time = datetime.now() - timedelta(days=121)
    data = dbutils.getSurseSince(None, "all", datetime.timestamp(time))

    result = {}
    if stat == "all":
        for name, func in stats.ALL_STATS.items():
            result[name] = func(data)
    else:
        result = stats.ALL_STATS[stat](data)

    return app.response_class(
        response=json.dumps(result),
        status=200,
        mimetype='application/json'
    )
示例#2
0
def api_stats_user(stat, nickname):
    user = User.query.filter(User.nickname == nickname).first()
    load_since = request.args.get('load_since')

    if stat not in stats.ALL_STATS and stat != "all":
        error = {
            "message": "This stat does not exist"
        }
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )

    if user is None:
        error = {
            "message": None
        }
        error["message"] = "This user does not exist"
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )

    if load_since is None:
        data = dbutils.getSurse(user, "all")
    else:
        data = dbutils.getSurseSince(user, "all", load_since)
    result = {}
    if stat == "all":
        for name, func in stats.ALL_STATS.items():
            result[name] = func(data)
    else:
        result = stats.ALL_STATS[stat](data)

    if dbutils.needsUpdate(user, "all"):
        dbutils.updateThreaded(user)

        return Response(json.dumps(result),
                        status=200,
                        mimetype='application/json')

    return app.response_class(
        response=json.dumps(result),
        status=200,
        mimetype='application/json'
    )
示例#3
0
def prob_user(nickname):
    user = User.query.filter(User.nickname == nickname).first()

    # In cazul in care userul cerut nu exista
    if user is None:
        app.logger.debug("Nu am gasit user cu nickname", nickname)
        return app.response_class(
            response=render_template('404.html'),
            status=404
        )

    app.logger.debug("Gasit username cu nickname %s", nickname)

    if dbutils.needsUpdate(user, "all"):
        app.logger.debug("%s are nevoie de update de surse", user.nickname)
        dbutils.updateThreaded(user)

        # Return old data to the user before we finish updating
        return render_template('prob.html',
                               updating=True,
                               user=user)

    return render_template('prob.html',
                           updating=False,
                           user=user)
示例#4
0
def api_getuser(user):
    # In cazul in care userul cerut nu exista
    if not dbutils.userExists(user):
        error = {
            "message": None
        }
        error["message"] = "This user does not exist"
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )
    # Pentru a creea un raspuns folosind JSON
    # @sites = usernameurile de pe siteuri
    # @id = id-ul din baza de date
    # @fullname = numele specificat de user
    user = dbutils.getUser(user)
    response = {
        "sites": {},
        "id": user.id,
        "fullname": user.fullname
    }
    # Pentru a adauga la fiecare site
    # username si data ultimei actualizari
    for site in SITES:
        if user[site] is None:
            response["sites"][site] = {
                "username": "",
                "last_check": -1
            }
        else:
            response["sites"][site] = {
                "username": user[site],
                "last_check": user["last_" + site]
            }

    # Pentru a specifica browserului ca este un raspuns JSON
    return app.response_class(
        response=json.dumps(response),
        status=200,
        mimetype='application/json'
    )
示例#5
0
def api_users_forcereload(nickname, site):
    load_since = request.args.get('load_since')

    user = User.query.filter(User.nickname == nickname).first()
    # In cazul in care userul cerut nu exista
    if user is None:
        error = {
            "message": None
        }
        error["message"] = "This user does not exist"
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )

    # In cazul in care site-ul cerut nu exista
    if site not in SITES_ALL:
        error = {
            "message": None
        }
        error["message"] = "Site dosen't exist or is not tracked"
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )

    # Pentru a creea un raspuns folosind JSON
    # @updating = daca va fi actualizat in viitorul apropiat
    # @result = problemele userului de pe site-ul cerut
    response = {
        "updating": True
    }
    dbutils.updateThreaded(user)
    return app.response_class(
        response=json.dumps(response),
        status=200,
        mimetype='application/json'
    )
示例#6
0
def api_getuserlist():
    # Pentru a creea un raspuns folosind JSON
    users = User.query.all()
    response = []
    for user in users:
        response.append(user.nickname)

    # Pentru a specifica browserului ca este un raspuns JSON
    return app.response_class(
        response=json.dumps(response),
        status=200,
        mimetype='application/json'
    )
示例#7
0
def profile_username(nickname):
    user = dbutils.getUser(nickname)
    if user is None:
        return app.response_class(
            response=render_template('404.html'),
            status=404
        )

        app.logger.debug("Username %s nu exista", nickname)
        return redirect(url_for('index'))
    if current_user.is_authenticated:
        if current_user.nickname == user.nickname:
            return redirect(url_for('index'))
        else:
            return render_template('index.html',
                                   SITES=SITES_ALL,
                                   user=user)
    else:
        return render_template('index.html',
                               SITES=SITES_ALL,
                               user=user)
示例#8
0
def api_users(nickname, site):
    # Adaugam debug pentru ca sa vedem cat de mult dureaza cu toolbar
    debug = False
    if site == "debug" and app.debug is True:
        site = "all"
        debug = True
    load_since = request.args.get('load_since')

    # In cazul in care site-ul cerut nu exista
    if site not in SITES_ALL:
        error = {
            "message": None
        }
        error["message"] = "Site dosen't exist or is not tracked"
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )

    user = User.query.filter(User.nickname == nickname).first()
    # In cazul in care userul cerut nu exista
    if user is None:
        error = {
            "message": None
        }
        error["message"] = "This user does not exist"
        return app.response_class(
            response=json.dumps(error),
            status=404,
            mimetype='application/json'
        )

    # Pentru a creea un raspuns folosind JSON
    # @updating = daca va fi actualizat in viitorul apropiat
    # @result = problemele userului de pe site-ul cerut
    response = {
        "updating": dbutils.needsUpdate(user, site),
        "result": {}
    }

    if load_since is None:
        app.logger.debug("Incaracam toate sursele...")
        data = dbutils.getSurse(user, site)
    else:
        app.logger.debug("Incarcam sursele mai noi decat %s", load_since)
        data = dbutils.getSurseSince(user, site, load_since)
    result = []
    for i in data:
        result.append(i.__json__())
    response["result"] = result
    db.session.commit()

    if response["updating"]:
        dbutils.updateThreaded(user)
        if debug:
            return render_template('debug.html', data=response)

        return Response(json.dumps(response),
                        status=200,
                        mimetype='application/json')

    if debug:
        return render_template('debug.html', data=response)
    # Pentru a specifica browserului ca este un raspuns JSON
    return app.response_class(
        response=json.dumps(response),
        status=200,
        mimetype='application/json'
    )