|
| 1 | +"""CollectionsMixin - Native NotebookLM collections management operations.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import time |
| 5 | + |
| 6 | +from .base import BaseClient |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class CollectionsMixin(BaseClient): |
| 12 | + """Mixin for native NotebookLM collections management operations.""" |
| 13 | + |
| 14 | + def _get_collections_header(self) -> list: |
| 15 | + """Returns the standard header for collections RPC operations.""" |
| 16 | + return [2, None, [1], [1, None, None, None, None, None, None, None, None, None, [1, 3]]] |
| 17 | + |
| 18 | + def list_collections(self) -> list[dict]: |
| 19 | + """Lists all collections by temporarily creating a hidden one and deleting it. |
| 20 | +
|
| 21 | + Returns: |
| 22 | + List of collections, where each collection is a dict with: |
| 23 | + - id: str |
| 24 | + - name: str |
| 25 | + - notebook_ids: list[str] |
| 26 | + - emoji: str |
| 27 | + """ |
| 28 | + header = self._get_collections_header() |
| 29 | + temp_name = f"_temp_list_query_{int(time.time())}" |
| 30 | + |
| 31 | + # 1. Create a temporary collection to force Google to return the list |
| 32 | + params_create = [header, None, None, None, None, [[temp_name], None, []], 3] |
| 33 | + |
| 34 | + result = self._call_rpc("agX4Bc", params_create) |
| 35 | + |
| 36 | + collections = [] |
| 37 | + temp_id = None |
| 38 | + |
| 39 | + if result and len(result) > 2 and isinstance(result[2], list): |
| 40 | + for col in result[2]: |
| 41 | + if not isinstance(col, list) or len(col) < 3: |
| 42 | + continue |
| 43 | + |
| 44 | + col_name = col[0] or "" |
| 45 | + col_notebooks = col[1] or [] |
| 46 | + col_id = col[2] or "" |
| 47 | + col_emoji = col[3] if len(col) > 3 else "" |
| 48 | + |
| 49 | + if col_name == temp_name: |
| 50 | + temp_id = col_id |
| 51 | + continue |
| 52 | + |
| 53 | + collections.append( |
| 54 | + { |
| 55 | + "id": col_id, |
| 56 | + "name": col_name, |
| 57 | + "notebook_ids": col_notebooks, |
| 58 | + "emoji": col_emoji, |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + # 2. Delete the temporary collection in the background |
| 63 | + if temp_id: |
| 64 | + try: |
| 65 | + self.delete_collection(temp_id) |
| 66 | + except Exception as e: |
| 67 | + logger.warning(f"Failed to cleanup temporary listing collection {temp_id}: {e}") |
| 68 | + |
| 69 | + return collections |
| 70 | + |
| 71 | + def create_collection(self, name: str, notebook_ids: list[str] = None) -> dict: |
| 72 | + """Creates a new collection with the given name and notebooks. |
| 73 | +
|
| 74 | + Args: |
| 75 | + name: Name of the collection |
| 76 | + notebook_ids: List of notebook UUIDs to include |
| 77 | +
|
| 78 | + Returns: |
| 79 | + The created collection dict. |
| 80 | + """ |
| 81 | + header = self._get_collections_header() |
| 82 | + notebooks = notebook_ids or [] |
| 83 | + |
| 84 | + params = [header, None, None, None, None, [[name], None, notebooks], 3] |
| 85 | + |
| 86 | + result = self._call_rpc("agX4Bc", params) |
| 87 | + |
| 88 | + # Find the newly created collection in the returned list |
| 89 | + if result and len(result) > 2 and isinstance(result[2], list): |
| 90 | + for col in result[2]: |
| 91 | + if isinstance(col, list) and len(col) >= 3 and col[0] == name: |
| 92 | + return { |
| 93 | + "id": col[2], |
| 94 | + "name": col[0], |
| 95 | + "notebook_ids": col[1] or [], |
| 96 | + "emoji": col[3] if len(col) > 3 else "", |
| 97 | + } |
| 98 | + |
| 99 | + raise RuntimeError("Failed to confirm collection creation on Google backend") |
| 100 | + |
| 101 | + def edit_collection( |
| 102 | + self, collection_id: str, name: str = None, notebook_ids: list[str] = None |
| 103 | + ) -> bool: |
| 104 | + """Edits an existing collection's name and/or notebooks. |
| 105 | +
|
| 106 | + Args: |
| 107 | + collection_id: UUID of the collection to edit |
| 108 | + name: New name (optional) |
| 109 | + notebook_ids: New complete list of notebook IDs (optional) |
| 110 | + """ |
| 111 | + header = self._get_collections_header() |
| 112 | + |
| 113 | + # Format: [[None, None, None, [[notebook_ids]]], [[new_name]]] |
| 114 | + notebooks_payload = None |
| 115 | + if notebook_ids is not None: |
| 116 | + notebooks_payload = [None, None, None, [notebook_ids]] |
| 117 | + |
| 118 | + name_payload = None |
| 119 | + if name is not None: |
| 120 | + name_payload = [[name]] |
| 121 | + |
| 122 | + edit_payload = [notebooks_payload, name_payload] |
| 123 | + |
| 124 | + params = [header, None, collection_id, edit_payload, 3] |
| 125 | + |
| 126 | + result = self._call_rpc("le8sX", params) |
| 127 | + return result == [] or result is not None |
| 128 | + |
| 129 | + def set_collection_emoji(self, collection_id: str, emoji: str) -> bool: |
| 130 | + """Sets or clears the emoji marker on a collection (pass "" to clear).""" |
| 131 | + header = self._get_collections_header() |
| 132 | + params = [header, None, collection_id, [[[None, emoji]]], 3] |
| 133 | + result = self._call_rpc("le8sX", params) |
| 134 | + return result == [] or result is not None |
| 135 | + |
| 136 | + def delete_collection(self, collection_id: str) -> bool: |
| 137 | + """Permanently deletes a collection. Notebooks are not deleted.""" |
| 138 | + header = self._get_collections_header() |
| 139 | + params = [header, None, [collection_id], 3] |
| 140 | + result = self._call_rpc("GyzE7e", params) |
| 141 | + return result == [] or result is not None |
0 commit comments