Skip to content

Turn into a python package#4

Open
drkane wants to merge 6 commits intoedent:mainfrom
drkane:python-package
Open

Turn into a python package#4
drkane wants to merge 6 commits intoedent:mainfrom
drkane:python-package

Conversation

@drkane
Copy link

@drkane drkane commented Aug 27, 2024

Quite a large refactor of the code to turn into a python package.

Main changes are:

Refactoring

  • code lives in the src/tweet2embed directory
  • added a pyproject.toml file to hold dependencies - so can now be installed, eg in a virtual environment (I've been using this to test out using uv). I've added selenium as an optional dependency as it has a bit of extra overhead
  • command line interface is managed through click

CLI

  • I've changed to a unified interface which is run through python -m tweet2embed. You can then follow it with any number of tweet IDs, tweet URLs or mastodon URLs.
  • You can specify the outputs using the -o or --output flag - the options are html, img or json - you can specify more than one
  • I've added -s/--save option to specify the output directory for saved files
  • The -b/--clipboard/--no-clipboard option can switch copy to clipboard on or off (this is obviously less useful for multiple tweets)
  • the -a/--archive/--no-archive option can switch off submission to archive.org
  • all the other flags are still present

Templating

  • I've added the jinja2 templating library. The main advantage is you get to separate out the html from the code. The templates are kept in src/tweet2embed/templates/
  • The other advantage is you get html escaping by default
  • I've also separated out the CSS into a separate file in that directory.
  • @todo at the moment only the tweet_to_html() function (and mastodon equivalent) uses the template. Ideally the get_card_html, get_media, etc would move to a template too

Others

  • I've tried to reuse requests.Session objects - this should in theory make it quicker with lots of tweets, but does cause a lot of annoying boilerplate.
  • some of the repeated functions are now in src/tweet2embed/utils

The code that does the actual work is still 99.9% original - it may have been moved around a bit. Let me know what you think - I may have moved it too far from the "do one thing well" scripts that you created...

@edent
Copy link
Owner

edent commented Oct 27, 2024

This all looks excellent. I hope to have some time to test this soon.

@edent
Copy link
Owner

edent commented Nov 20, 2024

Really good. A few issues.

Currently requires python = ">=3.12" - my system has 3.10, can this be lowered?

If I manual lower the version, it says

  • gftools 0.9.68 requires brotli, which is not installed.
  • gftools 0.9.68 requires tabulate, which is not installed.

Manually installing gftools works.

Can copying to the clipboard be default on? Even using -b doesn't seem to copy data to the clipboard.

The HTML output could do with being minified. Seems to be lots of whitespace in there. (Not sure if that's a regression or whether I didn't update the GitHub copy).

Converting Mastodon to JSON gives TypeError: a bytes-like object is required, not 'str'

If no output directory is specified, what happens to the downloaded data? It seems to disappear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants