Added Pleroma server support
This commit is contained in:
parent
992f96eba6
commit
1e2806e23b
3 changed files with 88 additions and 22 deletions
|
@ -49,7 +49,7 @@ def save_bot_account(hostname, username, client_id, client_secret, token, twitte
|
|||
|
||||
save_error = None
|
||||
|
||||
mastodon_sql = f'INSERT INTO maccounts (bot_id, hostname, username, client_id, client_secret, client_token) VALUES (%s,%s,%s,%s,%s,%s)'
|
||||
mastodon_sql = f'INSERT INTO maccounts (bot_id, hostname, username, client_id, client_secret, client_token, hostname_soft) VALUES (%s,%s,%s,%s,%s,%s,%s)'
|
||||
|
||||
twitter_sql = f'INSERT INTO taccounts (bot_id, username) VALUES (%s,%s)'
|
||||
|
||||
|
@ -61,7 +61,7 @@ def save_bot_account(hostname, username, client_id, client_secret, token, twitte
|
|||
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute(mastodon_sql, (bot_uuid, hostname, username, client_id, client_secret, token))
|
||||
cur.execute(mastodon_sql, (bot_uuid, hostname, username, client_id, client_secret, token, hostname_soft))
|
||||
|
||||
cur.execute(twitter_sql, (bot_uuid, twitter_username))
|
||||
|
||||
|
@ -106,14 +106,15 @@ def check_db_conn():
|
|||
|
||||
def ask_account():
|
||||
|
||||
print(f'Setting up Mastodon bot account...\n')
|
||||
hostname = input("Enter Mastodon hostname: ")
|
||||
print(f'Setting up Mastodon/Pleroma bot account...\n')
|
||||
hostname = input("Enter Mastodon/Pleroma hostname: ")
|
||||
hostname_soft = input("Server software (mastodon or pleroma)? ")
|
||||
user_name = input(f'User name, ex. user@{hostname}? ')
|
||||
user_password = getpass.getpass("User password? ")
|
||||
app_name = 'replicator'
|
||||
twitter_username = input(f'Twitter username (ex. jack)? ')
|
||||
|
||||
return (hostname, user_name, user_password, app_name, twitter_username)
|
||||
return (hostname, hostname_soft, user_name, user_password, app_name, twitter_username)
|
||||
|
||||
def log_in(hostname, user_name, user_password, app_name):
|
||||
|
||||
|
@ -185,13 +186,13 @@ if __name__ == '__main__':
|
|||
|
||||
check_db_conn()
|
||||
|
||||
hostname, user_name, user_password, app_name, twitter_username = ask_account()
|
||||
hostname, hostname_soft, user_name, user_password, app_name, twitter_username = ask_account()
|
||||
|
||||
is_duplicate = check_account(user_name)
|
||||
|
||||
if is_duplicate:
|
||||
|
||||
print(f'Mastodon account already exist!')
|
||||
print(f'Bot account already exist!')
|
||||
|
||||
sys.exit()
|
||||
|
||||
|
@ -201,7 +202,7 @@ if __name__ == '__main__':
|
|||
|
||||
if len(token) > 0:
|
||||
|
||||
save_error = save_bot_account(hostname, user_name, client_id, client_secret, token, twitter_username)
|
||||
save_error = save_bot_account(hostname, hostname_soft, user_name, client_id, client_secret, token, twitter_username)
|
||||
|
||||
if save_error == None:
|
||||
|
||||
|
|
|
@ -191,13 +191,17 @@ if __name__ == '__main__':
|
|||
db = replicator_db
|
||||
db_user = replicator_db_user
|
||||
|
||||
table = "id"
|
||||
table = "mastodon_id"
|
||||
sql = f'create table {table} (toot_id bigint PRIMARY KEY, tweet_id bigint)'
|
||||
|
||||
create_table(db, db_user, table, sql)
|
||||
|
||||
table = "pleroma_id"
|
||||
sql = f'create table {table} (toot_id varchar(18) PRIMARY KEY, tweet_id bigint)'
|
||||
|
||||
create_table(db, db_user, table, sql)
|
||||
table = "maccounts"
|
||||
sql = f'create table {table} (bot_id uuid, hostname varchar(20), username varchar(45), client_id varchar(45), client_secret varchar(45), client_token varchar(45), PRIMARY KEY (bot_id))'
|
||||
sql = f'create table {table} (bot_id uuid, hostname varchar(20), username varchar(45), client_id varchar(45), client_secret varchar(45), client_token varchar(45), hostname_soft varchar(10), PRIMARY KEY (bot_id))'
|
||||
|
||||
create_table(db, db_user, table, sql)
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
from mastodon import Mastodon
|
||||
from mastodon.Mastodon import MastodonMalformedEventError, MastodonNetworkError, MastodonReadTimeout, MastodonAPIError, MastodonIllegalArgumentError
|
||||
import psycopg2
|
||||
import sys
|
||||
import time
|
||||
|
@ -30,7 +31,9 @@ def load_bots():
|
|||
|
||||
client_token_lst = []
|
||||
|
||||
sql = 'select bot_id, hostname, username, client_id, client_secret, client_token from maccounts'
|
||||
hostname_soft_lst = []
|
||||
|
||||
sql = 'select bot_id, hostname, username, client_id, client_secret, client_token, hostname_soft from maccounts'
|
||||
|
||||
try:
|
||||
|
||||
|
@ -58,9 +61,11 @@ def load_bots():
|
|||
|
||||
client_token_lst.append(row[5])
|
||||
|
||||
hostname_soft_lst.append(row[6])
|
||||
|
||||
cur.close()
|
||||
|
||||
return(bot_id_lst, hostname_lst, username_lst, client_id_lst, client_secret_lst, client_token_lst)
|
||||
return(bot_id_lst, hostname_lst, username_lst, client_id_lst, client_secret_lst, client_token_lst, hostname_soft_lst)
|
||||
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
|
||||
|
@ -108,6 +113,42 @@ def get_tusername(bot_id):
|
|||
|
||||
conn.close()
|
||||
|
||||
def get_software(bot_id):
|
||||
|
||||
software = ''
|
||||
|
||||
sql = 'select hostname_soft from maccounts where bot_id=(%s)'
|
||||
|
||||
try:
|
||||
|
||||
conn = None
|
||||
|
||||
conn = psycopg2.connect(database = replicator_db, user = replicator_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
||||
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute(sql, (bot_id,))
|
||||
|
||||
row = cur.fetchone()
|
||||
|
||||
if row != 0:
|
||||
|
||||
software = row[0]
|
||||
|
||||
cur.close()
|
||||
|
||||
return(software)
|
||||
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
|
||||
print(error)
|
||||
|
||||
finally:
|
||||
|
||||
if conn is not None:
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
def parse_url(toot_text):
|
||||
|
||||
|
@ -123,7 +164,7 @@ def parse_url(toot_text):
|
|||
|
||||
return tuit_text
|
||||
|
||||
def get_toot_id(tweet_id):
|
||||
def get_toot_id(tweet_id, software):
|
||||
|
||||
toot_id = 0
|
||||
|
||||
|
@ -135,7 +176,13 @@ def get_toot_id(tweet_id):
|
|||
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute('select toot_id, tweet_id from id where tweet_id=(%s)', (tweet_id,))
|
||||
if software == 'mastodon':
|
||||
|
||||
cur.execute('select toot_id, tweet_id from id where tweet_id=(%s)', (tweet_id,))
|
||||
|
||||
elif software == 'pleroma':
|
||||
|
||||
cur.execute('select toot_id, tweet_id from pleroma_id where tweet_id=(%s)', (tweet_id,))
|
||||
|
||||
row = cur.fetchone()
|
||||
|
||||
|
@ -157,9 +204,15 @@ def get_toot_id(tweet_id):
|
|||
|
||||
conn.close()
|
||||
|
||||
def write_db(toot_id, tweet_id):
|
||||
def write_db(toot_id, tweet_id, softname):
|
||||
|
||||
sql_insert_ids = 'INSERT INTO id(toot_id, tweet_id) VALUES (%s,%s)'
|
||||
if softname == 'mastodon':
|
||||
|
||||
sql_insert_ids = 'INSERT INTO id(toot_id, tweet_id) VALUES (%s,%s)'
|
||||
|
||||
elif softname == 'pleroma':
|
||||
|
||||
sql_insert_ids = 'INSERT INTO pleroma_id(toot_id, tweet_id) VALUES (%s,%s)'
|
||||
|
||||
conn = None
|
||||
|
||||
|
@ -299,7 +352,7 @@ if __name__ == '__main__':
|
|||
|
||||
replicator_db, replicator_db_user = db_config()
|
||||
|
||||
bot_id_lst, hostname_lst, username_lst, client_id_lst, client_secret_lst, client_token_lst = load_bots()
|
||||
bot_id_lst, hostname_lst, username_lst, client_id_lst, client_secret_lst, client_token_lst, hostname_soft_lst = load_bots()
|
||||
|
||||
logged_in = False
|
||||
|
||||
|
@ -313,6 +366,8 @@ if __name__ == '__main__':
|
|||
|
||||
t_username = get_tusername(bot_id_lst[i])
|
||||
|
||||
software = get_software(bot_id_lst[i])
|
||||
|
||||
# check new tweets
|
||||
|
||||
if not logged_in:
|
||||
|
@ -341,11 +396,11 @@ if __name__ == '__main__':
|
|||
|
||||
tweet_id = tweet.id
|
||||
|
||||
toot_id = get_toot_id(tweet_id)
|
||||
toot_id = get_toot_id(tweet_id, software)
|
||||
|
||||
if toot_id == 0:
|
||||
|
||||
print(f'tweet id: {tweet.id}, {tweet.full_text}')
|
||||
print(f'{t_username}, tweet id: {tweet.id}, {tweet.full_text}')
|
||||
|
||||
is_reply = False
|
||||
|
||||
|
@ -357,7 +412,7 @@ if __name__ == '__main__':
|
|||
|
||||
tweet_reply_id = tweet.in_reply_to_status_id
|
||||
|
||||
reply_toot_id = get_toot_id(tweet_reply_id)
|
||||
reply_toot_id = get_toot_id(tweet_reply_id, software)
|
||||
|
||||
if reply_toot_id != 0:
|
||||
|
||||
|
@ -399,9 +454,15 @@ if __name__ == '__main__':
|
|||
|
||||
toot_text = f'{toot_text}\n{url}'
|
||||
|
||||
toot_id = mastodon.status_post(toot_text, in_reply_to_id=reply_toot_id, media_ids=images_id_lst if is_media else None).id
|
||||
try:
|
||||
|
||||
write_db(toot_id, tweet_id)
|
||||
toot_id = mastodon.status_post(toot_text, in_reply_to_id=reply_toot_id, media_ids=images_id_lst if is_media else None).id
|
||||
|
||||
write_db(toot_id, tweet_id, software)
|
||||
|
||||
except MastodonAPIError as a_error:
|
||||
|
||||
sys.stdout.write(f'\nbot: {t_username}\nerror: {str(a_error)}\n')
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
|
|
Loading…
Reference in a new issue