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

How to upload files (images, etc.) #30

Open
vovkapultik opened this issue Sep 5, 2020 · 3 comments
Open

How to upload files (images, etc.) #30

vovkapultik opened this issue Sep 5, 2020 · 3 comments

Comments

@vovkapultik
Copy link

vovkapultik commented Sep 5, 2020

It's not an issue, I just want to share this code

from telegraph import Telegraph, upload

telegraph = Telegraph()
telegraph.create_account(short_name='LetsTry')

def post(title, content):
	response = telegraph.create_page(title,
									 html_content = content)
	return 'https://telegra.ph/{}'.format(response['path'])

files = '/Users/vovkapultik/Downloads/52920087_159420908268041_6560105268061929472_n.jpg'

imgpath = upload.upload_file(files)

print(imgpath)

#['/file/02a1613fc106b225b2b74.jpg']

postlink = post('Title', '<img src="/file/02a1613fc106b225b2b74.jpg">')

print(postlink)
@MuuJian
Copy link

MuuJian commented Jul 7, 2022

imgpath = upload.upload_file(files)[0]['src']
postlink = post('Title', imgpath)

can write like this, bc upload_file() return a list of dicts with src key. (sorry, my english is poor.

@Suharaz
Copy link

Suharaz commented Dec 9, 2022

Is there a way to upload multiple photos at once? bro

@Sonico98
Copy link

Is there a way to upload multiple photos at once? bro

# Importing required package
from telegraph.aio import Telegraph
from pathlib import Path
import asyncio

# Declaring asynchronous function for using await
async def main():
    # Creating new Telegraph object
    telegraph = Telegraph()
    # Creating new account
    await telegraph.create_account("Test", author_name="Test")

    # Upload images in directory.
    image_dir = Path("~/Pictures/")
    uploaded_files = ""
    for path_object in image_dir.rglob("*"):
        if path_object.is_file():
            try:
                file = await telegraph.upload_file(path_object)
                uploaded_files += f'<img src={file[0]["src"]}>'
            except:
                print(f"Image {path_object} is probably too big")

    # Creating new page
    if uploaded_files != "":
        new_page = await telegraph.create_page(
            "Bulk",
            html_content=uploaded_files
        )
        # Printing page url into console
        print(new_page["url"])


# Running asynchronous function
asyncio.run(main())

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

4 participants