Files
nextcloud-deck-tools/import.py
2025-09-11 23:31:07 +03:00

93 lines
2.6 KiB
Python

#!/usr/bin/env python3
import csv
import requests
import json
import argparse
import getpass
def create_card(session, domain, board_id, stack_id, title, description):
url = f"https://{domain}/index.php/apps/deck/api/v1.0/boards/{board_id}/stacks/{stack_id}/cards"
headers = {"OCS-APIRequest": "true", "Content-Type": "application/json"}
payload = {
"title": title,
"type": "plain",
"order": 0,
"description": description,
"duedate": None,
}
response = session.post(url, headers=headers, data=json.dumps(payload))
return response
def main():
parser = argparse.ArgumentParser(
description="Import tasks from a CSV file into Nextcloud Deck"
)
parser.add_argument(
"--domain",
required=True,
help="Nextcloud instance domain (e.g., example.com or https://example.com)",
)
parser.add_argument(
"--board-id",
required=True,
type=int,
help="Board ID where the cards will be created",
)
parser.add_argument(
"--stack-id",
required=True,
type=int,
help="Stack ID where the cards will be added",
)
parser.add_argument(
"--csv-file", required=True, help="Path to the CSV file containing tasks"
)
parser.add_argument(
"--username", help="Nextcloud username (if not provided, will be prompted)"
)
parser.add_argument(
"--password",
help="Nextcloud password or app password (if not provided, will be prompted)",
)
args = parser.parse_args()
# Prompt for credentials if missing
username = args.username or input("Nextcloud username: ")
password = args.password or getpass.getpass(
"Nextcloud password (or app password): "
)
session = requests.Session()
session.auth = (username, password)
with open(args.csv_file, newline="", encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
title = row.get("title", "").strip()
description = row.get("description", "").strip()
if not title:
print(f"Skipping row with missing title: {row}")
continue
response = create_card(
session, args.domain, args.board_id, args.stack_id, title, description
)
if response.status_code in (200, 201):
print(f"✔️ Successfully created card: {title}")
else:
print(
f"❌ Failed to create card: {title} - Error: {response.status_code} {response.text}"
)
if __name__ == "__main__":
main()