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

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

(639)
Rid i books:nodejsDELETEauthorandGETbookstoshowallbooks withinfo
下一篇

相关推荐

  • css折行让文本更加美观

    示例示例CSS折行是指在CSS中使用word-wrap属性来控制文本的折行,它允许长单词或URL地址换行显示,而不会被分割。word-wrap属性可以使用以下值:…

    2023-02-20 08:58:55
    0 37 51
  • layui select 样式:1. 小学2. 初中3. 高中

    layui select 样式是指 layui 的 select 组件,它是一个可以让用户从列表中选择一个选项的组件。它可以用来替代 HTML 中的 select 元素,提供更多的功能和更好的用户体验。…

    2023-01-20 09:50:10
    0 52 31
  • css下划线颜色:Welcome to my website!

    CSS 下划线颜色可以通过使用 text-decoration 属性来改变。可以使用 `text-decoration-color` 属性来指定颜色,或者使用 `border-bottom` 属性来指定底部边框的颜色。…

    2023-01-10 07:41:30
    0 15 60
  • css设置粗体:标题

    使用css设置粗体,可以使用 font-weight 属性:也可以使用关键字:…

    2023-01-11 01:13:03
    0 23 85
  • css3.0参考手册:CSS3新特性介绍

    CSS3.0参考手册是一本专门介绍CSS3.0语法和样式的书籍,其中包含了CSS3.0的新特性、新属性、新选择器、新函数以及新媒体查询等。…

    2023-01-16 03:26:34
    0 82 22
  • css图片上怎么加文字:标题文字

    CSS图片上添加文字的方法有很多种,其中一种是使用CSS的。after伪元素。代码如下:…

    2023-03-13 12:57:53
    0 17 23
  • css图片和文字居中对齐:标题

    示例示例对于单行文字:使用`text-align: center;`来实现文字居中对齐。…

    2023-01-29 03:25:23
    0 26 59
  • css 时间轴一段发展历程

    CSS 时间轴是一种使用 CSS 来创建时间轴的技术。它可以为网站提供一个有趣的方式来展示历史事件或重要时刻,而不需要使用 JavaScript 或 Flash。…

    2023-03-18 00:39:40
    0 36 76

发表评论

登录 后才能评论

评论列表(76条)