1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
import argparse
import base64
import json
import os
import sys
from pathlib import Path
from typing import Any
from urllib.parse import quote
from xml.etree import ElementTree
from dotenv import load_dotenv
from mutagen.flac import FLAC
import requests
from tqdm import tqdm
load_dotenv()
def upload_to_nextcloud(
file_path: str | Path,
remote_path: str,
nextcloud_url: str | None = None,
username: str | None = None,
password: str | None = None,
) -> str:
nextcloud_url = (nextcloud_url or os.environ["NEXTCLOUD_URL"]).rstrip("/")
username = username or os.environ["NEXTCLOUD_USERNAME"]
password = password or os.environ["NEXTCLOUD_PASSWORD"]
file_path = Path(file_path)
if not file_path.is_file():
raise FileNotFoundError(file_path)
remote_path = remote_path.strip("/")
dav_base = (
f"{nextcloud_url}/remote.php/dav/files/"
f"{quote(username, safe='')}"
)
current_path = dav_base
for directory in remote_path.split("/"):
current_path += f"/{quote(directory)}"
response = requests.request(
"MKCOL",
current_path,
auth=(username, password),
)
# 201 = created
# 405 = already exists
if response.status_code not in (201, 405):
raise RuntimeError(
f"Failed to create directory '{directory}': "+
f"{response.status_code} {response.text}"
)
filename = file_path.name
upload_url = (
f"{dav_base}/"
f"{'/'.join(quote(p) for p in remote_path.split('/'))}/"
f"{quote(filename)}"
)
with file_path.open("rb") as f:
response = requests.put(
upload_url,
auth=(username, password),
data=f,
headers={
"Content-Type": "application/octet-stream",
},
)
if response.status_code not in (200, 201, 204):
raise RuntimeError(
f"Failed to upload file: " +
f"{response.status_code} {response.text}"
)
share_api = (
f"{nextcloud_url}/ocs/v2.php/apps/files_sharing/"
f"api/v1/shares"
)
share_path = f"/{remote_path}/{filename}"
response = requests.post(
share_api,
auth=(username, password),
headers={
"OCS-APIRequest": "true",
"Accept": "application/xml",
},
data={
"path": share_path,
"shareType": "3", # Public link
"permissions": "1", # Read-only
},
)
if not response.ok:
raise RuntimeError(
f"Failed to create share: " +
f"{response.status_code} {response.text}"
)
try:
root = ElementTree.fromstring(response.text)
url_element = root.find(".//url")
if url_element is None or not url_element.text:
raise RuntimeError(
f"Share was created but no URL was returned: "+
f"{response.text}"
)
return url_element.text
except ElementTree.ParseError as e:
raise RuntimeError(
f"Invalid response from Nextcloud: {response.text}"
) from e
def get_all_flac_files(directory: str) -> list[str]:
flac_files = []
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".flac"):
flac_files.append(os.path.join(root, file))
return flac_files
def get_flac_metadata(file_path: str | Path) -> dict[str, Any]:
audio = FLAC(str(file_path))
return {
"title": audio.get("title", [None])[0],
"artist": audio.get("artist", [None])[0],
"album": audio.get("album", [None])[0],
}
def main():
parser = argparse.ArgumentParser(
description="Upload an album folder (FLAC files + cover.jpg) to Nextcloud and output base64-encoded album JSON."
)
_ = parser.add_argument(
"folder",
type=str,
help="Path to local folder containing FLAC files and cover.jpg"
)
_ = parser.add_argument(
"remote_path",
type=str,
help="Destination remote path on Nextcloud"
)
_ = parser.add_argument(
"--nextcloud-url",
type=str,
default=None,
help="Nextcloud base URL"
)
_ = parser.add_argument(
"--username",
type=str,
default=None,
help="Nextcloud username"
)
_ = parser.add_argument(
"--password",
type=str,
default=None,
help="Nextcloud password"
)
args = parser.parse_args()
local_dir = Path(args.folder)
if not local_dir.is_dir():
raise NotADirectoryError(f"Local directory not found: {local_dir}")
cover_path = local_dir / "cover.jpg"
if not cover_path.is_file():
raise FileNotFoundError(f"cover.jpg not found in {local_dir}")
print(f"Scanning '{local_dir}' for FLAC files...", file=sys.stderr)
flac_files = get_all_flac_files(str(local_dir))
if not flac_files:
raise RuntimeError(f"No FLAC files found in {local_dir}")
flac_files = sorted(flac_files)
print(f"Found {len(flac_files)} FLAC track(s).", file=sys.stderr)
print("Uploading cover.jpg...", file=sys.stderr)
cover_url = upload_to_nextcloud(
cover_path,
args.remote_path,
nextcloud_url=args.nextcloud_url,
username=args.username,
password=args.password,
)
cover_url = cover_url.rstrip("/")
if not cover_url.endswith("/download"):
cover_url += "/download"
print("Cover uploaded and shared successfully.", file=sys.stderr)
album_name = None
artist = None
tracks = []
with tqdm(flac_files, desc="Uploading tracks", unit="track") as pbar:
for flac_file in pbar:
track_path = Path(flac_file)
pbar.set_postfix(file=track_path.name)
meta = get_flac_metadata(flac_file)
if not album_name and meta.get("album"):
album_name = meta["album"]
if not artist and meta.get("artist"):
artist = meta["artist"]
track_url = upload_to_nextcloud(
flac_file,
args.remote_path,
nextcloud_url=args.nextcloud_url,
username=args.username,
password=args.password,
)
track_url = track_url.rstrip("/")
if not track_url.endswith("/download"):
track_url += "/download"
title = meta.get("title") or track_path.stem
tracks.append({
"title": title,
"url": track_url
})
album_data = {
"album_name": album_name or local_dir.name,
"artist": artist or "Unknown Artist",
"album_art": cover_url,
"tracks": tracks
}
print("Generating base64 encoded album JSON...", file=sys.stderr)
json_str = json.dumps(album_data, indent=2, ensure_ascii=False)
b64_encoded = base64.b64encode(json_str.encode("utf-8")).decode("utf-8")
print(f"https://audio.pinapelz.com/?data={b64_encoded}")
if __name__ == "__main__":
main()
|