首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电报API,上传文件

电报API,上传文件
EN

Stack Overflow用户
提问于 2021-12-09 14:52:50
回答 1查看 904关注 0票数 0

我试图上传一个文件,这是一个图像,通过它的API到电报页。如果我发送一个链接,一个网址到一个已经上传到网上的文件,一切都很好。

代码语言:javascript
复制
url_telegraph_API = 'https://api.telegra.ph/'

short_name = 'forecast_bot_help'
access_token = 'my_token'
path = 'Kak-byl-poluchen-prognoz-kursa-12-06-9'
just_image_url = 'https://www.import.io/wp-content/uploads/2021/02/manuel-t-xf1nszrKH_s- 
unsplash-scaled.jpg'

content_update = \
    [
    {'tag': 'strong', 'id': 'Node', 'children': ['''Конечно, никакого сигнала из Космоса не 
    было. 
    Да и ИИ, искуственный интеллект, - всего лишь генератор случайных чисел.'''], },
    {'tag': 'br'},
    {'tag': 'figure', 'children': [
    {'tag': 'img', 'attrs': {'src': just_image_url}},
    {'tag': 'figcaption'}
    ]},
    {"tag":"p","children":["test"]},
    {'tag': 'i', 'children':['''Но точность многих предсказаний курсов имееет такую же 
    ценность.''', 
     {'tag': 'i', 'children': [' В чем не сложно убедиться.']}]},
    {'tag': 'br'},
    {'tag': 'p', 'children': ['Поэтому улыбнитесь и двигайтесь дальше.']}
    ]

cont = json.dumps(content_update)
command = 'editPage?'
params = {
    'access_token': access_token,
    'path': path,
    'title': title,
    'author_name': author_name,
    'auth_url': auth_url,
    'content': cont, 
    'return_content': True
}
url = f'{url_telegraph_API}{command}'
create_page = requests.post(url, json=params)

我的问题是,是否存在的任何方法?

  • upload本地文件,通过API;
  • get及其url.

发送电报

我一直在申请

代码语言:javascript
复制
def telegraph_file_upload(file):
    
    url_telegraph_API = 'https://api.telegra.ph/'
    command = 'upload'
    params = {'file': file}
    url = f'{url_telegraph_API}{command}'
    response = requests.get(url, params=params)
    
    return response

with open('test_image.jpg', 'rb') as f:
    file = f.read()

r = telegraph_file_upload(file)
print(r.content())

但到目前为止还没有成功。有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2021-12-10 16:03:11

最后它起作用了

代码语言:javascript
复制
def telegraph_file_upload(path_to_file):
    '''
    Sends a file to telegra.ph storage and returns its url
    Works ONLY with 'gif', 'jpeg', 'jpg', 'png', 'mp4' 
    
    Parameters
    ---------------
    path_to_file -> str, path to a local file
    
    Return
    ---------------
    telegraph_url -> str, url of the file uploaded

    >>>telegraph_file_upload('test_image.jpg')
    https://telegra.ph/file/16016bafcf4eca0ce3e2b.jpg    
    >>>telegraph_file_upload('untitled.txt')
    error, txt-file can not be processed
    '''
    file_types = {'gif': 'image/gif', 'jpeg': 'image/jpeg', 'jpg': 'image/jpg', 'png': 'image/png', 'mp4': 'video/mp4'}
    file_ext = path_to_file.split('.')[-1]
    
    if file_ext in file_types:
        file_type = file_types[file_ext]
    else:
        return f'error, {file_ext}-file can not be proccessed' 
      
    with open(path_to_file, 'rb') as f:
        url = 'https://telegra.ph/upload'
        response = requests.post(url, files={'file': ('file', f, file_type)}, timeout=1)
    
    telegraph_url = json.loads(response.content)
    telegraph_url = telegraph_url[0]['src']
    telegraph_url = f'https://telegra.ph{telegraph_url}'
    
    return telegraph_url

通过API api.telagra.ph发送是我的错误。

应该使用telagra.ph

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70291947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档