Skip to content
Snippets Groups Projects
Commit 025c53b9 authored by M33's avatar M33 :speech_balloon:
Browse files

Make it work with RSS feeds

parent 3796464e
No related merge requests found
...@@ -8,8 +8,8 @@ import feedparser ...@@ -8,8 +8,8 @@ import feedparser
from mastodon import Mastodon from mastodon import Mastodon
import requests import requests
if len(sys.argv) < 4: if len(sys.argv) < 8:
print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance [max_days [footer_tags [delay]]]") # noqa print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance max_days footer_tags delay mastodon_account_email") # noqa
sys.exit(1) sys.exit(1)
# sqlite db to store processed tweets (and corresponding toots ids) # sqlite db to store processed tweets (and corresponding toots ids)
...@@ -20,38 +20,36 @@ db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text, ...@@ -20,38 +20,36 @@ db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text,
if len(sys.argv) > 4: if len(sys.argv) > 4:
instance = sys.argv[4] instance = sys.argv[4]
else:
instance = 'amicale.net'
if len(sys.argv) > 5: if len(sys.argv) > 5:
days = int(sys.argv[5]) days = int(sys.argv[5])
else:
days = 1
if len(sys.argv) > 6: if len(sys.argv) > 6:
tags = sys.argv[6] tags = sys.argv[6]
else:
tags = None
if len(sys.argv) > 7: if len(sys.argv) > 7:
delay = int(sys.argv[7]) delay = int(sys.argv[7])
else:
delay = 0
if len(sys.argv) > 8:
mastodon_account_email = sys.argv[8]
source = sys.argv[1] source = sys.argv[1]
mastodon = sys.argv[2] mastodon = sys.argv[2]
passwd = sys.argv[3] passwd = sys.argv[3]
mastodon_api = None mastodon_api = None
if source[:4] == 'http': if source[:4] == 'http':
print("Parsing source ",source," ...", end='')
d = feedparser.parse(source) d = feedparser.parse(source)
twitter = None twitter = None
print("ok.")
else: else:
d = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+source) d = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+source)
twitter = source twitter = source
print("Processing", end='')
for t in reversed(d.entries): for t in reversed(d.entries):
# check if this tweet has been processed # check if this tweet has been processed
db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?', (t.id, source, mastodon, instance)) # noqa db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?', (t.id, source, mastodon, instance)) # noqa
...@@ -63,33 +61,38 @@ for t in reversed(d.entries): ...@@ -63,33 +61,38 @@ for t in reversed(d.entries):
if last is None and age < timedelta(days=days) and age > timedelta(days=delay): if last is None and age < timedelta(days=days) and age > timedelta(days=delay):
if mastodon_api is None: 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'): try:
if Mastodon.create_app( Mastodon.create_app('tootbot', api_base_url='https://'+instance, to_file='/var/run/lock/'+instance+'.secret')
'tootbot', except:
api_base_url='https://'+instance, print('ERROR: failed to create app on instance '+instance)
to_file=instance+'.secret' sys.exit(1)
):
print('tootbot app created on instance '+instance)
else:
print('failed to create app on instance '+instance)
sys.exit(1)
try: try:
print("Trying to connect with ",instance+'.secret'," to ",'https://'+instance," ...", end='')
mastodon_api = Mastodon( mastodon_api = Mastodon(
client_id=instance+'.secret', client_id='/var/run/lock/'+instance+'.secret',
api_base_url='https://'+instance api_base_url='https://'+instance
) )
print("ok.")
except:
print("ERROR: can't connect")
sys.exit(1)
print("Login with email ",mastodon_account_email," ...", end='')
try:
mastodon_api.log_in( mastodon_api.log_in(
username=mastodon, mastodon_account_email,
password=passwd, passwd,
scopes=['read', 'write'], to_file='/var/run/lock/'+instance+'.secret'
to_file=mastodon+".secret"
) )
print("ok.")
except: except:
print("ERROR: First Login Failed!") print("ERROR: First Login Failed! ",)
sys.exit(1) sys.exit(1)
c = t.title c = t.title
print("============================================================")
print("[Posting]:",c)
print(" ")
if twitter and t.author.lower() != ('(@%s)' % twitter).lower(): if twitter and t.author.lower() != ('(@%s)' % twitter).lower():
c = ("RT https://twitter.com/%s\n" % t.author[2:-1]) + c c = ("RT https://twitter.com/%s\n" % t.author[2:-1]) + c
toot_media = [] toot_media = []
...@@ -98,6 +101,7 @@ for t in reversed(d.entries): ...@@ -98,6 +101,7 @@ for t in reversed(d.entries):
media = requests.get(p.group(0)) media = requests.get(p.group(0))
media_posted = mastodon_api.media_post(media.content, mime_type=media.headers.get('content-type')) media_posted = mastodon_api.media_post(media.content, mime_type=media.headers.get('content-type'))
toot_media.append(media_posted['id']) toot_media.append(media_posted['id'])
print("Added media",id)
# replace short links by original URL # replace short links by original URL
m = re.search(r"http[^ \xa0]*", c) m = re.search(r"http[^ \xa0]*", c)
...@@ -132,3 +136,7 @@ for t in reversed(d.entries): ...@@ -132,3 +136,7 @@ for t in reversed(d.entries):
db.execute("INSERT INTO tweets VALUES ( ? , ? , ? , ? , ? )", db.execute("INSERT INTO tweets VALUES ( ? , ? , ? , ? , ? )",
(t.id, toot["id"], source, mastodon, instance)) (t.id, toot["id"], source, mastodon, instance))
sql.commit() sql.commit()
else:
print(".",end='')
print("Bye!")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment