#NEWLINE# def update_github( ) : #NEWLINE# #INDENT# elsewhere = db . all ( "SELECT user_id FROM ELSEWHERE WHERE platform='github' ORDER BY id;" ) #NEWLINE# url = 'https://api.github.com/user/%s' #NEWLINE# client_id = os . environ . get ( 'GITHUB_CLIENT_ID' ) #NEWLINE# client_secret = os . environ . get ( 'GITHUB_CLIENT_SECRET' ) #NEWLINE# for user_id in elsewhere : #NEWLINE# #INDENT# response = requests . get ( ( url % user_id ) , params = { #NEWLINE# #INDENT# 'client_id' : client_id , #NEWLINE# 'client_secret' : client_secret , #NEWLINE# #UNINDENT# } ) #NEWLINE# nremaining = int ( response . headers [ 'X-RATELIMIT-REMAINING' ] ) #NEWLINE# reset = int ( response . headers [ 'X-RATELIMIT-RESET' ] ) #NEWLINE# now = time . time ( ) #NEWLINE# print ( nremaining , reset , now , ( reset - now ) , end = ' ' ) #NEWLINE# status = response . status_code #NEWLINE# if ( status == 200 ) : #NEWLINE# #INDENT# user_info = response . json ( ) #NEWLINE# for ( k , v ) in user_info . items ( ) : #NEWLINE# #INDENT# user_info [ k ] = unicode ( v ) #NEWLINE# #UNINDENT# assert ( user_id == user_info [ 'id' ] ) #NEWLINE# db . one ( "\n                UPDATE elsewhere\n                SET user_info=%s\n                WHERE user_id=%s\n                AND platform='github'\n                RETURNING id\n            " , ( user_info , user_id ) ) #NEWLINE# print ( 'updated {} ({})' . format ( user_info [ 'login' ] , user_id ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( status == 404 ) : #NEWLINE# #INDENT# db . one ( "\n                UPDATE elsewhere\n                SET user_info=NULL\n                WHERE user_id=%s\n                AND platform='github'\n                RETURNING id\n            " , ( user_id , ) ) #NEWLINE# print ( 'orphan found: {}' . format ( user_id ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# print ( response . status_code , response . text ) #NEWLINE# #UNINDENT# #UNINDENT# sleep_for = 0.5 #NEWLINE# if ( nremaining == 0 ) : #NEWLINE# #INDENT# sleep_for = ( reset - time . time ( ) ) #NEWLINE# sleep_for += 10 #NEWLINE# #UNINDENT# time . sleep ( sleep_for )