new feature: posting unfollowing accounts

This commit is contained in:
spla 2023-04-02 10:28:30 +02:00
parent cba9a3b650
commit 27119c1244
2 changed files with 121 additions and 5 deletions

View File

@ -59,6 +59,8 @@ class Database():
print(f'follower {account} is already in the database.')
self.update_follower(account)
found = True
else:
@ -79,11 +81,53 @@ class Database():
conn.close()
def find_unfollowers(self):
now = datetime.now()
unfollowers = []
select_sql = "select account from followers where following='f' and updated_at > (now() + interval '2 hours') - interval '6 hours';"
conn = None
try:
conn = psycopg2.connect(database=self.followers_db, user=self.followers_db_user, password = self.followers_db_user_password, host="/var/run/postgresql", port="6432")
cur = conn.cursor()
print(f'Searching unfollowers...')
cur.execute(select_sql)
rows = cur.fetchall()
if rows != None:
for row in rows:
unfollowers.append(row[0])
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return unfollowers
def save_follower(self, account):
now = datetime.now()
insert_sql = "INSERT INTO followers(account, updated_at) VALUES(%s,%s) ON CONFLICT DO NOTHING"
insert_sql = "INSERT INTO followers(account, updated_at, following) VALUES(%s,%s,%s) ON CONFLICT DO NOTHING"
conn = None
@ -95,7 +139,39 @@ class Database():
print(f'Writing follower {account}...')
cur.execute(insert_sql, (account, now))
cur.execute(insert_sql, (account, now, True))
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def update_follower(self, account):
now = datetime.now()
update_sql = "UPDATE followers set updated_at=(%s), following=(%s) where account=(%s)"
conn = None
try:
conn = psycopg2.connect(database=self.followers_db, user=self.followers_db_user, password = self.followers_db_user_password, host="/var/run/postgresql", port="6432")
cur = conn.cursor()
print(f'Updating follower {account}...')
cur.execute(update_sql, (now, True, account))
conn.commit()
@ -185,15 +261,45 @@ class Database():
conn.close()
def reset(self):
now = datetime.now()
update_sql = "UPDATE followers set following='f'"
conn = None
try:
conn = psycopg2.connect(database=self.followers_db, user=self.followers_db_user, password = self.followers_db_user_password, host="/var/run/postgresql", port="6432")
cur = conn.cursor()
cur.execute(update_sql)
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def totals(self):
local_followers = 0
remote_followers = 0
local_sql = "select count(*) from followers where account not like '%@%'"
local_sql = "select count(*) from followers where account not like '%@%' and following"
remote_sql = "select count(*) from followers where account like '%@%'"
remote_sql = "select count(*) from followers where account like '%@%' and following"
conn = None
@ -291,7 +397,7 @@ class Database():
db = self.followers_db
table = "followers"
sql = "create table "+table+" (account varchar(100) PRIMARY KEY, updated_at timestamptz)"
sql = "create table "+table+" (account varchar(100) PRIMARY KEY, updated_at timestamptz, following boolean)"
self.__create_table(self, table, sql)
table = "total"

View File

@ -56,6 +56,8 @@ if __name__ == '__main__':
followers = followers_list(my_id)
db.reset()
for follower in followers:
db.find_follower(follower.acct)
@ -82,10 +84,18 @@ if __name__ == '__main__':
db.save_total(local_followers, local_incr, remote_followers, remote_incr)
unfollowers = db.find_unfollowers()
post_text = f"@{mastodon.me().acct}\nseguidors: {len(followers)}\n"
post_text += f"locals: {local_followers} ({local_incr})\nremots: {remote_followers} ({remote_incr})"
if unfollowers != None:
for unfollow in unfollowers:
post_text += f'\nJa no et segueix:\n@{unfollow}'
mastodon.status_post(post_text, in_reply_to_id=None, visibility='direct')