【问题标题】:Telegraph API, upload fileTelegraph API,上传文件
【发布时间】:2021-12-09 14:52:50
【问题描述】:

我正在尝试通过其 API 将图像文件上传到 Telegraph 页面。 如果我发送一个链接,一个已经上传到网络的文件的 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
  • 获取其网址

我一直在努力申请

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

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

【问题讨论】:

    标签: python telegraph


    【解决方案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 应改为使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-25
      • 2022-01-27
      • 2017-02-26
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多