Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disabling Mastodon stream_user version check. #373

Open
GamePlayer-8 opened this issue Mar 22, 2024 · 7 comments
Open

Disabling Mastodon stream_user version check. #373

GamePlayer-8 opened this issue Mar 22, 2024 · 7 comments

Comments

@GamePlayer-8
Copy link

Good afternoon,

How do I disable the check of remote Mastodon instance version in Mastodon.py?

I'm using Sharkey version v2024.3.1 which contains an API mostly compatible with Mastodon API.

The current error what I'm getting is:

Traceback (most recent call last):
  File "/source/runner.py", line 218, in <module>
    setup()
  File "/source/runner.py", line 215, in setup
    main()
  File "/source/runner.py", line 194, in main
    stream = mastodon_instance.stream_user(listener, run_async=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/utility.py", line 46, in wrapper
    raise MastodonVersionError(f"Version check failed (Need version {version})")
mastodon.errors.MastodonVersionError: Version check failed (Need version 1.1.0)
@halcy
Copy link
Owner

halcy commented Mar 22, 2024

Sigh. I genuinely wish people would stop making up new creative ways of formatting the version string for the API and just put the version of the API that their server software tries to support in there, or use any of the things people already do. I already have like 3 different weird edge cases for pleroma and masto forks, apparently sharkey invented yet another way that I get to put in now ("version": "3.0.0 (compatible; Sharkey 2024.3.1)")

anyways: set "version_check_mode" to "none" (string "none", lowercase) in the constructor, that should hopefully mostly work. Some functions that disambiguate by version may not work (mostly search), use the _v1 or _v2 variants of those explicitly.

@GamePlayer-8
Copy link
Author

thank you! ❤️

Also for reporting streaming API errors should I refer to the Sharkey devs or here (in the issue tracker)?
I.e.:

Traceback (most recent call last):
  File "/source/runner.py", line 219, in <module>
    setup()
  File "/source/runner.py", line 216, in setup
    main()
  File "/source/runner.py", line 195, in main
    stream = mastodon_instance.stream_user(listener, run_async=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/utility.py", line 49, in wrapper
    return function(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/streaming_endpoints.py", line 21, in stream_user
    return self.__stream('/api/v1/streaming/user', listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/internals.py", line 429, in __stream
    url = self.__get_streaming_base()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/internals.py", line 403, in __get_streaming_base
    instance = self.instance()
               ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/utility.py", line 49, in wrapper
    return function(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/instance.py", line 24, in instance
    return self.__instance()
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/instance.py", line 30, in __instance
    instance = self.__api_request('GET', '/api/v1/instance/')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/internals.py", line 297, in __api_request
    raise ex_type('Mastodon API returned error', response_object.status_code, response_object.reason, error_msg)
mastodon.errors.MastodonNotFoundError: ('Mastodon API returned error', 404, 'Not Found', {'message': 'Unknown API endpoint.', 'code': 'UNKNOWN_API_ENDPOINT', 'id': '2ca3b769-540a-4f08-9dd5-b5a825b6d0f1', 'kind': 'client'})

@halcy
Copy link
Owner

halcy commented Mar 22, 2024

Ah, hm. Maybe Sharkey implements only the v2 version of the instance API, not the v1. Since Mastodon does not remove the old ones, I currently do not take that into account. I should probably do that.

Both would be good, probably, ideally they should support v1 for compat, and I should obviously use v2 when v1 is not available.

Currently once again busy with other stuff, so I can't promise a timeline on a fix, but if you want to quickly kludge it yourself, you probably can make it work by looking at api docs and swapping URLs around (maybe even just v1 to v2 in there is good enough, not sure)

Thank you, in any case, for the report!

@halcy halcy reopened this Mar 22, 2024
@GamePlayer-8
Copy link
Author

Currently once again busy with other stuff, so I can't promise a timeline on a fix, but if you want to quickly kludge it yourself, you probably can make it work by looking at api docs and swapping URLs around (maybe even just v1 to v2 in there is good enough, not sure)

Is that possible to specify which version API (v1 or v2) can be used by Mastodon.py or do I need to proxy /api/v1 directive to /api/v2 at the server side?

@halcy
Copy link
Owner

halcy commented Mar 25, 2024

it's per endpoint, there's (intended to be a) _v1 and _v2 version for functions that have multiple variants and one without suffix that selects dynamically, but I think in the current stable version on pip, I didn't implement that for the v2 instance API yet (and in any case, the reason it fails for streaming is that the streaming API calls this internally because I needs to get the streaming endpoints from the return value, so you actually couldn't fix it that way). I will, eventually, but I keep getting sidetracked. As a workaround, you could edit your local copy of Mastodon.py and replace /v1/ with /v2/ in the "__instance" function (may break things) or, as you said, proxy rewrite it.

@GamePlayer-8
Copy link
Author

unfortunately v1 seems to have differ from v2 a bit more:

Traceback (most recent call last):
  File "/home/gameplayer/workspace/followatch-sharkey/app/runner.py", line 223, in <module>
    setup()
  File "/home/gameplayer/workspace/followatch-sharkey/app/runner.py", line 214, in setup
    main()
  File "/home/gameplayer/workspace/followatch-sharkey/app/runner.py", line 187, in main
    mastodon_instance.status_post('**FolloWatch** Booted up.', visibility='direct')
  File "/usr/lib/python3.11/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/utility.py", line 49, in wrapper
    return function(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/statuses.py", line 248, in status_post
    return self.__status_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/statuses.py", line 182, in __status_internal
    return self.__api_request('POST', '/api/v1/statuses', params, headers=headers, use_json=use_json)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mastodon/internals.py", line 297, in __api_request
    raise ex_type('Mastodon API returned error', response_object.status_code, response_object.reason, error_msg)
mastodon.errors.MastodonNotFoundError: ('Mastodon API returned error', 404, 'Not Found', 'Not Found')

As a workaround, you could edit your local copy of Mastodon.py and replace /v1/ with /v2/ in the "__instance" function

Will try that now.

@GamePlayer-8
Copy link
Author

('Mastodon API returned error', 404, 'Not Found', AttribAccessDict([('message', 'Unknown API endpoint.'), ('code', 'UNKNOWN_API_ENDPOINT'), ('id', '2ca3b769-540a-4f08-9dd5-b5a825b6d0f1'), ('kind', 'client')]))
Traceback (most recent call last):
  File "/home/gameplayer/workspace/followatch-sharkey/app/runner.py", line 224, in <module>
    setup()
  File "/home/gameplayer/workspace/followatch-sharkey/app/runner.py", line 215, in setup
    main()
  File "/home/gameplayer/workspace/followatch-sharkey/app/runner.py", line 194, in main
    stream = mastodon_instance.stream_user(listener, run_async=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/decorator.py", line 232, in fun
    return caller(func, *(extras + args), **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gameplayer/workspace/followatch-sharkey/app/mastodon/versions.py", line 43, in wrapper
    return function(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gameplayer/workspace/followatch-sharkey/app/mastodon/streaming_endpoints.py", line 21, in stream_user
    return self.__stream('/api/v2/streaming/user', listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gameplayer/workspace/followatch-sharkey/app/mastodon/internals.py", line 370, in __stream
    url = self.__get_streaming_base()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gameplayer/workspace/followatch-sharkey/app/mastodon/internals.py", line 343, in __get_streaming_base
    instance = self.__instance()
               ^^^^^^^^^^^^^^^^^
  File "/home/gameplayer/workspace/followatch-sharkey/app/mastodon/instance.py", line 35, in __instance
    instance = self.__api_request('GET', '/api/v2/instance/', override_type=Instance)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gameplayer/workspace/followatch-sharkey/app/mastodon/internals.py", line 252, in __api_request
    raise ex_type('Mastodon API returned error', response_object.status_code, response_object.reason, error_msg)
mastodon.errors.MastodonNotFoundError: ('Mastodon API returned error', 404, 'Not Found', AttribAccessDict([('message', 'Unknown API endpoint.'), ('code', 'UNKNOWN_API_ENDPOINT'), ('id', '2ca3b769-540a-4f08-9dd5-b5a825b6d0f1'), ('kind', 'client')]))

replacing v1 with v2 in the source code causes 404 error. Will probably forward the issue as well to the Sharkey devs.

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

No branches or pull requests

2 participants