This commit is contained in:
codl 2017-07-28 01:07:51 +02:00
parent 8164f786ec
commit 03262e4f5d
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 16 additions and 12 deletions

View File

@ -35,10 +35,12 @@ def receive_verifier(oauth_token, oauth_verifier, consumer_key=None, consumer_se
remote_acct = new_twitter.account.verify_credentials()
acct = Account(remote_id = remote_acct['id_str'])
acct = db.session.merge(acct)
acct.remote_display_name = remote_acct['name']
acct.remote_avatar_url = remote_acct['profile_image_url_https']
new_token.account = acct
db.session.commit()
return new_token
def get_twitter_for_acc(account, consumer_key=None, consumer_secret=None):
@ -55,9 +57,9 @@ def fetch_posts_for_acc(account, consumer_key=None, consumer_secret=None):
kwargs = { 'user_id': account.remote_id, 'count': 200, 'trim_user': True }
#most_recent_post = Post.query.order_by(db.desc(Post.created_at)).filter(Post.author_id == account.remote_id).first()
#if most_recent_post:
# kwargs['since_id'] = most_recent_post.remote_id
most_recent_post = Post.query.order_by(db.desc(Post.created_at)).filter(Post.author_id == account.remote_id).first()
if most_recent_post:
kwargs['since_id'] = most_recent_post.remote_id
while True:
tweets = t.statuses.user_timeline(**kwargs)
@ -76,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()
db.session.commit()
account.last_post_fetch = datetime.now()
db.session.commit()

View File

@ -4,7 +4,7 @@ from app import app as flaskapp
from app import db
from model import Session, Account
from lib.twitter import fetch_posts_for_acc
from datetime import timedelta
from datetime import timedelta, datetime
app = Celery('tasks', broker=flaskapp.config['CELERY_BROKER'], task_serializer='pickle')
@ -19,17 +19,19 @@ def fetch_posts(remote_id):
fetch_posts_for_acc(Account.query.get(remote_id), **flaskapp.config.get_namespace("TWITTER_"))
@app.task
def queue_fetch_for_most_stale_accs(num=5, min_staleness=timedelta(hours=1)):
accs = Account.query\
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))\
.limit(num)
for acc in accs:
.first()
if acc:
fetch_posts.s(acc.remote_id).delay()
acc.last_post_fetch = datetime.now()
db.session.commit()
app.add_periodic_task(60*60, remove_old_sessions)
app.add_periodic_task(60, queue_fetch_for_most_stale_accs)
app.add_periodic_task(10*60, remove_old_sessions)
app.add_periodic_task(20, queue_fetch_for_most_stale_account)