Skip to content

Commit 697f290

Browse files
authored
2 parents c9223c0 + 19f7285 commit 697f290

File tree

3 files changed

+64
-37
lines changed

3 files changed

+64
-37
lines changed

notion/client.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from requests import Session, HTTPError
77
from requests.cookies import cookiejar_from_dict
8-
from urllib.parse import urljoin
8+
from urllib.parse import unquote, urljoin
99
from requests.adapters import HTTPAdapter
1010
from requests.packages.urllib3.util.retry import Retry
1111
from getpass import getpass
@@ -53,6 +53,7 @@ def create_session(client_specified_retry=None):
5353
)
5454
adapter = HTTPAdapter(max_retries=retry)
5555
session.mount("https://", adapter)
56+
session.headers.update({"content-type": "application/json"})
5657
return session
5758

5859

@@ -130,6 +131,10 @@ def _update_user_info(self):
130131
self._store.store_recordmap(records)
131132
self.current_user = self.get_user(list(records["notion_user"].keys())[0])
132133
self.current_space = self.get_space(list(records["space"].keys())[0])
134+
135+
self.session.headers.update({"x-notion-active-user-header":
136+
json.loads(unquote(self.session.cookies.get("notion_users")))[1]})
137+
133138
return records
134139

135140
def get_email_uid(self):
@@ -140,7 +145,6 @@ def get_email_uid(self):
140145
}
141146

142147
def set_user_by_uid(self, user_id):
143-
self.session.headers.update({"x-notion-active-user-header": user_id})
144148
self._update_user_info()
145149

146150
def set_user_by_email(self, email):
@@ -158,15 +162,15 @@ def get_top_level_pages(self):
158162
records = self._update_user_info()
159163
return [self.get_block(bid) for bid in records["block"].keys()]
160164

161-
def get_record_data(self, table, id, force_refresh=False):
162-
return self._store.get(table, id, force_refresh=force_refresh)
165+
def get_record_data(self, table, id, force_refresh=False, limit=100):
166+
return self._store.get(table, id, force_refresh=force_refresh, limit=limit)
163167

