| @@ 57-88 (lines=32) @@ | ||
| 54 | json.dump(selected_collections, f, ensure_ascii=False, indent=4) |
|
| 55 | except Exception as e: logging.error(f"Error saving {SELECTED_COLLECTIONS_FILE}: {e}") |
|
| 56 | ||
| 57 | def get_recently_pinned_collections(selected_collections, config): |
|
| 58 | """Gets titles of non-special collections pinned within the repeat_block_hours window.""" |
|
| 59 | # Note: This function now only considers titles saved in the history file, |
|
| 60 | # which (with the main loop change) will exclude special collections. |
|
| 61 | repeat_block_hours = config.get('repeat_block_hours', 12) |
|
| 62 | if not isinstance(repeat_block_hours, (int, float)) or repeat_block_hours <= 0: |
|
| 63 | logging.warning(f"Invalid 'repeat_block_hours', defaulting 12."); repeat_block_hours = 12 |
|
| 64 | cutoff_time = datetime.now() - timedelta(hours=repeat_block_hours) |
|
| 65 | recent_titles = set() |
|
| 66 | timestamps_to_keep = {} |
|
| 67 | logging.info(f"Checking history since {cutoff_time.strftime('%Y-%m-%d %H:%M:%S')} for recently pinned non-special items") |
|
| 68 | for timestamp_str, titles in list(selected_collections.items()): |
|
| 69 | if not isinstance(titles, list): logging.warning(f"Cleaning invalid history: {timestamp_str}"); selected_collections.pop(timestamp_str, None); continue |
|
| 70 | try: |
|
| 71 | try: timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S') |
|
| 72 | except ValueError: timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d') |
|
| 73 | if timestamp >= cutoff_time: |
|
| 74 | valid_titles = {t for t in titles if isinstance(t, str)}; recent_titles.update(valid_titles) |
|
| 75 | timestamps_to_keep[timestamp_str] = titles # Keep this entry in the temporary dict |
|
| 76 | except ValueError: logging.warning(f"Cleaning invalid date format: '{timestamp_str}'."); selected_collections.pop(timestamp_str, None) |
|
| 77 | except Exception as e: logging.error(f"Cleaning problematic history '{timestamp_str}': {e}."); selected_collections.pop(timestamp_str, None) |
|
| 78 | ||
| 79 | # Update the main selected_collections dict to only contain recent entries |
|
| 80 | keys_to_remove = set(selected_collections.keys()) - set(timestamps_to_keep.keys()) |
|
| 81 | if keys_to_remove: |
|
| 82 | logging.info(f"Removing {len(keys_to_remove)} old entries from history file.") |
|
| 83 | for key in keys_to_remove: selected_collections.pop(key, None) |
|
| 84 | save_selected_collections(selected_collections) # Save cleaned history immediately |
|
| 85 | ||
| 86 | if recent_titles: |
|
| 87 | logging.info(f"Recently pinned non-special collections (excluded): {', '.join(sorted(list(recent_titles)))}") |
|
| 88 | return recent_titles # This set now only contains non-special recently pinned items |
|
| 89 | ||
| 90 | def is_regex_excluded(title, patterns): |
|
| 91 | """Checks if a title matches any regex pattern.""" |
|
| @@ 57-88 (lines=32) @@ | ||
| 54 | json.dump(selected_collections, f, ensure_ascii=False, indent=4) |
|
| 55 | except Exception as e: logging.error(f"Error saving {SELECTED_COLLECTIONS_FILE}: {e}") |
|
| 56 | ||
| 57 | def get_recently_pinned_collections(selected_collections, config): |
|
| 58 | """Gets titles of non-special collections pinned within the repeat_block_hours window.""" |
|
| 59 | # Note: This function now only considers titles saved in the history file, |
|
| 60 | # which (with the main loop change) will exclude special collections. |
|
| 61 | repeat_block_hours = config.get('repeat_block_hours', 12) |
|
| 62 | if not isinstance(repeat_block_hours, (int, float)) or repeat_block_hours <= 0: |
|
| 63 | logging.warning(f"Invalid 'repeat_block_hours', defaulting 12."); repeat_block_hours = 12 |
|
| 64 | cutoff_time = datetime.now() - timedelta(hours=repeat_block_hours) |
|
| 65 | recent_titles = set() |
|
| 66 | timestamps_to_keep = {} |
|
| 67 | logging.info(f"Checking history since {cutoff_time.strftime('%Y-%m-%d %H:%M:%S')} for recently pinned non-special items") |
|
| 68 | for timestamp_str, titles in list(selected_collections.items()): |
|
| 69 | if not isinstance(titles, list): logging.warning(f"Cleaning invalid history: {timestamp_str}"); selected_collections.pop(timestamp_str, None); continue |
|
| 70 | try: |
|
| 71 | try: timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d %H:%M:%S') |
|
| 72 | except ValueError: timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d') |
|
| 73 | if timestamp >= cutoff_time: |
|
| 74 | valid_titles = {t for t in titles if isinstance(t, str)}; recent_titles.update(valid_titles) |
|
| 75 | timestamps_to_keep[timestamp_str] = titles # Keep this entry in the temporary dict |
|
| 76 | except ValueError: logging.warning(f"Cleaning invalid date format: '{timestamp_str}'."); selected_collections.pop(timestamp_str, None) |
|
| 77 | except Exception as e: logging.error(f"Cleaning problematic history '{timestamp_str}': {e}."); selected_collections.pop(timestamp_str, None) |
|
| 78 | ||
| 79 | # Update the main selected_collections dict to only contain recent entries |
|
| 80 | keys_to_remove = set(selected_collections.keys()) - set(timestamps_to_keep.keys()) |
|
| 81 | if keys_to_remove: |
|
| 82 | logging.info(f"Removing {len(keys_to_remove)} old entries from history file.") |
|
| 83 | for key in keys_to_remove: selected_collections.pop(key, None) |
|
| 84 | save_selected_collections(selected_collections) # Save cleaned history immediately |
|
| 85 | ||
| 86 | if recent_titles: |
|
| 87 | logging.info(f"Recently pinned non-special collections (excluded): {', '.join(sorted(list(recent_titles)))}") |
|
| 88 | return recent_titles # This set now only contains non-special recently pinned items |
|
| 89 | ||
| 90 | def is_regex_excluded(title, patterns): |
|
| 91 | """Checks if a title matches any regex pattern.""" |
|