|
|
|
@ -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):
|
|
|
|
|
|
|
|
|
|
if softname == 'mastodon':
|
|
|
|
|
|
|
|
|
|
sql_insert_ids = 'INSERT INTO id(toot_id, tweet_id) VALUES (%s,%s)'
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
|
|
write_db(toot_id, tweet_id)
|
|
|
|
|
sys.stdout.write(f'\nbot: {t_username}\nerror: {str(a_error)}\n')
|
|
|
|
|
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
|
|