164-
def get_block(self, url_or_id, force_refresh=False):
168+
def get_block(self, url_or_id, force_refresh=False, limit=100):
165169
"""
166170
Retrieve an instance of a subclass of Block that maps to the block/page identified by the URL or ID passed in.
167171
"""
168172
block_id = extract_id(url_or_id)
169-
block = self.get_record_data("block", block_id, force_refresh=force_refresh)
173+
block = self.get_record_data("block", block_id, force_refresh=force_refresh, limit=limit)
170174
if not block:
171175
return None
172176
if block.get("parent_table") == "collection":
@@ -306,11 +310,11 @@ def in_transaction(self):
306310
"""
307311
return hasattr(self, "_transaction_operations")
308312

309-
def search_pages_with_parent(self, parent_id, search=""):
313+
def search_pages_with_parent(self, parent_id, search="", limit=100):
310314
data = {
311315
"query": search,
312316
"parentId": parent_id,
313-
"limit": 10000,
317+
"limit": limit,
314318
"spaceId": self.current_space.id,
315319
}
316320
response = self.post("searchPagesWithParent", data).json()

notion/collection.py

+28-12
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ def __init__(
360360
sort=[],
361361
calendar_by="",
362362
group_by="",
363+
limit=100
363364
):
364365
assert not (
365366
aggregate and aggregations
@@ -374,25 +375,40 @@ def __init__(
374375
self.sort = _normalize_query_data(sort, collection)
375376
self.calendar_by = _normalize_property_name(calendar_by, collection)
376377
self.group_by = _normalize_property_name(group_by, collection)
378+
self.limit = limit
377379
self._client = collection._client
378380

379381
def execute(self):
380382

381383
result_class = QUERY_RESULT_TYPES.get(self.type, QueryResult)
382384

385+
kwargs = {
386+
'collection_id':self.collection.id,
387+
'collection_view_id':self.collection_view.id,
388+
'search':self.search,
389+
'type':self.type,
390+
'aggregate':self.aggregate,
391+
'aggregations':self.aggregations,
392+
'filter':self.filter,
393+
'sort':self.sort,
394+
'calendar_by':self.calendar_by,
395+
'group_by':self.group_by,
396+
'limit':0
397+
}
398+
399+
if self.limit == -1:
400+
# fetch remote total
401+
result = self._client.query_collection(
402+
**kwargs
403+
)
404+
self.limit = result.get("total",-1)
405+
406+
kwargs['limit'] = self.limit
407+
383408
return result_class(
384409
self.collection,
385410
self._client.query_collection(
386-
collection_id=self.collection.id,
387-
collection_view_id=self.collection_view.id,
388-
search=self.search,
389-
type=self.type,
390-
aggregate=self.aggregate,
391-
aggregations=self.aggregations,
392-
filter=self.filter,
393-
sort=self.sort,
394-
calendar_by=self.calendar_by,
395-
group_by=self.group_by,
411+
**kwargs
396412
),
397413
self,
398414
)
@@ -704,14 +720,15 @@ def __init__(self, collection, result, query):
704720
self.collection = collection
705721
self._client = collection._client
706722
self._block_ids = self._get_block_ids(result)
723+
self.total = result.get("total", -1)
707724
self.aggregates = result.get("aggregationResults", [])
708725
self.aggregate_ids = [
709726
agg.get("id") for agg in (query.aggregate or query.aggregations)
710727
]
711728
self.query = query
712729

713730
def _get_block_ids(self, result):
714-
return result["blockIds"]
731+
return result['reducerResults']['collection_group_results']["blockIds"]
715732

716733
def _get_block(self, id):
717734
block = CollectionRowBlock(self._client, id)
@@ -754,7 +771,6 @@ def __contains__(self, item):
754771
return False
755772
return item_id in self._block_ids
756773

757-
758774
class TableQueryResult(QueryResult):
759775

760776
_type = "table"

notion/store.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ def get_role(self, table, id, force_refresh=False):
174174
self.get(table, id, force_refresh=force_refresh)
175175
return self._role[table].get(id, None)
176176

177-
def get(self, table, id, force_refresh=False):
177+
def get(self, table, id, force_refresh=False, limit=100):
178178
id = extract_id(id)
179179
# look up the record in the current local dataset
180180
result = self._get(table, id)
181181
# if it's not found, try refreshing the record from the server
182182
if result is Missing or force_refresh:
183183
if table == "block":
184-
self.call_load_page_chunk(id)
184+
self.call_load_page_chunk(id,limit=limit)
185185
else:
186186
self.call_get_record_values(**{table: id})
187187
result = self._get(table, id)
@@ -269,15 +269,17 @@ def get_current_version(self, table, id):
269269
else:
270270
return -1
271271

272-
def call_load_page_chunk(self, page_id):
272+
def call_load_page_chunk(self, page_id, limit=100):
273273

274274
if self._client.in_transaction():
275275
self._pages_to_refresh.append(page_id)
276276
return
277277

278278
data = {
279-
"pageId": page_id,
280-
"limit": 100000,
279+
"page": {
280+
"id": page_id,
281+
},
282+
"limit": limit,
281283
"cursor": {"stack": []},
282284
"chunkNumber": 0,
283285
"verticalColumns": False,
@@ -310,6 +312,7 @@ def call_query_collection(
310312
sort=[],
311313
calendar_by="",
312314
group_by="",
315+
limit=50
313316
):
314317

315318
assert not (
@@ -323,21 +326,25 @@ def call_query_collection(
323326
sort = [sort]
324327

325328
data = {
326-
"collectionId": collection_id,
327-
"collectionViewId": collection_view_id,
329+
"collection": {
330+
"id": collection_id,
331+
"spaceId": self._client.current_space.id
332+
},
333+
"collectionView": {
334+
"id": collection_view_id,
335+
"spaceId": self._client.current_space.id
336+
},
328337
"loader": {
329-
"limit": 10000,
330-
"loadContentCover": True,
338+
'reducers': {
339+
'collection_group_results': {
340+
'limit': limit,
341+
'type': 'results',
342+
},
343+
},
331344
"searchQuery": search,
332-
"userLocale": "en",
345+
'sort': sort,
333346
"userTimeZone": str(get_localzone()),
334-
"type": type,
335-
},
336-
"query": {
337-
"aggregate": aggregate,
338-
"aggregations": aggregations,
339-
"filter": filter,
340-
"sort": sort,
347+
"type": 'reducer',
341348
},
342349
}
343350

0 commit comments

Comments
 (0)