DAcollector.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:DeviantArt-Extractor 作者: volfegan 项目源码 文件源码
def get_token():
    """Returns the token inside deviantartdb.sqlite. If no token exists on database or it is expired, fetch a new one from DeviantArt"""
    token_url='https://www.deviantart.com/oauth2/token'
    conn = sqlite3.connect('deviantartdb.sqlite')
    cur = conn.cursor()
    cur.execute('''SELECT token FROM deviantart_session''')
    token=None
    for row in cur: #if the table on token FROM deviantart_session is empty, this for loop step is just skipped
        try:
            token = json.loads(row[0])
            print 'Adquiring token from deviantartdb.sqlite database...'
        except:
            print 'Adquiring token from deviantartdb.sqlite FAIL!'"\n"

    if token==None:
        print 'No token inside deviantartdb.sqlite'"\n"'Adquiring token from Deviantart...'
        token = deviantart_session.fetch_token(token_url=token_url, client_id=client_id, client_secret=client_secret)
    else:
        timenow=time.time()
        if timenow>token['expires_at']:
            print 'The token on database is expired. Adquiring new access token from Deviantart...'
            token = deviantart_session.fetch_token(token_url=token_url, client_id=client_id, client_secret=client_secret)

    cur.close()
    print 'Token:\n', json.dumps(token, indent=3)
    print 'Token expires at:', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(json.dumps(token['expires_at'])))),"\n"
    token_saver(token)
    return token
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号