python273 / telegraph

Telegraph API wrapper | Telegra.ph
https://pypi.org/project/telegraph/
MIT License
289 stars 45 forks source link

How can I upload images to telegraph via this library? #3

Closed f126ck closed 7 years ago

f126ck commented 7 years ago

Noob here. I wanted to know if it was possible to make a telegraph article including photos/videos which I have got on my hard disk.

How can I upload them and show in the article?

Thanks

python273 commented 7 years ago

It's not possible to upload photos/videos via API, but you can use this code

import requests

with open('/Users/python273/Desktop/123345.jpeg', 'rb') as f:
    print(
        requests.post(
            'http://telegra.ph/upload',
            files={'file': ('file', f, 'image/jpeg')}  # image/gif, image/jpeg, image/jpg, image/png, video/mp4
        ).json()
    )
f126ck commented 7 years ago

Thank you very very very much :)

mieburungdara commented 6 years ago

how to use this in php?

import requests

with open('/Users/python273/Desktop/123345.jpeg', 'rb') as f:
    print(
        requests.post(
            'http://telegra.ph/upload',
            files={'file': ('file', f, 'image/jpeg')}  # image/gif, image/jpeg, image/jpg, image/png, video/mp4
        ).json()
    )
deantipin commented 6 years ago

@f126ck Could you, please, share your code? What should I put instead of 'file'? - I've tried every possible combination of my file name and it doesn't work..

deantipin commented 6 years ago

@python273 Could you tell me, please, what would I change in order to make the post with picture? I used your code for example. I will be waiting for your response. Thank you!

from telegraph import Telegraph

telegraph = Telegraph()

telegraph.create_account(short_name='1337')

response = telegraph.create_page(
    'Hey',
    html_content='<p>Hello, world!</p>'
)

with open('/Users/deantipin/picture.png', 'rb') as f:
    print(
        requests.post(
            'http://telegra.ph/upload',
            files={'Hey': ('Hey', f, 'image/png')}  # image/gif, image/jpeg, image/jpg, image/png, video/mp4
        ).json()
    )
print('http://telegra.ph/{}'.format(response['path']))
n0execution commented 6 years ago

@deantipin maybe i'm late, but it should work

from telegraph import Telegraph

telegraph = Telegraph()

telegraph.create_account(short_name='1337')

with open('/Users/deantipin/picture.png', 'rb') as f:
    path = requests.post(
                    'https://telegra.ph/upload', files={'file': 
                                                        ('file', f, 
                                                        'image/jpeg')}).json()[0]['src']

response = telegraph.create_page(
    'Hey',
    html_content="<p>Hello, world!</p> \
                  <img src='{}'/>".format(path),
)

print('http://telegra.ph/{}'.format(response['path']))
greatspoke commented 6 years ago

Hello! Guys its possible posting to telegra.ph but from url via node js? I need posting to telegraph picture not form hard disk, from url only Thank you

python273 commented 6 years ago

@greatspoke you can download the picture and upload to telegraph

apcel commented 6 years ago

Hey guys Just in case telegraph has suddenly started returning "error: no files passed": it looks like telegraph has moved to https (finally!), so http://telegra.ph/upload from the examples above should be changed to https://telegra.ph/upload

greatspoke commented 6 years ago

I mean use with https://www.npmjs.com/package/telegraph-node try

ph.createPage(token, 'bot_greatspoke', ['[url]https://goo.gl/FHcL485[/url]'], { return_content: true }).then((result) => { console.log(result) })

But not work(((

jzr-supove commented 4 years ago

Using http I'm getting: response [200] with {"error":"No files passed"} Using https I'm getting: response [500]

What I'm doing wrong?

macsunmood commented 4 years ago

It's not possible to upload photos/videos via API, but you can use this code

import requests

with open('/Users/python273/Desktop/123345.jpeg', 'rb') as f:
    print(
        requests.post(
            'http://telegra.ph/upload',
            files={'file': ('file', f, 'image/jpeg')}  # image/gif, image/jpeg, image/jpg, image/png, video/mp4
        ).json()
    )

Hello, @python273! Have been trying to upload an image both via requests.post() (as featured in your example) and telegraph.upload.upload_file(f) from your library, but I keep always getting the following error:

SSLError: HTTPSConnectionPool(host='telegra.ph', port=443): Max retries exceeded with url: /upload (Caused by SSLError(SSLError("bad handshake: SysCallError(10054, 'WSAECONNRESET')")))

No googling of the issue helped so far. :( Please, can anything be done about it ?

python273 commented 4 years ago

Seems like /upload is not part of the API, so not really sure about 500 and other errors

SSLError: HTTPSConnectionPool(host='telegra.ph', port=443): Max retries exceeded with url: /upload (Caused by SSLError(SSLError("bad handshake: SysCallError(10054, 'WSAECONNRESET')")))

Maybe telegra.ph is blocked for you, so you need to use proxy

giangdinh commented 3 years ago

I face this issue: https://github.com/python273/telegraph/issues/36

Does anyone here face this? I do the same code that mentions this issue, it works fine for image but error for video/mp4.

shizomontazh commented 2 years ago

@GooDeeJaY Maybe telegra.ph is blocked for you Use te.legra.ph in this case

227234 commented 1 year ago

https://github.com/python273/telegraph/issues/3#issuecomment-418197910

227234 commented 1 year ago

https://github.com/python273/telegraph/issues/3#issuecomment-583859146

227234 commented 1 year ago

https://github.com/python273/telegraph/issues/3#issuecomment-1017489272

Maniac1488ss commented 1 year ago

53