jhjklhasdsf
This commit is contained in:
parent
03262e4f5d
commit
ccac563bcc
1
app.py
1
app.py
|
@ -6,7 +6,6 @@ from flask_migrate import Migrate
|
|||
app = Flask(__name__)
|
||||
|
||||
default_config = {
|
||||
"SQLALCHEMY_ECHO": True,
|
||||
"SQLALCHEMY_TRACK_MODIFICATIONS": False,
|
||||
"SQLALCHEMY_DATABASE_URI": "postgresql+psycopg2:///forget",
|
||||
"SECRET_KEY": "hunter2",
|
||||
|
|
|
@ -52,7 +52,7 @@ def get_twitter_for_acc(account, consumer_key=None, consumer_secret=None):
|
|||
import locale
|
||||
locale.setlocale(locale.LC_TIME, 'C') # jeez i hate that i have to do this
|
||||
|
||||
def fetch_posts_for_acc(account, consumer_key=None, consumer_secret=None):
|
||||
def fetch_acc(account, consumer_key=None, consumer_secret=None):
|
||||
t = get_twitter_for_acc(account, consumer_key=consumer_key, consumer_secret=consumer_secret)
|
||||
|
||||
kwargs = { 'user_id': account.remote_id, 'count': 200, 'trim_user': True }
|
||||
|
@ -78,6 +78,6 @@ def fetch_posts_for_acc(account, consumer_key=None, consumer_secret=None):
|
|||
kwargs['max_id'] = min(tweet['id'] - 1, kwargs['max_id'])
|
||||
|
||||
|
||||
account.last_post_fetch = datetime.now()
|
||||
account.last_fetch = datetime.now()
|
||||
db.session.commit()
|
||||
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
"""empty message
|
||||
|
||||
Revision ID: 1003f9df0ae0
|
||||
Revises: 8c2ce3a66650
|
||||
Create Date: 2017-07-28 12:32:54.375901
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '1003f9df0ae0'
|
||||
down_revision = '8c2ce3a66650'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
op.alter_column('accounts', 'last_post_fetch', new_column_name='last_fetch')
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.alter_column('accounts', 'last_fetch', new_column_name='last_post_fetch')
|
2
model.py
2
model.py
|
@ -26,7 +26,7 @@ class Account(db.Model, TimestampMixin):
|
|||
remote_display_name = db.Column(db.String)
|
||||
remote_avatar_url = db.Column(db.String)
|
||||
|
||||
last_post_fetch = db.Column(db.DateTime, server_default='epoch')
|
||||
last_fetch = db.Column(db.DateTime, server_default='epoch')
|
||||
|
||||
# backref: tokens
|
||||
|
||||
|
|
|
@ -21,8 +21,9 @@ def touch_viewer(resp):
|
|||
|
||||
@app.route('/')
|
||||
def index():
|
||||
print('hey')
|
||||
if g.viewer:
|
||||
posts = Post.query.order_by(db.desc(Post.created_at)).limit(30)
|
||||
posts = Post.query.filter_by(author_id = g.viewer.account_id).order_by(db.desc(Post.created_at)).limit(30)
|
||||
return render_template('index.html', posts=posts)
|
||||
else:
|
||||
return render_template('index.html')
|
||||
|
@ -53,9 +54,3 @@ def logout():
|
|||
db.session.commit()
|
||||
g.viewer = None
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/debug')
|
||||
def debug():
|
||||
import tasks
|
||||
tasks.fetch_posts.s('808418').delay()
|
||||
return "hi"
|
||||
|
|
30
tasks.py
30
tasks.py
|
@ -3,7 +3,8 @@ from celery import Celery
|
|||
from app import app as flaskapp
|
||||
from app import db
|
||||
from model import Session, Account
|
||||
from lib.twitter import fetch_posts_for_acc
|
||||
import lib.twitter
|
||||
from twitter import TwitterError
|
||||
from datetime import timedelta, datetime
|
||||
|
||||
app = Celery('tasks', broker=flaskapp.config['CELERY_BROKER'], task_serializer='pickle')
|
||||
|
@ -14,24 +15,23 @@ def remove_old_sessions():
|
|||
delete(synchronize_session=False)
|
||||
db.session.commit()
|
||||
|
||||
@app.task
|
||||
def fetch_posts(remote_id):
|
||||
fetch_posts_for_acc(Account.query.get(remote_id), **flaskapp.config.get_namespace("TWITTER_"))
|
||||
@app.task(autoretry_for=(TwitterError,))
|
||||
def fetch_acc(remote_id):
|
||||
lib.twitter.fetch_acc(Account.query.get(remote_id), **flaskapp.config.get_namespace("TWITTER_"))
|
||||
|
||||
@app.task
|
||||
def queue_fetch_for_most_stale_account(min_staleness=timedelta(minutes=5)):
|
||||
acc = Account.query\
|
||||
.filter(Account.last_post_fetch < db.func.now() - min_staleness)\
|
||||
.order_by(db.asc(Account.last_post_fetch))\
|
||||
.first()
|
||||
if acc:
|
||||
fetch_posts.s(acc.remote_id).delay()
|
||||
acc.last_post_fetch = datetime.now()
|
||||
db.session.commit()
|
||||
|
||||
def queue_fetch_for_most_stale_accounts(min_staleness=timedelta(minutes=1), limit=20):
|
||||
accs = Account.query\
|
||||
.filter(Account.last_fetch < db.func.now() - min_staleness)\
|
||||
.order_by(db.asc(Account.last_fetch))\
|
||||
.limit(limit)
|
||||
for acc in accs:
|
||||
fetch_acc.s(acc.remote_id).delay()
|
||||
acc.last_fetch = db.func.now()
|
||||
db.session.commit()
|
||||
|
||||
app.add_periodic_task(10*60, remove_old_sessions)
|
||||
app.add_periodic_task(20, queue_fetch_for_most_stale_account)
|
||||
app.add_periodic_task(60, queue_fetch_for_most_stale_accounts)
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue