Answer the question
In order to leave comments, you need to log in
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. 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)
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question