mercuree / html-telegraph-poster

Python html to telegra.ph poster (telegram article service)
MIT License
100 stars 31 forks source link

Exception: Error while uploading the image #20

Closed Scarlet06 closed 1 year ago

Scarlet06 commented 1 year ago

Recently, I have been experiencing an issue when trying to upload an image file using the command html_telegraph_poster.upload_images("file_path.jpg"). This issue has become more frequent, to the point where I am no longer able to upload images.

Doing some testsing I was able to discover that the result of the request for the upload was <Response [500]>. I also tried to upload the image by myself imitating the method used here (changing some parameters) but without any success.

But I've found this website, which could upload the same images without anz problem. Does anzone have anz solution for this?

mercuree commented 1 year ago

Could you provide image you try to upload?

Scarlet06 commented 1 year ago

The problem was with random images, sometimes it was uploading them and other times it was not for the same set of images.

But today, when I was testing specific files to share them with you... Everything was working fine as expected. I actually don't know what is going on, maybe a server problem with telegram? Anyway for now I'll close the issue.

Thank you very much for your time.

Scarlet06 commented 1 year ago

Sadly the problem started to show up again. Here the traceback

('Error while uploading the image', <Response [500]>)
  File "***\Main.py", line 9278, in uploading
    img = upload_image(self.images[self.i])
Exception: ('Error while uploading the image', <Response [500]>)

As I've mentioned above it happens randomly, sometime the same image is uploaded and sometimes it is rejected. As example, previously, I've managed to upload all these images without any problem. But today I couldn't reach the second image I would mention that the first image was uploaded at the third attempt. If you need, here you can find all the original images.

mercuree commented 1 year ago

I made several false assumptions:

  1. This issue is image related, because your image contained some tag information. Same issue happened when I removed all tags. So this is not the case.
  2. Browser uses http/2 while python uses http 1.1. But issue was also reproduced with http/2 enabled in chrome. Now I think this is random issue on server side. I can reproduce it with any image on https://telegra.ph too.

But after several attempts I can upload the image, so you can do something like this:

for attempt_number in range(5):
    try:
        print(f"uploading attempt {attempt_number} of 5")
        upload_image("image_name.png")
        break
    except:
        time.sleep(0.5)
Scarlet06 commented 1 year ago

So it actually is a problem with telegra.ph server! Thank you very much for your help and your time.

If at the end of the for loop it still didn't work (that can be checked by adding an else statement after it), to make sure tho that the image would be loaded anyway I thounght about doing something like that:

from base64 import b64encode
with open("image_name.png","rb") as bytefile:
    bf = b64encode(bytefile.read()).decode("utf-8")
image_source = f'"data:image/png;base64,{bf}"'

But then using img = f"<img src={image_source }>" it doesn't work. To me it doesn't look I'm doing something wrong and testing with a simple html file it seems I've done it right... Can you please confirm that the only way to add images to telegraph is by using upload_image? Or am I missing something?

ps: to be clear, to add the image to the telegraph page, I use the edit function, adding img at the current text

mercuree commented 1 year ago

@Scarlet06 I doubt that telegra.ph supports base64 images. At least there is a limit of 64kb for page content size. But you can upload images to any image hosting, which provides direct image url. This will definitely work

Scarlet06 commented 1 year ago

Thank you very much for your suggestion!