robertoszek / pleroma-bot

Bot for mirroring one or multiple Twitter accounts in Pleroma/Mastodon/Misskey.
https://robertoszek.github.io/pleroma-bot
MIT License
104 stars 18 forks source link

Multiple video attachments cause 422 on Mastodon #92

Closed robertoszek closed 1 year ago

robertoszek commented 1 year ago

Twitter only allows posting one video per tweet but due to how we handle RTs and quote tweets, we gather all attachments and add them to the Fedi post. So if a RT or quote tweet includes a video and it's quoting/retweeting a tweet also containing a video it will result on 2 video attachments being uploaded and assigned to the Fediverse post.

This is not an issue on Pleroma or Misskey instances, as they support multiple videos per post but it causes issues on Mastodon instances (error 422 returned).

We already have some limits in place when the instance detection logic finds that the target is Mastodon (character limit is set to 500 and max attachments is limited to 4), so we'll add to it and only apply this restriction for Mastodon instances.

I'm thinking the video on the newest retweet/quote should take precedence over the referenced ones, it's a shame to drop attachments but there's no way around it with how Mastodon works at the moment.

robertoszek commented 1 year ago

I've committed the changes to the develop branch: ab4e540138ddbea2455206113d50f7522ee7f2ee

They are included in 1.1.1rc29: pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple pleroma-bot==1.1.1rc29

dawnerd commented 1 year ago

Not sure if related but I saw it mentioned in the other 422 issue. Getting the same Unprocessable Entity

2022-12-03 07:59:32,689 pleroma_bot ERROR: Exception occurred for user, skipping...
Traceback (most recent call last):
  File "/home/troy/.local/lib/python3.6/site-packages/pleroma_bot/cli.py", line 685, in main
    tweets_to_post["media_processed"]
  File "/home/troy/.local/lib/python3.6/site-packages/pleroma_bot/_utils.py", line 766, in post
    post_id = self.post_pleroma(tweet, poll, sensitive, media)
  File "/home/troy/.local/lib/python3.6/site-packages/pleroma_bot/_pleroma.py", line 314, in post_pleroma
    response.raise_for_status()
  File "/home/troy/.local/lib/python3.6/site-packages/requests/models.py", line 960, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://opencoaster.net/api/v1/statuses

Verbose output: https://gist.github.com/dawnerd/9ef8c6167b2eed2201a1e206b597b135

Since it was the first run on that particular user it didn't even sync the bio or images, otherwise I'd be fine with it skipping posts it can't support.

Running that rc42 version, which btw doing a great job keeping the dupes at bay!

robertoszek commented 1 year ago

@dawnerd Hmm... the attachments by themselves don't seem to have any trouble uploading (at least in the Mastodon instance I've tested). In some of the tweets there is a mix of 1 picture and 1 video, I'm wondering if it has to do with the video attachment being first. Curiously enough I'm able to add images in Mastodon and then a video, but not the other way around. Once a video is attached no more media can be uploaded.

Perhaps you can verify my theory by running 1.1.1rc44: pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple pleroma-bot==1.1.1rc44

And you can try reposting the tweets on the log you shared by including them in the config of your corresponding user:

pleroma_base_url: https://pleroma.instance
users:
- twitter_username: User1
  pleroma_username: MyPleromaUser1
  pleroma_token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  tweet_ids:
    - 1598723669294882816
    - 1598702545865670659
    - 1598701020108001281
    - 1598693917834330116
    - 1598643641924259840
    - 1598634807503618048
    - 1598624589629865985
    - 1598565207202099200
    - 1598376386879582208
    - 1598285100617179136
    - 1598267410708299778
dawnerd commented 1 year ago

https://gist.github.com/dawnerd/06b13844f7833d7c8797eb95995d3358

Yeah breaks there on rc44

robertoszek commented 1 year ago

@dawnerd Right, the order doesn't seem to matter in the end, the error message in the response from Mastodon is quite explicit: 422 Cannot attach a video to a post that already contains images

So a Mastodon post can either have one single video or images as attachments but not both mixed together.

I only see one way out of this limitation:

https://github.com/robertoszek/pleroma-bot/commit/fd379fcce41f3c3b7167744870bfc1b44256ef42

You can try it out with 1.1.1rc45: pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple pleroma-bot==1.1.1rc45

dawnerd commented 1 year ago

@robertoszek that seemed to work great! 🙌

I think your solution is a pretty great compromise. Thanks a lot for looking into it!