def checkUser():
    headers = get_headers()
    req = requests.get('https://www.deviantart.com/api/v1/oauth2/user/whoami', headers=headers)
    response = req.content
    response =  json.loads(response)
    usrquery = User.query.filter_by(useruuid=response['userid']).first()
    if usrquery is None:
        user = User(response['userid'], response['usericon'], response['username'])
        db.session.add(user)
        db.session.commit()
        usrquery = User.query.filter_by(useruuid=response['userid']).first()
    else:
        if usrquery.usericon != response['usericon'] or usrquery.username != response['username']:
            if usrquery.usericon != response['usericon']:
                usrquery.usericon = response['usericon']
            if usrquery.username != response['username']:
                usrquery.username = response['username']
            db.session.commit()
    timestamp = time.time()
    timestamp = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
    try:
        ipaddress = request.headers['X-Real-IP'] # Pythonanywhere hack
    except:
        ipaddress = request.remote_addr
    login = LoginTimestamp(usrquery.id, response['username'], ipaddress, timestamp)
    db.session.add(login)
    db.session.commit()
    session['user'] = {}
    session['user']['usericon'] = response['usericon']
    session['user']['userid'] = response['userid']
    session['user']['username'] = response['username']
    session['user']['loggedIn'] = True
    session['user']['isModerator'] = usrquery.isModerator()
def art():
    headers = get_headers()
    uuid = request.args.get('deviationid')
    if uuid is None:
        return jsonify(error=True,message="Invalid Deviation ID")
    req = requests.get('https://www.deviantart.com/api/v1/oauth2/deviation/{}'.format(uuid), headers=headers)
    response = req.content
    return jsonify(error=False,art=json.loads(response))
def favorite():
    headers = get_headers()
    uuid = request.args.get('favoriteid')
    usr = request.args.get('username')
    mature = request.args.get('mature', False)
    if uuid and usr is None:
        return jsonify(error=True)
    results = []
    has_more = True
    offset = 0
    while has_more:
        parameters = {'username': usr, 'offset': offset, 'mature_content': mature}
        req = requests.get('https://www.deviantart.com/api/v1/oauth2/collections/{}'.format(uuid),params=parameters, headers=headers)
        response = req.content
        has_more = json.loads(response)['has_more']
        results = results + json.loads(response)['results']
        if has_more:
            offset = json.loads(response)['next_offset']
            print offset
    return jsonify(error=False,favorite=results)
Esempio n. 4
0
def checkUser():
    headers = get_headers()
    req = requests.get('https://www.deviantart.com/api/v1/oauth2/user/whoami',
                       headers=headers)
    response = req.content
    response = json.loads(response)
    usrquery = User.query.filter_by(useruuid=response['userid']).first()
    if usrquery is None:
        user = User(response['userid'], response['usericon'],
                    response['username'])
        db.session.add(user)
        db.session.commit()
        usrquery = User.query.filter_by(useruuid=response['userid']).first()
    else:
        if usrquery.usericon != response[
                'usericon'] or usrquery.username != response['username']:
            if usrquery.usericon != response['usericon']:
                usrquery.usericon = response['usericon']
            if usrquery.username != response['username']:
                usrquery.username = response['username']
            db.session.commit()
    timestamp = time.time()
    timestamp = datetime.datetime.fromtimestamp(timestamp).strftime(
        '%Y-%m-%d %H:%M:%S')
    try:
        ipaddress = request.headers['X-Real-IP']  # Pythonanywhere hack
    except:
        ipaddress = request.remote_addr
    login = LoginTimestamp(usrquery.id, response['username'], ipaddress,
                           timestamp)
    db.session.add(login)
    db.session.commit()
    session['user'] = {}
    session['user']['usericon'] = response['usericon']
    session['user']['userid'] = response['userid']
    session['user']['username'] = response['username']
    session['user']['loggedIn'] = True
    session['user']['isModerator'] = usrquery.isModerator()
def whoami():
    headers = get_headers()
    req = requests.get('https://www.deviantart.com/api/v1/oauth2/user/whoami', headers=headers)
    response = req.content
    return jsonify(whoami=json.loads(response))