E
E
EnderToy2021-10-17 15:10:12
Python
EnderToy, 2021-10-17 15:10:12

Why is this query not working correctly?

In short, I need to upload a few photos to the telegraph article. Here is a working version:

with open('путь к файлу', 'rb') as f:
    path = requests.post(
                    'https://telegra.ph/upload', files={'file':
                                                        ('file', f,
                                                        'image')}).json()[0]['src']
params = {
    'access_token': "мой токен",
    'path': 'путь',
    'title': 'My Title',
    'content':[{
            "tag":"figure",
            "children":[
               {
                  "tag":"img",
                  "attrs":{
                     "src":path
                  }
               }
            ]
         }],
    'author_name': 'My Name',
    'author_url': 'https://remanga.org/team/peacekeepingsquad',
    'return_content': 'true'
}

url = 'https://api.telegra.ph/createPage'
edit = 'https://api.telegra.ph/editPage/'

r = requests.post(edit, json=params)
r.raise_for_status()
response = r.json()
print(response)
, but in this case one image is loaded. We need a code that would load all the photos from the folder.
I wrote something, but when I try to pass it as content, something goes wrong.
The code:
import os
 directory = 'путь'
 included_extensions = ['jpg','jpeg', 'bmp', 'png', 'gif']
 file_names = [fn for fn in os.listdir(directory)
               if any(fn.endswith(ext) for ext in included_extensions)]
 img = []

 for x in file_names:
     with open(f'{directory}\\{x}', 'rb') as f:
         path = requests.post(
                         'https://telegra.ph/upload', files={'file':
                                                             ('file', f,
                                                             'image')}).json()[0]['src']
         img.append('{"tag":"figure","children":[{"tag":"img","attrs":{"src":'+path+'}}}')
         img_list= ",".join(img)

An error occurs when trying to pass this string.
params = {
    'access_token': "токен",
    'path': '/Sample-Page-10-17-27',
    'title': 'My Title',
    'content':[img_list],
    'author_name': 'My Name',
    'author_url': 'https://remanga.org/team/peacekeepingsquad',
    'return_content': 'true'
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2021-10-17
@karabanov

Source code for telegraph.upload

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question