mastodon login only when new tweets to toot
This commit is contained in:
parent
bdb09e1b25
commit
d221df18ae
1 changed files with 38 additions and 29 deletions
37
tootbot.py
37
tootbot.py
|
@ -8,7 +8,7 @@ import re
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from datetime import datetime, date, time, timedelta
|
from datetime import datetime, date, time, timedelta
|
||||||
|
|
||||||
if len(sys.argv) < 3:
|
if len(sys.argv) < 4:
|
||||||
print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance")
|
print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
@ -17,17 +17,34 @@ sql = sqlite3.connect('tootbot.db')
|
||||||
db = sql.cursor()
|
db = sql.cursor()
|
||||||
db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text, twitter text, mastodon text, instance text)''')
|
db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text, twitter text, mastodon text, instance text)''')
|
||||||
|
|
||||||
if len(sys.argv)>3:
|
if len(sys.argv)>4:
|
||||||
instance = sys.argv[4]
|
instance = sys.argv[4]
|
||||||
else:
|
else:
|
||||||
instance = 'amicale.net'
|
instance = 'amicale.net'
|
||||||
|
|
||||||
|
if len(sys.argv)>5:
|
||||||
|
days = int(sys.argv[5])
|
||||||
|
else:
|
||||||
|
days = 1
|
||||||
|
|
||||||
twitter = sys.argv[1]
|
twitter = sys.argv[1]
|
||||||
mastodon = sys.argv[2]
|
mastodon = sys.argv[2]
|
||||||
passwd = sys.argv[3]
|
passwd = sys.argv[3]
|
||||||
|
|
||||||
# Create application if it does not exist
|
mastodon_api = None
|
||||||
if not os.path.isfile(instance+'.secret'):
|
|
||||||
|
d = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+twitter)
|
||||||
|
|
||||||
|
for t in reversed(d.entries):
|
||||||
|
# check if this tweet has been processed
|
||||||
|
db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?',(t.id, twitter, mastodon, instance))
|
||||||
|
last = db.fetchone()
|
||||||
|
|
||||||
|
# process only unprocessed tweets less than 1 day old
|
||||||
|
if last is None and (datetime.now()-datetime(t.published_parsed.tm_year, t.published_parsed.tm_mon, t.published_parsed.tm_mday, t.published_parsed.tm_hour, t.published_parsed.tm_min, t.published_parsed.tm_sec) < timedelta(days=days)):
|
||||||
|
if mastodon_api is None:
|
||||||
|
# Create application if it does not exist
|
||||||
|
if not os.path.isfile(instance+'.secret'):
|
||||||
if Mastodon.create_app(
|
if Mastodon.create_app(
|
||||||
'tootbot',
|
'tootbot',
|
||||||
api_base_url='https://'+instance,
|
api_base_url='https://'+instance,
|
||||||
|
@ -38,7 +55,7 @@ if not os.path.isfile(instance+'.secret'):
|
||||||
print('failed to create app on instance '+instance)
|
print('failed to create app on instance '+instance)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mastodon_api = Mastodon(
|
mastodon_api = Mastodon(
|
||||||
client_id=instance+'.secret',
|
client_id=instance+'.secret',
|
||||||
api_base_url='https://'+instance
|
api_base_url='https://'+instance
|
||||||
|
@ -49,19 +66,11 @@ try:
|
||||||
scopes=['read', 'write'],
|
scopes=['read', 'write'],
|
||||||
to_file=mastodon+".secret"
|
to_file=mastodon+".secret"
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
print("ERROR: First Login Failed!")
|
print("ERROR: First Login Failed!")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
d = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+twitter)
|
|
||||||
|
|
||||||
for t in reversed(d.entries):
|
|
||||||
# check if this tweet has been processed
|
|
||||||
db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?',(t.id, twitter, mastodon, instance))
|
|
||||||
last = db.fetchone()
|
|
||||||
|
|
||||||
# process only unprocessed tweets less than 1 day old
|
|
||||||
if last is None and (datetime.now()-datetime(t.published_parsed.tm_year, t.published_parsed.tm_mon, t.published_parsed.tm_mday, t.published_parsed.tm_hour, t.published_parsed.tm_min, t.published_parsed.tm_sec) < timedelta(days=1)):
|
|
||||||
#h = BeautifulSoup(t.summary_detail.value, "html.parser")
|
#h = BeautifulSoup(t.summary_detail.value, "html.parser")
|
||||||
c = t.title
|
c = t.title
|
||||||
toot_media = []
|
toot_media = []
|
||||||
|
|
Loading…
Reference in a new issue