@ -41,26 +41,36 @@ def post_webhook(msg):
except :
except :
return False
return False
def get_activity ( ) :
def get_latest_tipped_posts ( ) :
posts = Post . select ( )
key_name = ' latest_tips '
w = Wallet ( )
tipped_posts = rw_cache ( key_name , None , 1200 )
data = { }
for p in posts :
data [ p . timestamp ] = { ' type ' : ' post ' , ' post ' : p }
for tx in w . incoming_transfers ( p . account_index ) :
if ' timestamp ' in tx :
data [ tx [ ' timestamp ' ] ] = { ' type ' : ' tx ' , ' post ' : p }
dates = sorted ( data , reverse = True )
if not tipped_posts :
new_data = [ ]
new_data = [ ]
for d in dates :
w = Wallet ( )
new_data . append ( data [ d ] [ ' post ' ] )
data = { }
for acc in w . accounts ( ) :
txes = w . transfers ( acc )
if ' in ' in txes :
for tx in txes [ ' in ' ] :
p = Post . select ( ) . where (
Post . account_index == acc
) . first ( )
if p :
data [ tx [ ' timestamp ' ] ] = p
return new_data [ 0 : 20 ]
dates = sorted ( data , reverse = True )
for d in dates :
if not data [ d ] in new_data :
new_data . append ( data [ d ] )
tipped_posts = rw_cache ( key_name , new_data , 1200 )
return tipped_posts
# Use hacky filesystem cache since i dont feel like shipping redis
# Use hacky filesystem cache since i dont feel like shipping redis
def rw_cache ( key_name , data = None ) :
def rw_cache ( key_name , data = None , diff_seconds = 3600 ):
pickle_file = path . join ( config . DATA_FOLDER , f ' { key_name } .pkl ' )
pickle_file = path . join ( config . DATA_FOLDER , f ' { key_name } .pkl ' )
try :
try :
if path . isfile ( pickle_file ) :
if path . isfile ( pickle_file ) :
@ -70,7 +80,7 @@ def rw_cache(key_name, data=None):
diff = now - mtime
diff = now - mtime
# If pickled data file is less than an hour old, load it and render page
# If pickled data file is less than an hour old, load it and render page
# Otherwise, determine balances, build json, store pickled data, and render page
# Otherwise, determine balances, build json, store pickled data, and render page
if diff . seconds < 3600 :
if diff . seconds < diff_seconds :
print ( f ' unpickling { key_name } ' )
print ( f ' unpickling { key_name } ' )
with open ( pickle_file , ' rb ' ) as f :
with open ( pickle_file , ' rb ' ) as f :
pickled_data = pickle . load ( f )
pickled_data = pickle . load ( f )