Created
September 14, 2017 21:06
-
-
Save maop/8ddd8c9a0f8276f26d9a4c0da906dc94 to your computer and use it in GitHub Desktop.
RSS to Mastodon with a Media Attachment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# rss to toot with media attachment | |
# CommitStrip | |
# by Marco Alfonso [maop.mx] | |
# 2017-09 | |
import os | |
import feedparser #pip3 install feedparser | |
from mastodon import Mastodon #pip3 install Mastodon.py | |
import urllib.request | |
import time | |
# Replace this settings with your own | |
# Also you'll need to modify the way i extract the image's url | |
# acording to how your feed presents the url | |
feed_url = 'http://www.commitstrip.com/en/feed/' | |
instance_url= 'https://mstdn.mx' #mastodon instance | |
login_id = '[email protected]' #mastodon username | |
login_password = 'xxxxxxxxxxxx' #mastodon password | |
bot_name = 'CommitStripBot' #the AppName for Mastodon API | |
history_file = 'feed-log.txt' | |
clientcred_file = 'mastodon_clientcred.txt' | |
if not os.path.exists(clientcred_file): | |
Mastodon.create_app( | |
bot_name, | |
to_file = clientcred_file, | |
api_base_url = instance_url | |
) | |
mastodon = Mastodon( | |
client_id = clientcred_file, | |
api_base_url = instance_url | |
) | |
mastodon.log_in( | |
login_id, | |
login_password | |
) | |
feed = feedparser.parse(feed_url) | |
for item in reversed(feed.entries): | |
link = item.link | |
content = item.title +'\n' + item.link | |
for_send = True | |
# Does the log file exists? load it | |
if os.path.exists(history_file): | |
data = open(history_file, 'r+') | |
entries = data.readlines() | |
else: | |
data = open(history_file, 'a+') | |
entries = [] | |
# Have we tooted this link already? | |
for entry in entries: | |
if link in entry: | |
for_send = False | |
if for_send: | |
# Just for the log: | |
print("----------------------------------------") | |
print(time.strftime("%F %R") + " >> " + link) | |
# Extract the img src | |
# YOU NEED TO CHANGE THIS, this only works for the | |
# specific case of commitstrip's rss feed | |
index1 = item.content[0]['value'].find('src="') | |
index2 = item.content[0]['value'][index1+5:].find('"') | |
img_src = item.content[0]['value'][index1+5:index1+5+index2] | |
# commitstrip uses french words with accents in their filenames :-/ | |
img_src = urllib.parse.quote(img_src, safe=':/') | |
# Save the img to a file | |
index = img_src.rfind('/') | |
img_filename = img_src[index+1:] | |
img_file_path = "/tmp/"+img_filename | |
urllib.request.urlretrieve(img_src, img_file_path) | |
try: | |
#upload media | |
media_uploaded_id = (mastodon.media_post(img_file_path))["id"] | |
#mastodon.toot expects media_ids to be an array | |
medias = [] | |
medias.append(media_uploaded_id) | |
# finally! actually toot it ! | |
response = mastodon.status_post(content, media_ids=medias) | |
assert not response.get("error"), response | |
#save item link as done | |
data.write(link + '\n') | |
data.flush | |
except Exception as e: | |
import traceback | |
traceback.print_exc() | |
print("ERROR while posting: %s" % e) | |
continue | |
# clean downloaded image | |
if os.path.isfile(img_file_path): | |
os.remove(img_file_path) | |
data.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment