replaced twit_account and tweets_and_replies

This commit is contained in:
jeancf 2022-11-18 16:54:31 +01:00
parent c8986ad5fb
commit aa75146580

View File

@ -398,11 +398,9 @@ def main(argv):
print('Missing Mastodon user')
exit(-1)
# twit_account = args['t']
# mast_instance = args['i']
# mast_account = args['m']
# mast_password = args['p']
# tweets_and_replies = args['r']
# suppress_retweets = args['s']
# get_vids = args['v']
# remove_trackers = args['u']
@ -439,7 +437,7 @@ def main(argv):
print(toml)
# DEBUG CONFIG
exit(1)
# exit(1)
# Try to open database. If it does not exist, create it
sql = sqlite3.connect('twoot.db')
@ -473,9 +471,9 @@ def main(argv):
}
)
url = nitter_url + '/' + twit_account
url = nitter_url + '/' + toml['config']['twitter_account']
# Use different page if we need to handle replies
if tweets_and_replies:
if toml['options']['post_reply_to']:
url += '/with_replies'
# Download twitter page of user.
@ -497,7 +495,7 @@ def main(argv):
logging.info('Nitter page downloaded successfully from ' + url)
# DEBUG: Save page to file
# of = open(twit_account + '.html', 'w')
# of = open(toml['config']['twitter_account'] + '.html', 'w')
# of.write(twit_account_page.text)
# of.close()
@ -508,7 +506,7 @@ def main(argv):
ta = soup.find('meta', property='og:title').get('content')
ta_match = re.search(r'\(@(.+)\)', ta)
if ta_match is not None:
twit_account = ta_match.group(1)
toml['config']['twitter_account'] = ta_match.group(1)
# Extract twitter timeline
timeline = soup.find_all('div', class_='timeline-item')
@ -552,7 +550,7 @@ def main(argv):
# Check in database if tweet has already been posted
db.execute(
"SELECT * FROM toots WHERE twitter_account=? AND mastodon_instance=? AND mastodon_account=? AND tweet_id=?",
(twit_account, mast_instance, mast_account, tweet_id))
(toml['config']['twitter_account'], mast_instance, mast_account, tweet_id))
tweet_in_db = db.fetchone()
if tweet_in_db is not None:
@ -606,7 +604,7 @@ def main(argv):
# Process attachment: capture image or .mp4 url or download twitter video
attachments_class = status.find('div', class_='attachments')
if attachments_class is not None:
pics, vid_in_tweet = process_attachments(nitter_url, attachments_class, get_vids, twit_account, status_id,
pics, vid_in_tweet = process_attachments(nitter_url, attachments_class, get_vids, toml['config']['twitter_account'], status_id,
author_account)
photos.extend(pics)
if vid_in_tweet:
@ -643,7 +641,7 @@ def main(argv):
# Check if video was downloaded
video_file = None
video_path = Path('./output') / twit_account / status_id
video_path = Path('./output') / toml['config']['twitter_account'] / status_id
if video_path.exists():
# list video files
video_file_list = list(video_path.glob('*.mp4'))
@ -750,21 +748,21 @@ def main(argv):
# Insert toot id into database
if 'id' in toot:
db.execute("INSERT INTO toots VALUES ( ? , ? , ? , ? , ? )",
(twit_account, mast_instance, mast_account, tweet['tweet_id'], toot['id']))
(toml['config']['twitter_account'], mast_instance, mast_account, tweet['tweet_id'], toot['id']))
sql.commit()
logging.info(str(posted_cnt) + ' tweets posted to Mastodon')
# Cleanup downloaded video files
try:
shutil.rmtree('./output/' + twit_account)
shutil.rmtree('./output/' + toml['config']['twitter_account'])
except FileNotFoundError: # The directory does not exist
pass
# Evaluate excess records in database
excess_count = 0
db.execute('SELECT count(*) FROM toots WHERE twitter_account=?', (twit_account,))
db.execute('SELECT count(*) FROM toots WHERE twitter_account=?', (toml['config']['twitter_account'],))
db_count = db.fetchone()
if db_count is not None:
excess_count = db_count[0] - MAX_REC_COUNT
@ -780,7 +778,7 @@ def main(argv):
LIMIT ?
)
DELETE from toots
WHERE tweet_id IN excess''', (twit_account, excess_count))
WHERE tweet_id IN excess''', (toml['config']['twitter_account'], excess_count))
sql.commit()
logging.info('Deleted ' + str(excess_count) + ' old records from database.')