mastodon login only when new tweets to toot

This commit is contained in:
cquest 2017-05-29 22:54:37 +02:00
parent bdb09e1b25
commit d221df18ae

View file

@ -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,15 +17,32 @@ 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]
mastodon_api = None
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 # Create application if it does not exist
if not os.path.isfile(instance+'.secret'): if not os.path.isfile(instance+'.secret'):
if Mastodon.create_app( if Mastodon.create_app(
@ -53,15 +70,7 @@ 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 = []