@ -1,5 +1,4 @@
import pickle
from datetime import datetime
from datetime import datetime , timedelta
from os import path , remove
from io import BytesIO
from base64 import b64encode
@ -12,53 +11,14 @@ from suchwow import wownero
from suchwow import config
from suchwow . models import Post , Comment
from suchwow . utils . decorators import login_required , profile_required , moderator_required
from suchwow . utils . helpers import allowed_file , is_moderator , get_session_user , post_webhook
from suchwow . utils . helpers import allowed_file , is_moderator , get_session_user
from suchwow . utils . helpers import rw_cache , post_webhook
from suchwow . reddit import make_post
from suchwow . discord import post_discord_webhook
bp = Blueprint ( " post " , " post " )
@bp.route ( " /posts/top " )
def top ( ) :
top_posts = { }
pickle_file = path . join ( config . DATA_FOLDER , ' top_posts.pkl ' )
try :
mtime_ts = path . getmtime ( pickle_file )
mtime = datetime . fromtimestamp ( mtime_ts )
now = datetime . now ( )
diff = now - mtime
# 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
if diff . seconds < 3600 :
with open ( pickle_file , ' rb ' ) as f :
print ( ' Loading pickled data for /posts/top ' )
pickled_data = pickle . load ( f )
return render_template ( " post/top.html " , posts = pickled_data )
except :
pass
print ( ' Generating and pickling new data for /posts/top ' )
posts = Post . select ( ) . where ( Post . approved == True )
for post in posts :
transfers = [ ]
incoming = wownero . Wallet ( ) . incoming_transfers ( post . account_index )
if " transfers " in incoming :
for xfer in incoming [ " transfers " ] :
transfers . append ( xfer [ " amount " ] )
total = wownero . from_atomic ( sum ( transfers ) )
if total > 0 :
top_posts [ float ( total ) ] = post
_t = sorted ( top_posts . items ( ) , reverse = True ) [ 0 : 10 ]
with open ( pickle_file , ' wb ' ) as f :
f . write ( pickle . dumps ( _t ) )
return render_template ( " post/top.html " , posts = _t )
@bp.route ( " /post/<id> " )
def read ( id ) :
_address_qr = BytesIO ( )