diff --git a/README.md b/README.md index 43073fd..d694ba4 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,7 @@ A view of a Mastodon post showing a 5-picture collage, 1 larger image on the left and 4 small images in a grid orientation

-An experimental bot that posts a rundown of your musical week on Mastodon and/or Twitter. - -**:warning: The Twitter integration will be phased out as Twitter has taken an agressive stance against automatic tweeting. My development account has been suspended, so I'm no longer able to develop the integration. Mastodon will be the sole focus going forward.** +An experimental bot that posts a rundown of your musical week on Mastodon. ## 🤔 What does this do? @@ -54,9 +52,6 @@ See the [configuration example](/config.json.example) to see how to setup the to * Mastodon: global `MASTODON_URL`, `MASTODON_KEY` and `MASTODON_SECRET`. * Easily register an application in **Preferences**, then **Development**. * Permissions needed are **write:media** and **write:statuses**. -* Twitter: global `TWITTER_CONSUMER_KEY`, `TWITTER_CONSUMER_SECRET`, and per-user `TWITTER_ACCESS_TOKEN`, and `TWITTER_ACCESS_TOKEN`. - * You can [register for Twitter API keys here](https://developer.twitter.com/en/portal/dashboard). - * Ensure your access token has **read and write** capabilities (default is read only). With everything set, you can just run `python3 -m htw` from CLI, and all the magic should happen. You can see the optional arguments by running `python3 -m htw --help`. diff --git a/config.json.example b/config.json.example index 0200933..eb5bfdf 100644 --- a/config.json.example +++ b/config.json.example @@ -2,16 +2,12 @@ "clients": [ { "lastfmUsername": "example", - "twitterAccessToken": "key", - "twitterAccessSecret": "key", "mastodonUsername": "example@mastodon.social", "mastodonAccessToken": "key" } ], "config": { "lastfmKey": "key", - "twitterConsumerKey": "key", - "twitterConsumerSecret": "key", "mastodonURL": "https://mastodon.social", "mastodonKey": "key", "mastodonSecret": "key" diff --git a/htw/cli.py b/htw/cli.py index b44c022..89697aa 100644 --- a/htw/cli.py +++ b/htw/cli.py @@ -11,7 +11,7 @@ from htw.lfm import LFM, LFMPeriod from htw.collage import Collage -from htw.twitter import Twitter +from htw.compose import Compose from htw.mastodon import Mastodon class CLI(): @@ -24,8 +24,6 @@ def __init__(self): self.conf_path = "config.json" self.lfm_period = LFMPeriod.WEEK self.lastfm_key = getenv('LASTFM_KEY') - self.twitter_key = getenv('TWITTER_CONSUMER_KEY') - self.twitter_srt = getenv('TWITTER_CONSUMER_SECRET') self.mastodon_key = getenv('MASTODON_KEY') self.mastodon_srt = getenv('MASTODON_SECRET') self.mastodon_url = getenv('MASTODON_URL') @@ -138,7 +136,7 @@ def process_user(self, user_conf): if not self.suppress: print("- Composing tweet...") - tweet = Twitter().compose_tweet(artists, user_conf['lastfmUsername']) + tweet = Compose().compose_tweet(artists, user_conf['lastfmUsername']) if self.display_only: print(tweet) @@ -146,24 +144,6 @@ def process_user(self, user_conf): print(f"\033[92mCollage\033[00m: {pic}") colgen.cleanup() return True - - if 'twitterAccessToken' in user_conf: - if not self.suppress: - print("- Posting to \033[96mTwitter\033[00m...") - - try: - Twitter( - self.twitter_key, - self.twitter_srt, - user_conf['twitterAccessToken'], - user_conf['twitterAccessSecret'] - ).post_to_twitter(tweet, pic) - return True - except Exception as error: - print("\033[91mError\033[00m: " + str(error) + ".") - return False - finally: - colgen.cleanup() if 'mastodonAccessToken' in user_conf: if not self.suppress: @@ -200,10 +180,6 @@ def read_config(self, location): if 'lastfmKey' in conf['config']: self.lastfm_key = conf['config']['lastfmKey'] if self.lastfm_key is None else self.lastfm_key - if 'twitterConsumerKey' in conf['config']: - self.twitter_key = conf['config']['twitterConsumerKey'] if self.twitter_key is None else self.twitter_key - if 'twitterConsumerSecret' in conf['config']: - self.twitter_srt = conf['config']['twitterConsumerSecret'] if self.twitter_srt is None else self.twitter_srt if 'mastodonURL' in conf['config']: self.mastodon_url = conf['config']['mastodonURL'] if self.mastodon_url is None else self.mastodon_url @@ -219,7 +195,7 @@ def print_help(self): if self.suppress: return - print("Run without arguments to process last.fm & Twitter using environmental variables.") + print("Run without arguments to process last.fm & Mastodon using environmental variables.") print("Script will also check and use environment variables stored in '.env'.") print("") print("\033[93mOptions:\033[00m") @@ -231,7 +207,7 @@ def print_help(self): print("\033[92m-k, --keep \033[00mThe collage is dumped into the working directory instead ", end='') print("of a temporary disposable directory.") print("\033[92m-s, --silent \033[00mScript does not output anything, just success/fail code.") - print("\033[92m-d, --display \033[00mDisplays tweet, but does not post to Twitter.") + print("\033[92m-d, --display \033[00mDisplays tweet, but does not post to Mastodon.") print("") print("\033[92m-v, --version \033[00mDisplay script version.") print("\033[92m-h, --help \033[00mDisplay help information.") @@ -244,4 +220,4 @@ def print_version(self): return print("Hot this Week by soup-bowl - \033[93mpre-alpha\033[00m.") - print("https://github.com/soup-bowl/lastfm-twitter/") + print("https://github.com/soup-bowl/hot-this-week") diff --git a/htw/compose.py b/htw/compose.py new file mode 100644 index 0000000..4b87e43 --- /dev/null +++ b/htw/compose.py @@ -0,0 +1,23 @@ +""" +Contains the communication class for composing messages. +""" + +class Compose(): + """Contains the communication class for composing messages. + """ + + def compose_tweet(self, lfm_collection, name): + """Compose tweet message contents. + + Args: + lfm_collection ([type]): Last.fm user data collection. + name (str): Last.fm username. + + Returns: + [str]: Message contents. + """ + message = "\U0001F4BF my week with #lastfm:\n" + for artist in lfm_collection: + message = message + f"{artist['name']} ({artist['plays']})\n" + message = message + f"https://www.last.fm/user/{name}" + return message diff --git a/htw/twitter.py b/htw/twitter.py deleted file mode 100644 index 8e65ad2..0000000 --- a/htw/twitter.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Contains the communication class for interacting with Twitter. -""" - -from twython import Twython, TwythonError - -class Twitter(): - """Contains the communication class for interacting with Twitter. - - Args: - consumer_key (str): Twitter application credentials. - consumer_secret (str): Twitter application credentials. - access_key (str): Individual user Twitter credentials. - access_secret (str): Individual user Twitter credentials. - """ - def __init__(self, consumer_key = '', consumer_secret = '', access_key = '', access_secret = ''): - self._consumer_key = consumer_key - self._consumer_secret = consumer_secret - self._access_key = access_key - self._access_secret = access_secret - - def compose_tweet(self, lfm_collection, name): - """Compose tweet message contents. - - Args: - lfm_collection ([type]): Last.fm user data collection. - name (str): Last.fm username. - - Returns: - [str]: Message contents. - """ - message = "\U0001F4BF my week with #lastfm:\n" - for artist in lfm_collection: - message = message + f"{artist['name']} ({artist['plays']})\n" - message = message + f"https://www.last.fm/user/{name}" - return message - - def post_to_twitter(self, tweet, picture): - """Posts a message to the Twitter platform. - - Args: - tweet (str): Message contents. - picture (str): Filesystem location of the collage to attach. - """ - - twitter = Twython( - self._consumer_key, - self._consumer_secret, - self._access_key, - self._access_secret - ) - collage = open(picture, 'rb') - - try: - response = twitter.upload_media(media=collage) - twitter.update_status(status=tweet, media_ids=[response['media_id']]) - except TwythonError as error: - raise Exception("Twitter responded with an error code: " + str(error.error_code)) from error diff --git a/poetry.lock b/poetry.lock index 8e02df4..2f2a227 100644 --- a/poetry.lock +++ b/poetry.lock @@ -419,22 +419,6 @@ files = [ {file = "mccabe-0.7.0.tar.gz", hash = "sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325"}, ] -[[package]] -name = "oauthlib" -version = "3.2.2" -description = "A generic, spec-compliant, thorough implementation of the OAuth request-signing logic" -optional = false -python-versions = ">=3.6" -files = [ - {file = "oauthlib-3.2.2-py3-none-any.whl", hash = "sha256:8139f29aac13e25d502680e9e19963e83f16838d48a0d71c287fe40e7067fbca"}, - {file = "oauthlib-3.2.2.tar.gz", hash = "sha256:9859c40929662bec5d64f34d01c99e093149682a3f38915dc0655d5a633dd918"}, -] - -[package.extras] -rsa = ["cryptography (>=3.0.0)"] -signals = ["blinker (>=1.4.0)"] -signedtoken = ["cryptography (>=3.0.0)", "pyjwt (>=2.0.0,<3)"] - [[package]] name = "packaging" version = "24.1" @@ -684,24 +668,6 @@ urllib3 = ">=1.21.1,<3" socks = ["PySocks (>=1.5.6,!=1.5.7)"] use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] -[[package]] -name = "requests-oauthlib" -version = "2.0.0" -description = "OAuthlib authentication support for Requests." -optional = false -python-versions = ">=3.4" -files = [ - {file = "requests-oauthlib-2.0.0.tar.gz", hash = "sha256:b3dffaebd884d8cd778494369603a9e7b58d29111bf6b41bdc2dcd87203af4e9"}, - {file = "requests_oauthlib-2.0.0-py2.py3-none-any.whl", hash = "sha256:7dd8a5c40426b779b0868c404bdef9768deccf22749cde15852df527e6269b36"}, -] - -[package.dependencies] -oauthlib = ">=3.0.0" -requests = ">=2.0.0" - -[package.extras] -rsa = ["oauthlib[signedtoken] (>=3.0.0)"] - [[package]] name = "six" version = "1.16.0" @@ -735,21 +701,6 @@ files = [ {file = "tomlkit-0.13.0.tar.gz", hash = "sha256:08ad192699734149f5b97b45f1f18dad7eb1b6d16bc72ad0c2335772650d7b72"}, ] -[[package]] -name = "twython" -version = "3.9.1" -description = "Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs" -optional = false -python-versions = ">=3.5" -files = [ - {file = "twython-3.9.1-py3-none-any.whl", hash = "sha256:aef7ad4faabee91efcbe82a8618b38a948498fc6b3eca4cd76f642f957353818"}, - {file = "twython-3.9.1.tar.gz", hash = "sha256:5a3f0ac24d10705257028fb4205bfedf432ff28d358b796e0c2f01a2f9990c84"}, -] - -[package.dependencies] -requests = ">=2.1.0" -requests-oauthlib = ">=0.4.0" - [[package]] name = "typing-extensions" version = "4.12.2" @@ -781,4 +732,4 @@ zstd = ["zstandard (>=0.18.0)"] [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "c279cf5d30400247382630a008fe1af81d62cce487fdddb40999d956b3d88f29" +content-hash = "369c206bee4ba3f080dfb7e833d6df521cb3d4f8042ee4349c9e0700bb3cb409" diff --git a/pyproject.toml b/pyproject.toml index 84eaf23..3755910 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,6 @@ license = "MIT" [tool.poetry.dependencies] python = "^3.8" lxml = "^5.2.2" -twython = "^3.9.1" urllib3 = "^2.2.2" Pillow = "^10.0.1" "Mastodon.py" = "^1.7.0"