fix: stylize examples
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import os
|
||||
from argparse import ArgumentParser
|
||||
from os import getenv
|
||||
from os.path import isfile
|
||||
import sys
|
||||
|
||||
from itd import ITDClient
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
parser = ArgumentParser(
|
||||
description='Upload image and set it as profile banner'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--token',
|
||||
default=os.getenv('ITD_TOKEN'),
|
||||
default=getenv('ITD_TOKEN'),
|
||||
help='API token (or ITD_TOKEN env var)'
|
||||
)
|
||||
|
||||
@@ -22,56 +23,33 @@ def main():
|
||||
help='Path to image file'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--name',
|
||||
help='Filename on server (default: local filename)'
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.token:
|
||||
print('❌ Токен не задан (--token или ITD_TOKEN)', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
quit()
|
||||
|
||||
file_path = args.file
|
||||
|
||||
if not os.path.isfile(file_path):
|
||||
if not isfile(file_path):
|
||||
print(f'❌ Файл не найден: {file_path}', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
server_name = args.name or os.path.basename(file_path)
|
||||
quit()
|
||||
|
||||
try:
|
||||
client = ITDClient(None, args.token)
|
||||
|
||||
# Загружаем файл
|
||||
with open(file_path, 'rb') as f:
|
||||
response = client.upload_file(server_name, f)
|
||||
|
||||
# Проверяем, что получили id
|
||||
file_id = getattr(response, 'id', None)
|
||||
if file_id is None:
|
||||
print('❌ Не удалось получить id файла')
|
||||
print(response)
|
||||
sys.exit(1)
|
||||
|
||||
# Преобразуем UUID в строку
|
||||
file_id_str = str(file_id)
|
||||
|
||||
# Обновляем баннер
|
||||
update_resp = client.update_profile(banner_id=file_id_str)
|
||||
data, _ = client.update_banner_new(file_path)
|
||||
|
||||
print('✅ Баннер обновлён!')
|
||||
print('📄 Информация о файле:')
|
||||
print(f' id: {file_id_str}')
|
||||
print(f' filename: {response.filename}')
|
||||
print(f' mime_type: {response.mime_type}')
|
||||
print(f' size: {response.size} bytes')
|
||||
print(f' url: {response.url}')
|
||||
print(f' id: {data.id}')
|
||||
print(f' filename: {data.filename}')
|
||||
print(f' mime_type: {data.mime_type}')
|
||||
print(f' size: {data.size} bytes')
|
||||
print(f' url: {data.url}')
|
||||
|
||||
except Exception as e:
|
||||
print('❌ Произошла ошибка:', e, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
quit()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
#!/usr/bin/env python3
|
||||
from uuid import UUID
|
||||
from argparse import ArgumentParser
|
||||
from os import getenv
|
||||
from os.path import isfile, basename
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
from itd import ITDClient
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
parser = ArgumentParser(
|
||||
description='Create a post on ITD via CLI'
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
'--token',
|
||||
default=os.getenv('ITD_TOKEN'),
|
||||
default=getenv('ITD_TOKEN'),
|
||||
help='Refresh token (or set ITD_TOKEN environment variable)'
|
||||
)
|
||||
|
||||
@@ -35,46 +36,43 @@ def main():
|
||||
args = parser.parse_args()
|
||||
|
||||
if not args.token:
|
||||
print('❌ Token not provided (--token or ITD_TOKEN)', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
print('❌ Token not provided (--token or ITD_TOKEN)')
|
||||
quit()
|
||||
|
||||
try:
|
||||
client = ITDClient(None, args.token)
|
||||
|
||||
file_id = None
|
||||
if args.file:
|
||||
if not os.path.isfile(args.file):
|
||||
print(f'❌ File not found: {args.file}', file=sys.stderr)
|
||||
sys.exit(1)
|
||||
if not isfile(args.file):
|
||||
print(f'❌ File not found: {args.file}')
|
||||
quit()
|
||||
|
||||
server_name = args.filename or os.path.basename(args.file)
|
||||
server_name = args.filename or basename(args.file)
|
||||
with open(args.file, 'rb') as f:
|
||||
response = client.upload_file(server_name, f)
|
||||
|
||||
file_id = str(getattr(response, 'id', None))
|
||||
if not file_id:
|
||||
print('❌ Failed to get file ID')
|
||||
sys.exit(1)
|
||||
quit()
|
||||
print(f'✅ File uploaded: {response.filename} (id={file_id})')
|
||||
|
||||
# Создаём пост с правильным аргументом 'content'
|
||||
if file_id:
|
||||
post_resp = client.create_post(content=args.text, file_ids=[file_id])
|
||||
post_resp = client.create_post(content=args.text, attach_ids=[UUID(file_id)])
|
||||
else:
|
||||
post_resp = client.create_post(content=args.text)
|
||||
|
||||
# Вывод результата
|
||||
print('✅ Post created successfully!')
|
||||
print(f' id: {post_resp.id}')
|
||||
if hasattr(post_resp, 'url'):
|
||||
print(f' url: {post_resp.url}')
|
||||
print(f' text: {args.text}')
|
||||
if file_id:
|
||||
print(f' attached file id: {file_id}')
|
||||
|
||||
except Exception as e:
|
||||
print('❌ Error:', e, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
print('❌ Error:', e)
|
||||
quit()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user