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

requests.exceptions.JSONDecodeError at Telegraph.create_page #56

Open
SyberiaK opened this issue Aug 11, 2023 · 0 comments
Open

requests.exceptions.JSONDecodeError at Telegraph.create_page #56

SyberiaK opened this issue Aug 11, 2023 · 0 comments

Comments

@SyberiaK
Copy link

I have a Telegram bot that creates Telegraph pages with some user stats.
Sometimes it raises requests.exceptions.JSONDecodeError at Telegraph.create_page:

File "/root/botty/main.py", line 474, in user_stats
    telegraph_response = telegraph.create_page(stats_page_title,
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/telegraph/api.py", line 183, in create_page
    return self._telegraph.method('createPage', values={
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/telegraph/api.py", line 35, in method
    ).json()
      ^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/requests/models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What's strange is that if you try to call Telegraph.create_page again - it runs flawlessly.
So I ended up with "fuckit" strategy:

try:
    telegraph_response = telegraph.create_page(stats_page_title,
                                               html_content=stats_page_text,
                                               author_name=bot_name,
                                               author_url=bot_link)
except requests.exceptions.JSONDecodeError:  # fuck it
    telegraph_response = telegraph.create_page(stats_page_title,
                                               html_content=stats_page_text,
                                               author_name=bot_name,
                                               author_url=bot_link)

I suspect this could be fixed if we could pass in timeout argument to inner post request.

def create_page(self, title, content=None, html_content=None,
                author_name=None, author_url=None, return_content=False, timeout=0):
    ...

    return self._telegraph.method('createPage', values={
       'title': title,
       'author_name': author_name,
       'author_url': author_url,
       'content': content_json,
       'return_content': return_content
    }, timeout=timeout)
def method(self, method, values=None, path='', timeout=0):
    ...

    response = self.session.post(
        'https://api.{}/{}/{}'.format(self.domain, method, path),
        data=values, timeout=timeout
    ).json()

   ...
Sasivarnasarma added a commit to Sasivarnasarma/telegraph that referenced this issue Apr 5, 2024
Issues python273#56 , python273#55 , python273#47 Fixed.
Added New Exception.
 __init__ file improved.
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

1 participant