TonieToolbox 0.5.0a1__py3-none-any.whl → 0.6.0a1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,691 @@ from .teddycloud import TeddyCloudClient
21
21
 
22
22
  logger = get_logger('tonies_json')
23
23
 
24
- class ToniesJsonHandler:
24
+ class ToniesJsonHandlerv1:
25
+ """Handler for tonies.custom.json operations using v1 format."""
26
+
27
+ def __init__(self, client: TeddyCloudClient = None):
28
+ """
29
+ Initialize the handler.
30
+
31
+ Args:
32
+ client (TeddyCloudClient | None): TeddyCloudClient instance to use for API communication
33
+ """
34
+ self.client = client
35
+ self.custom_json = []
36
+ self.is_loaded = False
37
+
38
+ def load_from_server(self) -> bool:
39
+ """
40
+ Load tonies.custom.json from the TeddyCloud server.
41
+
42
+ Returns:
43
+ bool: True if successful, False otherwise
44
+ """
45
+ if self.client is None:
46
+ logger.error("Cannot load from server: no client provided")
47
+ return False
48
+
49
+ try:
50
+ result = self.client.get_tonies_custom_json()
51
+ if result is not None:
52
+ # Convert v2 format to v1 format if necessary
53
+ if len(result) > 0 and "data" in result[0]:
54
+ logger.debug("Converting v2 format from server to v1 format")
55
+ self.custom_json = self._convert_v2_to_v1(result)
56
+ else:
57
+ self.custom_json = result
58
+ self.is_loaded = True
59
+ logger.info("Successfully loaded tonies.custom.json with %d entries", len(self.custom_json))
60
+ return True
61
+ else:
62
+ logger.error("Failed to load tonies.custom.json from server")
63
+ return False
64
+
65
+ except Exception as e:
66
+ logger.error("Error loading tonies.custom.json: %s", e)
67
+ return False
68
+
69
+ def load_from_file(self, file_path: str) -> bool:
70
+ """
71
+ Load tonies.custom.json from a local file.
72
+
73
+ Args:
74
+ file_path (str): Path to the tonies.custom.json file
75
+
76
+ Returns:
77
+ bool: True if successful, False otherwise
78
+ """
79
+ try:
80
+ if os.path.exists(file_path):
81
+ logger.info("Loading tonies.custom.json from file: %s", file_path)
82
+ with open(file_path, 'r', encoding='utf-8') as f:
83
+ data = json.load(f)
84
+ if isinstance(data, list):
85
+ # Convert v2 format to v1 format if necessary
86
+ if len(data) > 0 and "data" in data[0]:
87
+ logger.debug("Converting v2 format from file to v1 format")
88
+ self.custom_json = self._convert_v2_to_v1(data)
89
+ else:
90
+ self.custom_json = data
91
+ self.is_loaded = True
92
+ logger.info("Successfully loaded tonies.custom.json with %d entries", len(self.custom_json))
93
+ return True
94
+ else:
95
+ logger.error("Invalid tonies.custom.json format in file, expected list")
96
+ return False
97
+ else:
98
+ logger.info("tonies.custom.json file not found, starting with empty list")
99
+ self.custom_json = []
100
+ self.is_loaded = True
101
+ return True
102
+
103
+ except Exception as e:
104
+ logger.error("Error loading tonies.custom.json from file: %s", e)
105
+ return False
106
+
107
+ def save_to_file(self, file_path: str) -> bool:
108
+ """
109
+ Save tonies.custom.json to a local file.
110
+
111
+ Args:
112
+ file_path (str): Path where to save the tonies.custom.json file
113
+
114
+ Returns:
115
+ bool: True if successful, False otherwise
116
+ """
117
+ if not self.is_loaded:
118
+ logger.error("Cannot save tonies.custom.json: data not loaded")
119
+ return False
120
+
121
+ try:
122
+ os.makedirs(os.path.dirname(os.path.abspath(file_path)), exist_ok=True)
123
+ logger.info("Saving tonies.custom.json to file: %s", file_path)
124
+ with open(file_path, 'w', encoding='utf-8') as f:
125
+ json.dump(self.custom_json, f, indent=2, ensure_ascii=False)
126
+
127
+ logger.info("Successfully saved tonies.custom.json to file")
128
+ return True
129
+
130
+ except Exception as e:
131
+ logger.error("Error saving tonies.custom.json to file: %s", e)
132
+ return False
133
+
134
+ def renumber_series_entries(self, series: str) -> None:
135
+ """
136
+ Re-sort and re-number all entries for a series by year (chronological),
137
+ with entries without a year coming last.
138
+
139
+ Args:
140
+ series (str): Series name to renumber
141
+ """
142
+ # Collect all entries for the series
143
+ series_entries = [entry for entry in self.custom_json if entry.get('series') == series]
144
+ # Separate entries with and without year
145
+ with_year = []
146
+ without_year = []
147
+ for entry in series_entries:
148
+ year = self._extract_year_from_text(entry.get('title', ''))
149
+ if not year:
150
+ year = self._extract_year_from_text(entry.get('episodes', ''))
151
+ if year:
152
+ with_year.append((year, entry))
153
+ else:
154
+ without_year.append(entry)
155
+ # Sort entries with year
156
+ with_year.sort(key=lambda x: x[0])
157
+ # Assign new numbers
158
+ new_no = 1
159
+ for _, entry in with_year:
160
+ entry['no'] = str(new_no)
161
+ new_no += 1
162
+ for entry in without_year:
163
+ entry['no'] = str(new_no)
164
+ new_no += 1
165
+
166
+ def add_entry_from_taf(self, taf_file: str, input_files: List[str], artwork_url: Optional[str] = None) -> bool:
167
+ """
168
+ Add an entry to the custom JSON from a TAF file.
169
+ If an entry with the same hash exists, it will be updated.
170
+ If an entry with the same series+episodes exists, the new hash will be added to it.
171
+
172
+ Args:
173
+ taf_file (str): Path to the TAF file
174
+ input_files (list[str]): List of input audio files used to create the TAF
175
+ artwork_url (str | None): URL of the uploaded artwork (if any)
176
+
177
+ Returns:
178
+ bool: True if successful, False otherwise
179
+ """
180
+ logger.trace("Entering add_entry_from_taf() with taf_file=%s, input_files=%s, artwork_url=%s",
181
+ taf_file, input_files, artwork_url)
182
+
183
+ if not self.is_loaded:
184
+ logger.error("Cannot add entry: tonies.custom.json not loaded")
185
+ return False
186
+
187
+ try:
188
+ logger.info("Adding entry for %s to tonies.custom.json", taf_file)
189
+ logger.debug("Extracting metadata from input files")
190
+ metadata = self._extract_metadata_from_files(input_files)
191
+ logger.debug("Extracted metadata: %s", metadata)
192
+ with open(taf_file, 'rb') as f:
193
+ taf_hash = hashlib.sha1(f.read()).hexdigest().upper()
194
+
195
+ timestamp = str(int(time.time()))
196
+ series = metadata.get('albumartist', metadata.get('artist', 'Unknown Artist'))
197
+ episodes = metadata.get('album', os.path.splitext(os.path.basename(taf_file))[0])
198
+ copyright = metadata.get('copyright', '')
199
+
200
+ # Extract year from metadata or from episode title
201
+ year = None
202
+ year_str = metadata.get('year', metadata.get('date', None))
203
+
204
+ # Try to convert metadata year to int if it exists
205
+ if year_str:
206
+ try:
207
+ # Extract 4 digits if the date includes more information (e.g., "2022-05-01")
208
+ import re
209
+ year_match = re.search(r'(\d{4})', str(year_str))
210
+ if year_match:
211
+ year = int(year_match.group(1))
212
+ else:
213
+ # If year is just a number, try to format it properly
214
+ year_val = int(year_str)
215
+ if 0 <= year_val <= 99: # Assume 2-digit year format
216
+ if year_val <= 25: # Arbitrary cutoff for 20xx vs 19xx
217
+ year = 2000 + year_val
218
+ else:
219
+ year = 1900 + year_val
220
+ else:
221
+ year = year_val
222
+ except (ValueError, TypeError):
223
+ logger.debug("Could not convert metadata year '%s' to integer", year_str)
224
+
225
+ if not year:
226
+ year_from_episodes = self._extract_year_from_text(episodes)
227
+ year_from_copyright = self._extract_year_from_text(copyright)
228
+ if year_from_episodes:
229
+ year = year_from_episodes
230
+ else:
231
+ year = year_from_copyright
232
+
233
+ # Ensure year is in YYYY format
234
+ year_formatted = None
235
+ if year:
236
+ # Validate the year is in the reasonable range
237
+ if 1900 <= year <= 2099:
238
+ year_formatted = f"{year:04d}" # Format as 4 digits
239
+ logger.debug("Formatted year '%s' as '%s'", year, year_formatted)
240
+ else:
241
+ logger.warning("Year '%s' outside reasonable range (1900-2099), ignoring", year)
242
+
243
+ if year_formatted:
244
+ title = f"{series} - {year_formatted} - {episodes}"
245
+ else:
246
+ title = f"{series} - {episodes}"
247
+
248
+ tracks = metadata.get('track_descriptions', [])
249
+ language = self._determine_language(metadata)
250
+ category = self._determine_category_v1(metadata)
251
+
252
+ existing_entry, entry_idx = self.find_entry_by_hash(taf_hash)
253
+ if existing_entry:
254
+ logger.info("Found existing entry with the same hash, updating it")
255
+ if artwork_url and artwork_url != existing_entry.get('pic', ''):
256
+ logger.debug("Updating artwork URL")
257
+ existing_entry['pic'] = artwork_url
258
+ if tracks and tracks != existing_entry.get('tracks', []):
259
+ logger.debug("Updating track descriptions")
260
+ existing_entry['tracks'] = tracks
261
+ if episodes and episodes != existing_entry.get('episodes', ''):
262
+ logger.debug("Updating episodes")
263
+ existing_entry['episodes'] = episodes
264
+ if series and series != existing_entry.get('series', ''):
265
+ logger.debug("Updating series")
266
+ existing_entry['series'] = series
267
+ logger.info("Successfully updated existing entry for %s", taf_file)
268
+ self.renumber_series_entries(series)
269
+ return True
270
+
271
+ existing_entry, entry_idx = self.find_entry_by_series_episodes(series, episodes)
272
+ if existing_entry:
273
+ logger.info("Found existing entry with the same series/episodes, adding hash to it")
274
+ if 'audio_id' not in existing_entry:
275
+ existing_entry['audio_id'] = []
276
+ if 'hash' not in existing_entry:
277
+ existing_entry['hash'] = []
278
+
279
+ existing_entry['audio_id'].append(timestamp)
280
+ existing_entry['hash'].append(taf_hash)
281
+
282
+ if artwork_url and artwork_url != existing_entry.get('pic', ''):
283
+ logger.debug("Updating artwork URL")
284
+ existing_entry['pic'] = artwork_url
285
+
286
+ logger.info("Successfully added new hash to existing entry for %s", taf_file)
287
+ self.renumber_series_entries(series)
288
+ return True
289
+
290
+ logger.debug("No existing entry found, creating new entry")
291
+
292
+ logger.debug("Generating entry number")
293
+ entry_no = self._generate_entry_no(series, episodes, year)
294
+ logger.debug("Generated entry number: %s", entry_no)
295
+
296
+ logger.debug("Generating model number")
297
+ model_number = self._generate_model_number()
298
+ logger.debug("Generated model number: %s", model_number)
299
+
300
+ entry = {
301
+ "no": entry_no,
302
+ "model": model_number,
303
+ "audio_id": [timestamp],
304
+ "hash": [taf_hash],
305
+ "title": title,
306
+ "series": series,
307
+ "episodes": episodes,
308
+ "tracks": tracks,
309
+ "release": timestamp,
310
+ "language": language,
311
+ "category": category,
312
+ "pic": artwork_url if artwork_url else ""
313
+ }
314
+
315
+ self.custom_json.append(entry)
316
+ logger.debug("Added entry to custom_json (new length: %d)", len(self.custom_json))
317
+
318
+ logger.info("Successfully added entry for %s", taf_file)
319
+ self.renumber_series_entries(series)
320
+ logger.trace("Exiting add_entry_from_taf() with success=True")
321
+ return True
322
+
323
+ except Exception as e:
324
+ logger.error("Error adding entry for %s: %s", taf_file, e)
325
+ logger.trace("Exiting add_entry_from_taf() with success=False due to exception: %s", str(e))
326
+ return False
327
+
328
+ def _generate_entry_no(self, series: str, episodes: str, year: Optional[int] = None) -> str:
329
+ """
330
+ Generate an entry number based on specific rules:
331
+ 1. For series entries with years: assign numbers in chronological order (1, 2, 3, etc.)
332
+ 2. For entries without years: assign the next available number after those with years
333
+
334
+ Args:
335
+ series (str): Series name
336
+ episodes (str): Episodes name
337
+ year (int | None): Release year from metadata, if available
338
+
339
+ Returns:
340
+ str: Generated entry number as string
341
+ """
342
+ logger.trace("Entering _generate_entry_no() with series='%s', episodes='%s', year=%s",
343
+ series, episodes, year)
344
+
345
+ # If we don't have a series name, use a simple approach to get the next number
346
+ if not series:
347
+ max_no = 0
348
+ for entry in self.custom_json:
349
+ try:
350
+ no_value = int(entry.get('no', '0'))
351
+ max_no = max(max_no, no_value)
352
+ except (ValueError, TypeError):
353
+ pass
354
+ return str(max_no + 1)
355
+
356
+ logger.debug("Generating entry number for series '%s'", series)
357
+
358
+ # Step 1: Collect all existing entries for this series and extract their years
359
+ series_entries = []
360
+ used_numbers = set()
361
+
362
+ for entry in self.custom_json:
363
+ entry_series = entry.get('series', '')
364
+ if entry_series == series:
365
+ entry_no = entry.get('no', '')
366
+ try:
367
+ entry_no_int = int(entry_no)
368
+ used_numbers.add(entry_no_int)
369
+ except (ValueError, TypeError):
370
+ pass
371
+
372
+ entry_title = entry.get('title', '')
373
+ entry_episodes = entry.get('episodes', '')
374
+
375
+ # Extract year from title and episodes
376
+ entry_year = self._extract_year_from_text(entry_title)
377
+ if not entry_year:
378
+ entry_year = self._extract_year_from_text(entry_episodes)
379
+
380
+ series_entries.append({
381
+ 'no': entry_no,
382
+ 'title': entry_title,
383
+ 'episodes': entry_episodes,
384
+ 'year': entry_year
385
+ })
386
+
387
+ # Try to extract year from episodes if not explicitly provided
388
+ if not year:
389
+ extracted_year = self._extract_year_from_text(episodes)
390
+ if extracted_year:
391
+ year = extracted_year
392
+ logger.debug("Extracted year %d from episodes '%s'", year, episodes)
393
+
394
+ # Step 2: Split entries into those with years and those without
395
+ entries_with_years = [e for e in series_entries if e['year'] is not None]
396
+ entries_without_years = [e for e in series_entries if e['year'] is None]
397
+
398
+ # Sort entries with years by year (oldest first)
399
+ entries_with_years.sort(key=lambda x: x['year'])
400
+
401
+ logger.debug("Found %d entries with years and %d entries without years",
402
+ len(entries_with_years), len(entries_without_years))
403
+
404
+ # Step 3: If this entry has a year, determine where it should be inserted
405
+ if year:
406
+ # Find position based on chronological order
407
+ insertion_index = 0
408
+ while insertion_index < len(entries_with_years) and entries_with_years[insertion_index]['year'] < year:
409
+ insertion_index += 1
410
+
411
+ # Resulting position is 1-indexed
412
+ position = insertion_index + 1
413
+ logger.debug("For year %d, calculated position %d based on chronological order", year, position)
414
+
415
+ # Now adjust position if needed to avoid conflicts with existing entries
416
+ while position in used_numbers:
417
+ position += 1
418
+ logger.debug("Position %d already used, incrementing to %d", position-1, position)
419
+
420
+ logger.debug("Final assigned entry number: %d", position)
421
+ return str(position)
422
+ else:
423
+ # Step 4: If this entry has no year, it should come after all entries with years
424
+ # Find the highest number used by entries with years
425
+ years_highest_no = 0
426
+ if entries_with_years:
427
+ for i, entry in enumerate(entries_with_years):
428
+ try:
429
+ expected_no = i + 1 # 1-indexed
430
+ actual_no = int(entry['no'])
431
+ years_highest_no = max(years_highest_no, actual_no)
432
+ except (ValueError, TypeError):
433
+ pass
434
+
435
+ # Find the highest number used overall
436
+ highest_no = max(used_numbers) if used_numbers else 0
437
+
438
+ # Next number should be at least one more than the highest from entries with years
439
+ next_no = max(years_highest_no, highest_no) + 1
440
+
441
+ logger.debug("No year available, assigned next number: %d", next_no)
442
+ return str(next_no)
443
+
444
+ def _extract_year_from_text(self, text: str) -> Optional[int]:
445
+ """
446
+ Extract a year (1900-2099) from text.
447
+
448
+ Args:
449
+ text (str): The text to extract the year from
450
+
451
+ Returns:
452
+ int | None: The extracted year as int, or None if no valid year found
453
+ """
454
+ import re
455
+ year_pattern = re.compile(r'(19\d{2}|20\d{2})')
456
+ year_match = year_pattern.search(text)
457
+
458
+ if year_match:
459
+ try:
460
+ extracted_year = int(year_match.group(1))
461
+ if 1900 <= extracted_year <= 2099:
462
+ return extracted_year
463
+ except (ValueError, TypeError):
464
+ pass
465
+
466
+ return None
467
+
468
+ def _format_number(self, number: int, existing_entries: List[Dict[str, Any]]) -> str:
469
+ """
470
+ Format a number to match the existing entry number format (e.g., with leading zeros).
471
+
472
+ Args:
473
+ number (int): The number to format
474
+ existing_entries (list[dict]): List of existing entries with their numbers
475
+
476
+ Returns:
477
+ str: Formatted number as string
478
+ """
479
+ max_digits = 1
480
+ for entry in existing_entries:
481
+ entry_no = entry.get('no', '')
482
+ if entry_no and isinstance(entry_no, str) and entry_no.isdigit():
483
+ leading_zeros = len(entry_no) - len(entry_no.lstrip('0'))
484
+ if leading_zeros > 0:
485
+ digits = len(entry_no)
486
+ max_digits = max(max_digits, digits)
487
+ if max_digits > 1:
488
+ logger.trace("Formatting with %d digits", max_digits)
489
+ return f"{number:0{max_digits}d}"
490
+
491
+ return str(number)
492
+
493
+ def _generate_model_number(self) -> str:
494
+ """
495
+ Generate a unique model number for a new entry.
496
+
497
+ Returns:
498
+ str: Unique model number in the format "model-" followed by sequential number with zero padding
499
+ """
500
+ logger.trace("Entering _generate_model_number()")
501
+ highest_num = -1
502
+ pattern = re.compile(r'tt-42(\d+)')
503
+
504
+ logger.debug("Searching for highest tt-42 ID in %d existing entries", len(self.custom_json))
505
+ for entry in self.custom_json:
506
+ model = entry.get('model', '')
507
+ logger.trace("Checking model ID: %s", model)
508
+ match = pattern.match(model)
509
+ if match:
510
+ try:
511
+ num = int(match.group(1))
512
+ logger.trace("Found numeric part: %d", num)
513
+ highest_num = max(highest_num, num)
514
+ except (IndexError, ValueError) as e:
515
+ logger.trace("Failed to parse model ID: %s (%s)", model, str(e))
516
+ pass
517
+
518
+ logger.debug("Highest tt-42 ID number found: %d", highest_num)
519
+ next_num = highest_num + 1
520
+ result = f"tt-42{next_num:010d}"
521
+ logger.debug("Generated new model ID: %s", result)
522
+
523
+ logger.trace("Exiting _generate_model_number() with result=%s", result)
524
+ return result
525
+
526
+ def _determine_category_v1(self, metadata: Dict[str, Any]) -> str:
527
+ """
528
+ Determine the category in v1 format.
529
+
530
+ Args:
531
+ metadata (dict): Dictionary containing file metadata
532
+
533
+ Returns:
534
+ str: Category string in v1 format
535
+ """
536
+ if 'genre' in metadata:
537
+ genre_value = metadata['genre'].lower().strip()
538
+
539
+ if any(keyword in genre_value for keyword in ['musik', 'song', 'music', 'lied']):
540
+ return "music"
541
+ elif any(keyword in genre_value for keyword in ['hörspiel', 'audio play', 'hörbuch', 'audiobook']):
542
+ return "audio-play"
543
+ elif any(keyword in genre_value for keyword in ['märchen', 'fairy', 'tales']):
544
+ return "fairy-tale"
545
+ elif any(keyword in genre_value for keyword in ['wissen', 'knowledge', 'learn']):
546
+ return "knowledge"
547
+ elif any(keyword in genre_value for keyword in ['schlaf', 'sleep', 'meditation']):
548
+ return "sleep"
549
+
550
+ return "audio-play"
551
+
552
+ def find_entry_by_hash(self, taf_hash: str) -> tuple[Optional[Dict[str, Any]], Optional[int]]:
553
+ """
554
+ Find an entry in the custom JSON by TAF hash.
555
+
556
+ Args:
557
+ taf_hash (str): SHA1 hash of the TAF file to find
558
+
559
+ Returns:
560
+ tuple[dict | None, int | None]: Tuple of (entry, entry_index) if found, or (None, None) if not found
561
+ """
562
+ logger.trace("Searching for entry with hash %s", taf_hash)
563
+
564
+ for entry_idx, entry in enumerate(self.custom_json):
565
+ if 'hash' not in entry:
566
+ continue
567
+
568
+ for hash_value in entry['hash']:
569
+ if hash_value == taf_hash:
570
+ logger.debug("Found existing entry with matching hash %s", taf_hash)
571
+ return entry, entry_idx
572
+
573
+ logger.debug("No entry found with hash %s", taf_hash)
574
+ return None, None
575
+
576
+ def find_entry_by_series_episodes(self, series: str, episodes: str) -> tuple[Optional[Dict[str, Any]], Optional[int]]:
577
+ """
578
+ Find an entry in the custom JSON by series and episodes.
579
+
580
+ Args:
581
+ series (str): Series name to find
582
+ episodes (str): Episodes name to find
583
+
584
+ Returns:
585
+ tuple[dict | None, int | None]: Tuple of (entry, entry_index) if found, or (None, None) if not found
586
+ """
587
+ logger.trace("Searching for entry with series='%s', episodes='%s'", series, episodes)
588
+
589
+ for entry_idx, entry in enumerate(self.custom_json):
590
+ if entry.get('series') == series and entry.get('episodes') == episodes:
591
+ logger.debug("Found existing entry with matching series/episodes: %s / %s", series, episodes)
592
+ return entry, entry_idx
593
+
594
+ logger.debug("No entry found with series/episodes: %s / %s", series, episodes)
595
+ return None, None
596
+
597
+ def _extract_metadata_from_files(self, input_files: List[str]) -> Dict[str, Any]:
598
+ """
599
+ Extract metadata from audio files to use in the custom JSON entry.
600
+
601
+ Args:
602
+ input_files (list[str]): List of paths to audio files
603
+
604
+ Returns:
605
+ dict: Dictionary containing metadata extracted from files
606
+ """
607
+ metadata = {}
608
+ track_descriptions = []
609
+ for file_path in input_files:
610
+ tags = get_file_tags(file_path)
611
+ if 'title' in tags:
612
+ track_descriptions.append(tags['title'])
613
+ else:
614
+ filename = os.path.splitext(os.path.basename(file_path))[0]
615
+ track_descriptions.append(filename)
616
+ for tag_name, tag_value in tags.items():
617
+ if tag_name not in metadata:
618
+ metadata[tag_name] = tag_value
619
+
620
+ metadata['track_descriptions'] = track_descriptions
621
+
622
+ return metadata
623
+
624
+ def _determine_language(self, metadata: Dict[str, Any]) -> str:
625
+ if 'language' in metadata:
626
+ lang_value = metadata['language'].lower().strip()
627
+ if lang_value in LANGUAGE_MAPPING:
628
+ return LANGUAGE_MAPPING[lang_value]
629
+ try:
630
+ system_lang, _ = locale.getdefaultlocale()
631
+ if system_lang:
632
+ lang_code = system_lang.split('_')[0].lower()
633
+ if lang_code in LANGUAGE_MAPPING:
634
+ return LANGUAGE_MAPPING[lang_code]
635
+ except Exception:
636
+ pass
637
+ return 'de-de'
638
+
639
+ def _convert_v2_to_v1(self, v2_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
640
+ """
641
+ Convert data from v2 format to v1 format.
642
+
643
+ Args:
644
+ v2_data (list[dict]): Data in v2 format
645
+
646
+ Returns:
647
+ list[dict]: Converted data in v1 format
648
+ """
649
+ v1_data = []
650
+
651
+ entry_no = 0
652
+ for v2_entry in v2_data:
653
+ if 'data' not in v2_entry:
654
+ continue
655
+
656
+ for v2_data_item in v2_entry['data']:
657
+ series = v2_data_item.get('series', '')
658
+ episodes = v2_data_item.get('episode', '')
659
+ model = v2_data_item.get('article', '')
660
+ title = f"{series} - {episodes}" if series and episodes else episodes
661
+
662
+ v1_entry = {
663
+ "no": str(entry_no),
664
+ "model": model,
665
+ "audio_id": [],
666
+ "hash": [],
667
+ "title": title,
668
+ "series": series,
669
+ "episodes": episodes,
670
+ "tracks": v2_data_item.get('track-desc', []),
671
+ "release": str(v2_data_item.get('release', int(time.time()))),
672
+ "language": v2_data_item.get('language', 'de-de'),
673
+ "category": self._convert_category_v2_to_v1(v2_data_item.get('category', '')),
674
+ "pic": v2_data_item.get('image', '')
675
+ }
676
+ if 'ids' in v2_data_item:
677
+ for id_entry in v2_data_item['ids']:
678
+ if 'audio-id' in id_entry:
679
+ v1_entry['audio_id'].append(str(id_entry['audio-id']))
680
+ if 'hash' in id_entry:
681
+ v1_entry['hash'].append(id_entry['hash'].upper())
682
+
683
+ v1_data.append(v1_entry)
684
+ entry_no += 1
685
+
686
+ return v1_data
687
+
688
+ def _convert_category_v2_to_v1(self, v2_category: str) -> str:
689
+ """
690
+ Convert category from v2 format to v1 format.
691
+
692
+ Args:
693
+ v2_category (str): Category in v2 format
694
+
695
+ Returns:
696
+ str: Category in v1 format
697
+ """
698
+ v2_to_v1_mapping = {
699
+ "music": "music",
700
+ "Hörspiele & Hörbücher": "audio-play",
701
+ "Schlaflieder & Entspannung": "sleep",
702
+ "Wissen & Hörmagazine": "knowledge",
703
+ "Märchen": "fairy-tale"
704
+ }
705
+
706
+ return v2_to_v1_mapping.get(v2_category, "audio-play")
707
+
708
+ class ToniesJsonHandlerv2:
25
709
  """Handler for tonies.custom.json operations."""
26
710
 
27
711
  def __init__(self, client: TeddyCloudClient = None):
@@ -29,7 +713,7 @@ class ToniesJsonHandler:
29
713
  Initialize the handler.
30
714
 
31
715
  Args:
32
- client: TeddyCloudClient instance to use for API communication
716
+ client (TeddyCloudClient | None): TeddyCloudClient instance to use for API communication
33
717
  """
34
718
  self.client = client
35
719
  self.custom_json = []
@@ -40,7 +724,7 @@ class ToniesJsonHandler:
40
724
  Load tonies.custom.json from the TeddyCloud server.
41
725
 
42
726
  Returns:
43
- True if successful, False otherwise
727
+ bool: True if successful, False otherwise
44
728
  """
45
729
  if self.client is None:
46
730
  logger.error("Cannot load from server: no client provided")
@@ -66,10 +750,10 @@ class ToniesJsonHandler:
66
750
  Load tonies.custom.json from a local file.
67
751
 
68
752
  Args:
69
- file_path: Path to the tonies.custom.json file
753
+ file_path (str): Path to the tonies.custom.json file
70
754
 
71
755
  Returns:
72
- True if successful, False otherwise
756
+ bool: True if successful, False otherwise
73
757
  """
74
758
  try:
75
759
  if os.path.exists(file_path):
@@ -99,10 +783,10 @@ class ToniesJsonHandler:
99
783
  Save tonies.custom.json to a local file.
100
784
 
101
785
  Args:
102
- file_path: Path where to save the tonies.custom.json file
786
+ file_path (str): Path where to save the tonies.custom.json file
103
787
 
104
788
  Returns:
105
- True if successful, False otherwise
789
+ bool: True if successful, False otherwise
106
790
  """
107
791
  if not self.is_loaded:
108
792
  logger.error("Cannot save tonies.custom.json: data not loaded")
@@ -128,12 +812,12 @@ class ToniesJsonHandler:
128
812
  If an entry with the same series+episode exists, the new hash will be added to it.
129
813
 
130
814
  Args:
131
- taf_file: Path to the TAF file
132
- input_files: List of input audio files used to create the TAF
133
- artwork_url: URL of the uploaded artwork (if any)
815
+ taf_file (str): Path to the TAF file
816
+ input_files (list[str]): List of input audio files used to create the TAF
817
+ artwork_url (str | None): URL of the uploaded artwork (if any)
134
818
 
135
819
  Returns:
136
- True if successful, False otherwise
820
+ bool: True if successful, False otherwise
137
821
  """
138
822
  logger.trace("Entering add_entry_from_taf() with taf_file=%s, input_files=%s, artwork_url=%s",
139
823
  taf_file, input_files, artwork_url)
@@ -234,7 +918,7 @@ class ToniesJsonHandler:
234
918
  Generate a unique article ID for a new entry.
235
919
 
236
920
  Returns:
237
- Unique article ID in the format "tt-42" followed by sequential number starting from 0
921
+ str: Unique article ID in the format "tt-42" followed by sequential number starting from 0
238
922
  """
239
923
  logger.trace("Entering _generate_article_id()")
240
924
  highest_num = -1
@@ -267,26 +951,26 @@ class ToniesJsonHandler:
267
951
  Extract metadata from audio files to use in the custom JSON entry.
268
952
 
269
953
  Args:
270
- input_files: List of paths to audio files
954
+ input_files (list[str]): List of paths to audio files
271
955
 
272
956
  Returns:
273
- Dictionary containing metadata extracted from files
957
+ dict: Dictionary containing metadata extracted from files
274
958
  """
275
959
  metadata = {}
276
960
  track_descriptions = []
277
961
  for file_path in input_files:
278
962
  tags = get_file_tags(file_path)
963
+ # Extract track descriptions
279
964
  if 'title' in tags:
280
965
  track_descriptions.append(tags['title'])
281
966
  else:
282
967
  filename = os.path.splitext(os.path.basename(file_path))[0]
283
968
  track_descriptions.append(filename)
284
-
285
- if 'language' not in metadata and 'language' in tags:
286
- metadata['language'] = tags['language']
287
969
 
288
- if 'genre' not in metadata and 'genre' in tags:
289
- metadata['genre'] = tags['genre']
970
+ # Copy all available tags, but don't overwrite existing ones
971
+ for tag_name, tag_value in tags.items():
972
+ if tag_name not in metadata:
973
+ metadata[tag_name] = tag_value
290
974
 
291
975
  metadata['track_descriptions'] = track_descriptions
292
976
 
@@ -362,10 +1046,10 @@ class ToniesJsonHandler:
362
1046
  Find an entry in the custom JSON by TAF hash.
363
1047
 
364
1048
  Args:
365
- taf_hash: SHA1 hash of the TAF file to find
1049
+ taf_hash (str): SHA1 hash of the TAF file to find
366
1050
 
367
1051
  Returns:
368
- Tuple of (entry, entry_index, data_index) if found, or (None, None, None) if not found
1052
+ tuple[dict | None, int | None, int | None]: Tuple of (entry, entry_index, data_index) if found, or (None, None, None) if not found
369
1053
  """
370
1054
  logger.trace("Searching for entry with hash %s", taf_hash)
371
1055
 
@@ -390,11 +1074,11 @@ class ToniesJsonHandler:
390
1074
  Find an entry in the custom JSON by series and episode.
391
1075
 
392
1076
  Args:
393
- series: Series name to find
394
- episode: Episode name to find
1077
+ series (str): Series name to find
1078
+ episode (str): Episode name to find
395
1079
 
396
1080
  Returns:
397
- Tuple of (entry, entry_index, data_index) if found, or (None, None, None) if not found
1081
+ tuple[dict | None, int | None, int | None]: Tuple of (entry, entry_index, data_index) if found, or (None, None, None) if not found
398
1082
  """
399
1083
  logger.trace("Searching for entry with series='%s', episode='%s'", series, episode)
400
1084
 
@@ -415,10 +1099,10 @@ class ToniesJsonHandler:
415
1099
  Calculate the total runtime in minutes from a list of audio files.
416
1100
 
417
1101
  Args:
418
- input_files: List of paths to audio files
1102
+ input_files (list[str]): List of paths to audio files
419
1103
 
420
1104
  Returns:
421
- Total runtime in minutes (rounded to the nearest minute)
1105
+ int: Total runtime in minutes (rounded to the nearest minute)
422
1106
  """
423
1107
  logger.trace("Entering _calculate_runtime() with %d input files", len(input_files))
424
1108
  total_runtime_seconds = 0
@@ -463,7 +1147,6 @@ class ToniesJsonHandler:
463
1147
  logger.warning("Error processing file %s: %s", file_path, e)
464
1148
  logger.trace("Exception details for %s: %s", file_path, str(e), exc_info=True)
465
1149
 
466
- # Convert seconds to minutes, rounding to nearest minute
467
1150
  total_runtime_minutes = round(total_runtime_seconds / 60)
468
1151
 
469
1152
  logger.info("Calculated total runtime: %d seconds (%d minutes) from %d/%d files",
@@ -480,25 +1163,134 @@ class ToniesJsonHandler:
480
1163
  logger.trace("Exiting _calculate_runtime() with total runtime=%d minutes", total_runtime_minutes)
481
1164
  return total_runtime_minutes
482
1165
 
483
- def fetch_and_update_tonies_json(client: TeddyCloudClient, taf_file: Optional[str] = None, input_files: Optional[List[str]] = None,
1166
+ def fetch_and_update_tonies_json_v1(client: TeddyCloudClient, taf_file: Optional[str] = None, input_files: Optional[List[str]] = None,
1167
+ artwork_url: Optional[str] = None, output_dir: Optional[str] = None) -> bool:
1168
+ """
1169
+ Fetch tonies.custom.json from server and merge with local file if it exists, then update with new entry in v1 format.
1170
+
1171
+ Args:
1172
+ client (TeddyCloudClient): TeddyCloudClient instance to use for API communication
1173
+ taf_file (str | None): Path to the TAF file to add
1174
+ input_files (list[str] | None): List of input audio files used to create the TAF
1175
+ artwork_url (str | None): URL of the uploaded artwork (if any)
1176
+ output_dir (str | None): Directory where to save the tonies.custom.json file (defaults to './output')
1177
+
1178
+ Returns:
1179
+ bool: True if successful, False otherwise
1180
+ """
1181
+ logger.trace("Entering fetch_and_update_tonies_json_v1 with client=%s, taf_file=%s, input_files=%s, artwork_url=%s, output_dir=%s",
1182
+ client, taf_file, input_files, artwork_url, output_dir)
1183
+
1184
+ handler = ToniesJsonHandlerv1(client)
1185
+ if not output_dir:
1186
+ output_dir = './output'
1187
+ logger.debug("No output directory specified, using default: %s", output_dir)
1188
+
1189
+ os.makedirs(output_dir, exist_ok=True)
1190
+ logger.debug("Ensuring output directory exists: %s", output_dir)
1191
+
1192
+ json_file_path = os.path.join(output_dir, 'tonies.custom.json')
1193
+ logger.debug("JSON file path: %s", json_file_path)
1194
+
1195
+ loaded_from_server = False
1196
+ if client:
1197
+ logger.info("Attempting to load tonies.custom.json from server")
1198
+ loaded_from_server = handler.load_from_server()
1199
+ logger.debug("Load from server result: %s", "success" if loaded_from_server else "failed")
1200
+ else:
1201
+ logger.debug("No client provided, skipping server load")
1202
+
1203
+ if os.path.exists(json_file_path):
1204
+ logger.info("Local tonies.custom.json file found, merging with server content")
1205
+ logger.debug("Local file exists at %s, size: %d bytes", json_file_path, os.path.getsize(json_file_path))
1206
+
1207
+ local_handler = ToniesJsonHandlerv1()
1208
+ if local_handler.load_from_file(json_file_path):
1209
+ logger.debug("Successfully loaded local file with %d entries", len(local_handler.custom_json))
1210
+
1211
+ if loaded_from_server:
1212
+ logger.debug("Merging local entries with server entries")
1213
+ server_hashes = set()
1214
+ for entry in handler.custom_json:
1215
+ if 'hash' in entry:
1216
+ for hash_value in entry['hash']:
1217
+ server_hashes.add(hash_value)
1218
+
1219
+ logger.debug("Found %d unique hash values from server", len(server_hashes))
1220
+
1221
+ added_count = 0
1222
+ for local_entry in local_handler.custom_json:
1223
+ if 'hash' in local_entry:
1224
+ has_unique_hash = False
1225
+ for hash_value in local_entry['hash']:
1226
+ if hash_value not in server_hashes:
1227
+ has_unique_hash = True
1228
+ break
1229
+
1230
+ if has_unique_hash:
1231
+ logger.trace("Adding local-only entry to merged content")
1232
+ handler.custom_json.append(local_entry)
1233
+ added_count += 1
1234
+
1235
+ logger.debug("Added %d local-only entries to merged content", added_count)
1236
+ else:
1237
+ logger.debug("Using only local entries (server load failed or no client)")
1238
+ handler.custom_json = local_handler.custom_json
1239
+ handler.is_loaded = True
1240
+ logger.info("Using local tonies.custom.json content")
1241
+ elif not loaded_from_server:
1242
+ logger.debug("No local file found and server load failed, starting with empty list")
1243
+ handler.custom_json = []
1244
+ handler.is_loaded = True
1245
+ logger.info("No tonies.custom.json found, starting with empty list")
1246
+
1247
+ if taf_file and input_files and handler.is_loaded:
1248
+ logger.debug("Adding new entry for TAF file: %s", taf_file)
1249
+ logger.debug("Using %d input files for metadata extraction", len(input_files))
1250
+
1251
+ if not handler.add_entry_from_taf(taf_file, input_files, artwork_url):
1252
+ logger.error("Failed to add entry to tonies.custom.json")
1253
+ logger.trace("Exiting fetch_and_update_tonies_json_v1 with success=False (failed to add entry)")
1254
+ return False
1255
+
1256
+ logger.debug("Successfully added new entry for %s", taf_file)
1257
+ else:
1258
+ if not taf_file:
1259
+ logger.debug("No TAF file provided, skipping add entry step")
1260
+ elif not input_files:
1261
+ logger.debug("No input files provided, skipping add entry step")
1262
+ elif not handler.is_loaded:
1263
+ logger.debug("Handler not properly loaded, skipping add entry step")
1264
+
1265
+ logger.debug("Saving updated tonies.custom.json to %s", json_file_path)
1266
+ if not handler.save_to_file(json_file_path):
1267
+ logger.error("Failed to save tonies.custom.json to file")
1268
+ logger.trace("Exiting fetch_and_update_tonies_json_v1 with success=False (failed to save file)")
1269
+ return False
1270
+
1271
+ logger.debug("Successfully saved tonies.custom.json with %d entries", len(handler.custom_json))
1272
+ logger.trace("Exiting fetch_and_update_tonies_json_v1 with success=True")
1273
+ return True
1274
+
1275
+ def fetch_and_update_tonies_json_v2(client: TeddyCloudClient, taf_file: Optional[str] = None, input_files: Optional[List[str]] = None,
484
1276
  artwork_url: Optional[str] = None, output_dir: Optional[str] = None) -> bool:
485
1277
  """
486
1278
  Fetch tonies.custom.json from server and merge with local file if it exists, then update with new entry.
487
1279
 
488
1280
  Args:
489
- client: TeddyCloudClient instance to use for API communication
490
- taf_file: Path to the TAF file to add
491
- input_files: List of input audio files used to create the TAF
492
- artwork_url: URL of the uploaded artwork (if any)
493
- output_dir: Directory where to save the tonies.custom.json file (defaults to './output')
1281
+ client (TeddyCloudClient): TeddyCloudClient instance to use for API communication
1282
+ taf_file (str | None): Path to the TAF file to add
1283
+ input_files (list[str] | None): List of input audio files used to create the TAF
1284
+ artwork_url (str | None): URL of the uploaded artwork (if any)
1285
+ output_dir (str | None): Directory where to save the tonies.custom.json file (defaults to './output')
494
1286
 
495
1287
  Returns:
496
- True if successful, False otherwise
1288
+ bool: True if successful, False otherwise
497
1289
  """
498
1290
  logger.trace("Entering fetch_and_update_tonies_json with client=%s, taf_file=%s, input_files=%s, artwork_url=%s, output_dir=%s",
499
1291
  client, taf_file, input_files, artwork_url, output_dir)
500
1292
 
501
- handler = ToniesJsonHandler(client)
1293
+ handler = ToniesJsonHandlerv2(client)
502
1294
  if not output_dir:
503
1295
  output_dir = './output'
504
1296
  logger.debug("No output directory specified, using default: %s", output_dir)
@@ -521,7 +1313,7 @@ def fetch_and_update_tonies_json(client: TeddyCloudClient, taf_file: Optional[st
521
1313
  logger.info("Local tonies.custom.json file found, merging with server content")
522
1314
  logger.debug("Local file exists at %s, size: %d bytes", json_file_path, os.path.getsize(json_file_path))
523
1315
 
524
- local_handler = ToniesJsonHandler()
1316
+ local_handler = ToniesJsonHandlerv2()
525
1317
  if local_handler.load_from_file(json_file_path):
526
1318
  logger.debug("Successfully loaded local file with %d entries", len(local_handler.custom_json))
527
1319