Coin telegraph:电报API 上传文件(telegraph gif)

我试图通过它的 API 上传一个文件,这是一个图像到电报页面。如果我发送一个链接,一个 URL 到已经上传到网络的文件,一切工作正常。

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)

我的问题是是否存在任

通过 API 将本地文件上传到 Telegraph

获取其 URL

我一直在尝试申请

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())

但是到目前为止还没有成功。知道吗?

1

最终它的工作原理

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

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(501)
网易博客代码:博客-博客帖子-博客评论vs博客-帖子-评论
上一篇
广告招牌样式:自动广告预览不显示样式-以前工作正常
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(53条)