mirror of
https://gitlab.com/jeancf/twoot.git
synced 2024-11-27 14:01:13 +00:00
Fixed interactions with Mastodon instance (.secret files bug)
This commit is contained in:
parent
e062b55af7
commit
d11e5d123f
187
twoot.py
187
twoot.py
|
@ -25,22 +25,22 @@ from bs4 import BeautifulSoup, element
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon, MastodonError
|
||||||
|
|
||||||
|
|
||||||
#TODO manage command line
|
#TODO manage command line
|
||||||
TWIT_ACCOUNT = 'humansoflatees'
|
TWIT_ACCOUNT = 'blendernation'
|
||||||
MAST_ACCOUNT = 'jc@noirextreme.com'
|
MAST_ACCOUNT = 'twoot@noirextreme.com'
|
||||||
MAST_PASSWORD = 'NfH1D.Sdd63juBmK'
|
MAST_PASSWORD = 'AcX/ZK5Ml6fRVDFi'
|
||||||
MAST_INSTANCE = 'botsin.space'
|
MAST_INSTANCE = 'mastodon.host'
|
||||||
MAX_AGE = 1 # in days
|
MAX_AGE = 5 # in days
|
||||||
MIN_DELAY = 0 # in minutes
|
MIN_DELAY = 0 # in minutes
|
||||||
|
|
||||||
|
|
||||||
#TODO submit random user agent from list
|
#TODO submit random user agent from list
|
||||||
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
|
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
|
||||||
|
|
||||||
#TODO manage errors
|
#TODO log to file
|
||||||
|
|
||||||
def cleanup_tweet_text(tt_iter):
|
def cleanup_tweet_text(tt_iter):
|
||||||
'''
|
'''
|
||||||
|
@ -198,84 +198,103 @@ for result in results:
|
||||||
}
|
}
|
||||||
tweets.append(tweet)
|
tweets.append(tweet)
|
||||||
|
|
||||||
|
# DEBUG: Print extracted tweets
|
||||||
for t in tweets:
|
for t in tweets:
|
||||||
print(t)
|
print(t)
|
||||||
|
|
||||||
|
|
||||||
# # **********************************************************
|
# **********************************************************
|
||||||
# # Iterate tweets. Check if the tweet has already been posted
|
# Iterate tweets. Check if the tweet has already been posted
|
||||||
# # on Mastodon. If not, post it and add it to database
|
# on Mastodon. If not, post it and add it to database
|
||||||
# # **********************************************************
|
# **********************************************************
|
||||||
#
|
|
||||||
# # Try to open database. If it does not exist, create it
|
# Try to open database. If it does not exist, create it
|
||||||
# sql = sqlite3.connect('twoot.db')
|
sql = sqlite3.connect('twoot.db')
|
||||||
# db = sql.cursor()
|
db = sql.cursor()
|
||||||
# db.execute('''CREATE TABLE IF NOT EXISTS toots (twitter_account TEXT, mastodon_instance TEXT,
|
db.execute('''CREATE TABLE IF NOT EXISTS toots (twitter_account TEXT, mastodon_instance TEXT,
|
||||||
# mastodon_account TEXT, tweet_id TEXT, toot_id TEXT)''')
|
mastodon_account TEXT, tweet_id TEXT, toot_id TEXT)''')
|
||||||
#
|
|
||||||
# # Create Mastodon application if it does not exist yet
|
# Create Mastodon application if it does not exist yet
|
||||||
# if not os.path.isfile(MAST_INSTANCE + '.secret'):
|
if not os.path.isfile(MAST_INSTANCE + '.secret'):
|
||||||
# if not Mastodon.create_app(
|
try:
|
||||||
# 'twoot',
|
Mastodon.create_app(
|
||||||
# api_base_url='https://' + MAST_INSTANCE,
|
'twoot',
|
||||||
# to_file=MAST_INSTANCE + '.secret'
|
api_base_url='https://' + MAST_INSTANCE,
|
||||||
# ):
|
to_file=MAST_INSTANCE + '.secret'
|
||||||
# print('failed to create app on ' + MAST_INSTANCE)
|
)
|
||||||
# sys.exit(1)
|
|
||||||
#
|
except MastodonError as me:
|
||||||
# # Log in to mastodon instance
|
print('failed to create app on ' + MAST_INSTANCE)
|
||||||
# try:
|
sys.exit(1)
|
||||||
# mastodon = Mastodon(
|
|
||||||
# client_id=MAST_INSTANCE + '.secret',
|
# Log in to Mastodon instance
|
||||||
# api_base_url='https://' + MAST_INSTANCE
|
try:
|
||||||
# )
|
mastodon = Mastodon(
|
||||||
#
|
client_id=MAST_INSTANCE + '.secret',
|
||||||
# mastodon.log_in(
|
api_base_url='https://' + MAST_INSTANCE
|
||||||
# username=MAST_ACCOUNT,
|
)
|
||||||
# password=MAST_PASSWORD,
|
|
||||||
# scopes=['read', 'write'],
|
mastodon.log_in(
|
||||||
# to_file=MAST_INSTANCE + ".secret"
|
username=MAST_ACCOUNT,
|
||||||
# )
|
password=MAST_PASSWORD,
|
||||||
# except:
|
to_file=MAST_ACCOUNT + ".secret"
|
||||||
# print("ERROR: Login Failed")
|
)
|
||||||
# sys.exit(1)
|
|
||||||
#
|
except MastodonError as me:
|
||||||
# # Upload tweets
|
print('ERROR: Login to ' + MAST_INSTANCE + ' Failed')
|
||||||
# for tweet in tweets:
|
print(me)
|
||||||
# # Check in database if tweet has already been posted
|
sys.exit(1)
|
||||||
# db.execute('''SELECT * FROM toots WHERE twitter_account = ? AND mastodon_instance = ? AND
|
|
||||||
# mastodon_account = ? AND tweet_id = ?''',
|
# Upload tweets
|
||||||
# (TWIT_ACCOUNT, MAST_INSTANCE, MAST_ACCOUNT, tweet['tweet_id']))
|
for tweet in reversed(tweets):
|
||||||
# tweet_in_db = db.fetchone()
|
# Check in database if tweet has already been posted
|
||||||
#
|
db.execute('''SELECT * FROM toots WHERE twitter_account = ? AND mastodon_instance = ? AND
|
||||||
# if tweet_in_db is not None:
|
mastodon_account = ? AND tweet_id = ?''',
|
||||||
# # Skip to next tweet
|
(TWIT_ACCOUNT, MAST_INSTANCE, MAST_ACCOUNT, tweet['tweet_id']))
|
||||||
# continue
|
tweet_in_db = db.fetchone()
|
||||||
#
|
|
||||||
# # Check that the tweet is not too young (might be deleted) or too old
|
if tweet_in_db is not None:
|
||||||
# age_in_hours = (time.time() - float(tweet['timestamp'])) / 3600.0
|
# Skip to next tweet
|
||||||
# min_delay_in_hours = float(MIN_DELAY) / 60.0
|
continue
|
||||||
# max_age_in_hours = float(MAX_AGE) * 24.0
|
|
||||||
#
|
# Check that the tweet is not too young (might be deleted) or too old
|
||||||
# if age_in_hours < min_delay_in_hours or age_in_hours > max_age_in_hours:
|
age_in_hours = (time.time() - float(tweet['timestamp'])) / 3600.0
|
||||||
# # Skip to next tweet
|
min_delay_in_hours = float(MIN_DELAY) / 60.0
|
||||||
# continue
|
max_age_in_hours = float(MAX_AGE) * 24.0
|
||||||
#
|
|
||||||
# # Upload photos
|
if age_in_hours < min_delay_in_hours or age_in_hours > max_age_in_hours:
|
||||||
# media_ids = []
|
# Skip to next tweet
|
||||||
# for photo in tweet['photos']:
|
continue
|
||||||
# # Download picture
|
|
||||||
# media = requests.get(photo)
|
# Upload photos
|
||||||
#
|
media_ids = []
|
||||||
# # Upload picture to Mastodon instance
|
for photo in tweet['photos']:
|
||||||
# media_posted = mastodon.media_post(media.content, mime_type=media.headers.get('content-type'))
|
# Download picture
|
||||||
# media_ids.append(media_posted['id'])
|
media = requests.get(photo)
|
||||||
#
|
|
||||||
# # Post toot
|
# Upload picture to Mastodon instance
|
||||||
# toot = mastodon.status_post(tweet['tweet_text'], media_ids=media_ids, visibility='public')
|
media_posted = mastodon.media_post(media.content, mime_type=media.headers.get('content-type'))
|
||||||
#
|
media_ids.append(media_posted['id'])
|
||||||
# # Insert toot id into database
|
|
||||||
# if 'id' in toot:
|
# Post toot
|
||||||
# db.execute("INSERT INTO toots VALUES ( ? , ? , ? , ? , ? )",
|
try:
|
||||||
# (TWIT_ACCOUNT, MAST_INSTANCE, MAST_ACCOUNT, tweet['tweet_id'], toot['id']))
|
mastodon = Mastodon(
|
||||||
# sql.commit()
|
access_token=MAST_ACCOUNT + '.secret',
|
||||||
|
api_base_url='https://' + MAST_INSTANCE
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(media_ids) == 0:
|
||||||
|
toot = mastodon.status_post(tweet['tweet_text'], visibility='public')
|
||||||
|
else:
|
||||||
|
toot = mastodon.status_post(tweet['tweet_text'], media_ids=media_ids, visibility='public')
|
||||||
|
|
||||||
|
except MastodonError as me:
|
||||||
|
print('ERROR: posting ' + tweet['tweet_text'] + ' to ' + MAST_INSTANCE + ' Failed')
|
||||||
|
print(me)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
# 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']))
|
||||||
|
sql.commit()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user