add ability to keep posts with media (close #1)

This commit is contained in:
codl 2017-08-08 15:38:54 +02:00
parent 4be6edb15f
commit ac6f7203d8
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
7 changed files with 54 additions and 5 deletions

View File

@ -91,6 +91,8 @@ def post_from_api_tweet_object(tweet, post=None):
post.author_id = 'twitter:{}'.format(tweet['user']['id_str']) post.author_id = 'twitter:{}'.format(tweet['user']['id_str'])
if 'favorited' in tweet: if 'favorited' in tweet:
post.favourite = tweet['favorited'] post.favourite = tweet['favorited']
if 'entities' in tweet:
post.has_media = bool('media' in tweet['entities'] and tweet['entities']['media'])
return post return post
def fetch_acc(account, cursor, consumer_key=None, consumer_secret=None): def fetch_acc(account, cursor, consumer_key=None, consumer_secret=None):

View File

@ -0,0 +1,27 @@
"""add post media
Revision ID: 04da9abf37e2
Revises: 2e3a2882e5a4
Create Date: 2017-08-08 15:15:50.911420
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '04da9abf37e2'
down_revision = '2e3a2882e5a4'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('accounts', sa.Column('policy_keep_media', sa.Boolean(), server_default='FALSE', nullable=False))
op.add_column('posts', sa.Column('has_media', sa.Boolean(), server_default='FALSE', nullable=False))
# ### end Alembic commands ###
def downgrade():
op.drop_column('posts', 'has_media')
op.drop_column('accounts', 'policy_keep_media')

View File

@ -43,6 +43,7 @@ class Account(TimestampMixin, RemoteIDMixin):
policy_enabled = db.Column(db.Boolean, server_default='FALSE', nullable=False) policy_enabled = db.Column(db.Boolean, server_default='FALSE', nullable=False)
policy_keep_latest = db.Column(db.Integer, server_default='100', nullable=False) policy_keep_latest = db.Column(db.Integer, server_default='100', nullable=False)
policy_keep_favourites = db.Column(db.Boolean, server_default='TRUE', nullable=False) policy_keep_favourites = db.Column(db.Boolean, server_default='TRUE', nullable=False)
policy_keep_media = db.Column(db.Boolean, server_default='FALSE', nullable=False)
policy_delete_every = db.Column(db.Interval, server_default='30 minutes', nullable=False) policy_delete_every = db.Column(db.Interval, server_default='30 minutes', nullable=False)
policy_keep_younger = db.Column(db.Interval, server_default='365 days', nullable=False) policy_keep_younger = db.Column(db.Interval, server_default='365 days', nullable=False)
@ -95,6 +96,8 @@ class Account(TimestampMixin, RemoteIDMixin):
filter(~Post.id.in_(latest_n_posts)) filter(~Post.id.in_(latest_n_posts))
if(self.policy_keep_favourites): if(self.policy_keep_favourites):
query = query.filter_by(favourite = False) query = query.filter_by(favourite = False)
if(self.policy_keep_media):
query = query.filter_by(has_media = False)
return query.count() return query.count()
@ -118,6 +121,8 @@ class OAuthToken(db.Model, TimestampMixin):
# note: account_id is nullable here because we don't know what account a token is for # note: account_id is nullable here because we don't know what account a token is for
# until we call /account/verify_credentials with it # until we call /account/verify_credentials with it
class Session(db.Model, TimestampMixin): class Session(db.Model, TimestampMixin):
__tablename__ = 'sessions' __tablename__ = 'sessions'
@ -126,6 +131,7 @@ class Session(db.Model, TimestampMixin):
account_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False) account_id = db.Column(db.String, db.ForeignKey('accounts.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False)
account = db.relationship(Account, lazy='joined', backref='sessions') account = db.relationship(Account, lazy='joined', backref='sessions')
class Post(db.Model, TimestampMixin, RemoteIDMixin): class Post(db.Model, TimestampMixin, RemoteIDMixin):
__tablename__ = 'posts' __tablename__ = 'posts'
@ -137,12 +143,15 @@ class Post(db.Model, TimestampMixin, RemoteIDMixin):
backref=db.backref('posts', order_by=lambda: db.desc(Post.created_at))) backref=db.backref('posts', order_by=lambda: db.desc(Post.created_at)))
favourite = db.Column(db.Boolean, server_default='FALSE', nullable=False) favourite = db.Column(db.Boolean, server_default='FALSE', nullable=False)
has_media = db.Column(db.Boolean, server_default='FALSE', nullable=False)
def snippet(self):
if len(self.body) > 20:
return self.body[:19] + ""
return self.body
def __repr__(self): def __repr__(self):
snippet = self.body return '<Post ({}, "{}", Author: {})>'.format(self.id, self.snippet(), self.author_id)
if len(snippet) > 20:
snippet = snippet[:19] + ""
return '<Post ({}, "{}", Author: {})>'.format(self.id, snippet, self.author_id)
class TwitterArchive(db.Model, TimestampMixin): class TwitterArchive(db.Model, TimestampMixin):
__tablename__ = 'twitter_archives' __tablename__ = 'twitter_archives'

View File

@ -91,6 +91,7 @@ def settings():
'policy_delete_every_scale', 'policy_delete_every_scale',
'policy_keep_younger_significand', 'policy_keep_younger_significand',
'policy_keep_younger_scale', 'policy_keep_younger_scale',
'policy_keep_media',
): ):
try: try:
if attr in request.form: if attr in request.form:

View File

@ -140,7 +140,10 @@ def delete_from_account(account_id):
posts = refresh_posts(posts) posts = refresh_posts(posts)
if account.service == 'twitter': if account.service == 'twitter':
eligible = list((post for post in posts if not account.policy_keep_favourites or not post.favourite)) eligible = list((post for post in posts if
(not account.policy_keep_favourites or not post.favourite)
and (not account.policy_keep_media or not post.has_media)
))
if eligible: if eligible:
if account.policy_delete_every == timedelta(0): if account.policy_delete_every == timedelta(0):
print("deleting all {} eligible posts for {}".format(len(eligible), account)) print("deleting all {} eligible posts for {}".format(len(eligible), account))

View File

@ -13,6 +13,7 @@
<li>Pick between deleting every old post or randomly deleting one old post periodically</li> <li>Pick between deleting every old post or randomly deleting one old post periodically</li>
<li>Pick between deleting posts older than a date, posts past a post count, or both</li> <li>Pick between deleting posts older than a date, posts past a post count, or both</li>
<li>Optionally mark posts that you want to preserve forever, by giving them a like</li> <li>Optionally mark posts that you want to preserve forever, by giving them a like</li>
<li>Optionally keep posts with media forever</li>
<li>Upload a tweet archive if your twitter account has more tweets than the twitter api will return</li> <li>Upload a tweet archive if your twitter account has more tweets than the twitter api will return</li>
<li><a href="https://github.com/codl/forget">Free and Open Source software</a></li> <li><a href="https://github.com/codl/forget">Free and Open Source software</a></li>
</ul> </ul>

View File

@ -56,6 +56,12 @@
<label><input type=radio name=policy_keep_favourites value=false {{ "checked" if not g.viewer.account.policy_keep_favourites }}> No</label> <label><input type=radio name=policy_keep_favourites value=false {{ "checked" if not g.viewer.account.policy_keep_favourites }}> No</label>
</span> </span>
</p> </p>
<p>Posts that have media attached will stay fresh forever
<span style='display:inline-block'>
<label><input type=radio name=policy_keep_media value=true {{ "checked" if g.viewer.account.policy_keep_media }}> Yes</label>
<label><input type=radio name=policy_keep_media value=false {{ "checked" if not g.viewer.account.policy_keep_media }}> No</label>
</span>
</p>
<p>Any post that is not kept fresh by any of these rules is considered expired. One random expired post will be deleted every <p>Any post that is not kept fresh by any of these rules is considered expired. One random expired post will be deleted every
{{interval_input(g.viewer.account, 'policy_delete_every', scales)}} {{interval_input(g.viewer.account, 'policy_delete_every', scales)}}
</p> </p>