import feedparser import datetime import pypandoc from nltk import sent_tokenize from mastodon import Mastodon # https://feedparser.readthedocs.io/en/latest/ # ------------------------------------- # FEED DETAILS feed = 'output/feeds/feed.rss' d = feedparser.parse(feed) feedtitle = d.feed.title # ------------------------------------- # GREP LATEST RSS ENTRY title = d.entries[0].title author = d.entries[0].author link = d.entries[0].link year = "{:02d}".format(d.entries[0].published_parsed[0]) month = "{:02d}".format(d.entries[0].published_parsed[1]) day = "{:02d}".format(d.entries[0].published_parsed[2] + 1) print('LATEST POST IN RSS:', year, month, day) # ------------------------------------- # WHAT DATE IS IT TODAY? today = str(datetime.date.today()).split('-') today_year = "{:02d}".format(int(today[0])) today_month = "{:02d}".format(int(today[1])) today_day = "{:02d}".format(int(today[2])) print('TODAY: ', today_year, today_month, today_day) # ------------------------------------- # IF ENTRY IS PUBLISHED TODAY PUBLISH = False if year == today_year: if month == today_month: if day == today_day: PUBLISH = True # ------------------------------------- # MAKE THE TOOT ! toot = False if PUBLISH == True: post = d.entries[0].description post = pypandoc.convert_text(post, 'plain', format='html') post = post.split() first_letter = '\n'.join(post[:5]) post = '\n'.join(post[5:len(post)]) post = post.replace('\n\n','$$$') post = post.replace('\n',' ') post = post.replace('$$$','\n\n') footer = ''' ) ) )) ) ) ) ) ) )) ( ( (( ( ( ( ( ( (( {} by {} {} #ann #feed #AMRO #servus'''.format(title, author, link) maxlength = 500 # maximum length of a toot maxlength = maxlength-(len(footer) + 10) # to create space for the footer sentences = sent_tokenize(post) # print(sentences) toot = '' count = 0 for s in sentences: if count == 0: toot += '“' toot += first_letter count += 1 if count > 0: if (len(toot) + len(s)) < maxlength: toot += ' ' + s count += 1 toot += '”' toot += footer print(toot) # ------------------------------------- # PUBLISH ON MASTODON ! if toot: instance = 'https://social.servus.at/' # The following two steps only needs to be done once! # We already did it, so it is commented out now. # -------------------- # [done] Register the bot as an application, save details as a (.secret) plain text file. # -------------------- # Mastodon.create_app( # feedtitle, # api_base_url = instance, # to_file = 'rss-to-ap/rss-to-ap.secret' # ) # -------------------- # [done] Write your login details to a (.secret) plain text file. # -------------------- # mastodon = Mastodon( # client_id = 'rss-to-ap/rss-to-ap.secret', # api_base_url = instance # ) # username = 'nourishing-network@servus.at' # password = 'PASSWORD' # mastodon.log_in( # username, # password, # to_file = 'rss-to-ap/rss-to-ap-usercred.secret' # ) # -------------------- # Log in, using the .secret file. # -------------------- mastodon = Mastodon( access_token = 'rss-to-ap/rss-to-ap-usercred.secret', api_base_url = instance ) # mastodon.toot(toot)