-
Notifications
You must be signed in to change notification settings - Fork 1
Mastodon Favorites feed, mastodon client script, refactor #14
Conversation
…, timezones), add helpful script for creating minimum-permission mastodon client
| import sys | ||
|
|
||
| import os | ||
| from config import get_config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the config loading here so I could use it in create_mastodon_client.
|
|
||
| # Mastodon | ||
| try: | ||
| client_file = param['mastodon']['client_id_file'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some sanity checks here because I had a typo in my config file that was tricky to debug.
| access_token=param['mastodon']['access_token_file'] | ||
| client_id=client_file, | ||
| access_token=access_token_file, | ||
| api_base_url=mastodon_url |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this after discovering it refused to work for anything but the flagship instance.
| '✰ : ' + str(toot['favourites_count']) + '</div></blockquote>' | ||
|
|
||
| toot['created_at'] = datetime.datetime.strptime(toot['created_at'], '%Y-%m-%dT%H:%M:%S.%fZ') | ||
| if isinstance(toot['created_at'], str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the most recent Mastodon.py, created_at is already a datetime, so this conversion is only needed with older versions of the client.
|
|
||
| text = BeautifulSoup(toot['content'], "html.parser").text | ||
| pubdate = toot['created_at'] | ||
| if not pubdate.tzinfo: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, the created_at datetime already has timezone info. I'm not entirely sure which timezone is used, but I suspect it is whatever my user account's preference is set to.
|
|
||
| if __name__ == "__main__": | ||
| app.run() | ||
| app.run(use_reloader=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was helpful during dev. I haven't deployed it, but I suspect it won't have any effect under gunicorn or whatever.
| password = getpass("Password (not shown and not saved):") | ||
|
|
||
| # Log in - either every time, or use persisted | ||
| mastodon.log_in(user_email, password, to_file=mast_cfg['access_token_file'], scopes=['read']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This app doesn't need write or follow permissions, so this asks only for read access. It looks a little less scary on the authorized apps page...
| return xml | ||
|
|
||
| @app.route('/toot_favorites') | ||
| def toot_favorites_feed(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this was what started me on this whole thing.
|
thanks again @georgedorn 👍 |
I found your project today when searching again for an app that read Mastodon and outputted RSS; I've specifically been looking for a way to generate an RSS feed of my favorites, so I added that.
Along the way, I fixed a couple of bugs in a backwards-compatible way:
toot['created_at']is already a datetime in the current version of Mastodon.pyCreating a mastodon app and credentials is non-trivial, especially for users of instances other than the flagship. After figuring the process out, I wrote a simple script (
create_mastodon_client.py) to simplify the process; it relies on values inconfig.ymlso I updated the README accordingly.Feel free to ignore this; I'm just sending you this PR as a courtesy and a thank-you for implementing 95% of what I wanted.