Skip to content

requests.exceptions.JSONDecodeError at Telegraph.create_page #56

Description

@SyberiaK

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", line474, inuser_statstelegraph_response=telegraph.create_page(stats_page_title,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.11/dist-packages/telegraph/api.py", line183, increate_pagereturnself._telegraph.method('createPage', values={
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.11/dist-packages/telegraph/api.py", line35, inmethod
).json()
^^^^^^File"/usr/local/lib/python3.11/dist-packages/requests/models.py", line975, injsonraiseRequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expectingvalue: line1column1 (char0)

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)
exceptrequests.exceptions.JSONDecodeError: # fuck ittelegraph_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.

defcreate_page(self, title, content=None, html_content=None,
author_name=None, author_url=None, return_content=False, timeout=0):
...
returnself._telegraph.method('createPage', values={
'title': title,
'author_name': author_name,
'author_url': author_url,
'content': content_json,
'return_content': return_content
}, timeout=timeout)
defmethod(self, method, values=None, path='', timeout=0):
...
response=self.session.post(
'https://api.{}/{}/{}'.format(self.domain, method, path),
data=values, timeout=timeout
).json()
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions