def step_three(): token = os.environ.get('DO_NOT_COMMIT_NOTION_TOKEN') if not token: print( "Can't find token in environment. Did you do step 2 correctly or ran source .config?" ) return client = NotionClient(token_v2=token) parent_page = client.get_block(os.environ.get('NOTION_PAGE_URL')) for block in parent_page.children: try: if block.title == EXPENSE_TABLE_TITLE: print('Expenses Table already exists!') return except: continue cvb = parent_page.children.add_new(CollectionViewBlock) cvb.collection = client.get_collection( client.create_record("collection", parent=cvb, schema=get_collection_schema())) cvb.title = EXPENSE_TABLE_TITLE view = cvb.views.add_new(view_type="table") print('Success! Go back to the README for the last step')
def create_ticket_collection(title, views: [Collection_Type], tickets: [Ticket]): # Get notion config settings notion_config = Notion_config() # Set up client and get page to work on client = NotionClient(token_v2=notion_config.token) page = client.get_block(notion_config.base_page) # Add a new collection collection = page.children.add_new(CollectionViewBlock) collection.collection = client.get_collection( client.create_record("collection", parent=collection, schema=ticket_collection_scheme(tickets))) now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") collection.title = title + ' ' + dt_string # Add desired Views for view in views: collection.views.add_new(view_type=view.value) return collection
def notion_temp(self): token_v2 = '' #notion 사용자 고유의 토큰 주소 client = NotionClient(token_v2=token_v2) url = '' #자동으로 입력할 noiton 페이지 page = client.get_block(url) # 표 새로 만들고 저장 child = page.children.add_new(CollectionViewBlock) child.collection = client.get_collection( client.create_record( "collection", parent=child, schema={ "title": {"name": "내용", "type": "title"}, "date": {"name": "날짜", "type": "date"} }) ) child.title = '2020년도 회의록' child.views.add_new(view_type="table") row = child.collection.add_row() row.set_property('title', self.title) row.set_property('date', datetime.strptime(self.date, '%Y-%m-%d')) text = row.children.add_new(TextBlock) text.title = self.summary_result return url
class NotionInterface: def __init__(self): self.token_v2 = TOKEN_V2 self.client = NotionClient(token_v2=self.token_v2) self.strava_page_title = "Strava" self.strava_table_title = "Activity Logbook" def get_strava_page(self): strava_page = None for page in self.client.get_top_level_pages(): if self.strava_page_title == page.title: strava_page = page if strava_page is None: raise LookupError( f"Unable to find Strava page, is there a page with a " f"title of '{self.strava_page_title}' in your workspace?" ) return strava_page def create_activity_log_table(self): strava_page = self.get_strava_page() # Check if we've a table already for block in strava_page.children: if hasattr(block, 'title') and block.title == self.strava_table_title: return block # Create a table strava_table = strava_page.children.add_new(CollectionViewBlock) strava_table.collection = self.client.get_collection( self.client.create_record("collection", parent=strava_table, schema=SCHEMA) ) strava_table.title = self.strava_table_title strava_table.views.add_new(view_type="table") return strava_table def add_row_to_table(self, table, data): # HACK - needs to happen to work! table.collection.parent.views rows = table.collection.get_rows() if not any([r.name == data.name and r.date.start.date() == data.start_date_local.date() for r in rows]): row = table.collection.add_row() row.set_property("title", data.name) row.set_property("date", data.start_date_local) row.set_property("type", data.type) row.set_property("distance (m)", data.distance) row.set_property("time (s)", data.moving_time) row.set_property("cals", data.kilojoules) print(f'Added {data.name} on {data.start_date_local} to Notion!') else: print(f'Skipping {data.name} for {data.start_date_local}')
class Notion: def __init__(self, token): self.client = NotionClient(token_v2=token) self.url = None def set_url(self, url): self.url = url def get_table(self, dropna=False): table = self.client.get_collection_view(self.url) rows = [] for row in table.collection.get_rows(): rows.append(self._get_row_item(row)) table_df = pd.DataFrame(rows, columns=list(row.get_all_properties().keys())) if dropna: table_df = table_df.dropna().reset_index(drop=True) return table_df def _get_row_item(self, row): items = [] for col, item in row.get_all_properties().items(): type_ = type(item) item = row.get_property(identifier=col) if type_ not in [list, NotionDate]: items.append(item) elif type_ == list: items.append(' '.join(item)) elif type_ == NotionDate: items.append(item.__dict__['start']) return items def insert_rows(self, item_dict): table = self.client.get_collection_view(self.url) row = self._create_new_record(table) for col_name, value in item_dict.items(): row.set_property(identifier=col_name, val=value) def _create_new_record(self, table): row_id = self.client.create_record('block', parent=table.collection, type='page') row = CollectionRowBlock(self.client, row_id) with self.client.as_atomic_transaction(): for view in self.client.get_block(table.get("parent_id")).views: view.set("page_sort", view.get("page_sort", []) + [row_id]) return row
def main(): # login client = NotionClient(token_v2=TOKEN) # create new page page = client.get_block(PAGE_URL) if len(page.children) > 1: # if table exists del page.children[1] child = page.children.add_new(CollectionViewBlock) child.collection = client.get_collection( client.create_record( "collection", parent=child, schema=get_schema_todo()) ) child.title = 'Ecampus Assignments' child.views.add_new(view_type="table") for assignment in ASSIGNMENT: row = child.collection.add_row() row.set_property('title', assignment['assignment_title']) row.set_property('course', assignment['course']) row.set_property('date', assignment['assignment_date']) row.set_property('submit', assignment['is_submitted']) else: # if table not exists child = page.children.add_new(CollectionViewBlock) child.collection = client.get_collection( client.create_record( "collection", parent=child, schema=get_schema_todo()) ) child.title = 'Ecampus Assignments' child.views.add_new(view_type="table") for assignment in ASSIGNMENT: row = child.collection.add_row() row.set_property('title', assignment['assignment_title']) row.set_property('course', assignment['course']) row.set_property('date', assignment['assignment_date']) row.set_property('submit', assignment['is_submitted'])
def add_notion(token_v2, url, df): # update comments try: df = update_comments_table(token_v2, url, df) print('[NOTION] Update comments table') except: print('[NOTION] Create new comments table') # add check box property df['check'] = False df = df.sort_values(['check', 'date'], ascending=[False, False]) print(df.info()) client = NotionClient(token_v2=token_v2) page = client.get_block(url) # clean page if len(page.children) > 0: for i in range(len(page.children)): del page.children[i] child = page.children.add_new(CollectionViewBlock) child.collection = client.get_collection( client.create_record("collection", parent=child, schema=get_schema_comments(df.columns))) # set korea time kor_time = datetime.datetime.utcnow() + datetime.timedelta(hours=9) child.title = kor_time child.views.add_new(view_type='table') for i in range(len(df)): row = child.collection.add_row() row.set_property('title', str(i)) for col in df.iloc[i].index: if col == 'check': row.set_property(col, bool(df.iloc[i][col])) else: row.set_property(col, str(df.iloc[i][col]))
class NotionReporter(object): def __init__(self, token, cycle_url, test_url, execution_url, cycle_name): logger.info('Inside NotionReporter') self.cycle_name = cycle_name self.notion = NotionClient(token_v2=token) self.cycles = self.notion.get_collection_view(cycle_url) self.tests = self.notion.get_collection_view(test_url) self.test_executions = self.notion.get_collection_view(execution_url) # Add test cycle self.cycle = self.cycles.collection.add_row( icon='🚴', name=cycle_name, date_executed=NotionDate(datetime.now()), execution_status='Unexecuted') self.cycle.set('format.page_cover', CYCLE_COVER) self.cycle.set('format.page_cover_position', 0.5) def pytest_collection_modifyitems(self, config, items): for item in items: notion_marker = [ mark for mark in item.own_markers if mark.name == 'notion_test' ] if notion_marker: test_case_name = notion_marker[0].args[0] test_cases = self.tests.build_query( search=test_case_name).execute() if test_cases: item.user_properties.append(test_cases[0]) def pytest_runtest_logreport(self, report): if report.when == 'call': test_execution = self.test_executions.collection.add_row( icon='🔫', name='{0} - {1}'.format(self.cycle_name, report.nodeid), date_executed=NotionDate(datetime.now()), executed_by=self.notion.current_user, test_cycle=self.cycle.id) test_execution.set('format.page_cover', EXECUTION_COVER) test_execution.set('format.page_cover_position', 0.5) if report.user_properties: test_execution.name = '{0} - {1}'.format( self.cycle_name, report.user_properties[0].title) test_execution.test_case = report.user_properties[0].id if report.passed: test_execution.status = 'Passed' if report.failed: test_execution.status = 'Failed' test_execution.notes = report.longreprtext def pytest_terminal_summary(self, terminalreporter, exitstatus, config): if exitstatus == 0: self.cycle.execution_status = 'Passed' if exitstatus == 1: self.cycle.execution_status = 'Failed' exec_records = self.test_executions.collection.get_rows() test_exec_prop = [ item for item in exec_records[0].schema if item.get('slug') == 'test_cycle' ][0] filter = [{ 'property': test_exec_prop.get('id'), 'comparator': 'enum_contains', 'value': self.cycle.id, 'type': 'relation' }] cvb = self.cycle.children.add_new(CollectionViewBlock) view = self.notion.get_collection_view( self.notion.create_record("collection_view", parent=cvb, type="board"), collection=self.test_executions.collection) view.set("collection_id", self.test_executions.collection.id) cvb.set("collection_id", self.test_executions.collection.id) cvb.set("view_ids", [view.id]) cvb.title = "Test Executions" view.set('query.filter', filter)
'multi_select', "options": [{ "id": "dd3500bb-eb53-4c74-9c4b-2f84d508acde", "value": "Day One" }] } } for source_file in args.files: journal_name = Path(source_file).stem print(f'Importing journal: {journal_name}') journal_page = archive_page.children.add_new(CollectionViewPageBlock) journal = client.get_collection( client.create_record('collection', parent=journal_page, schema=journal_schema)) journal_page.collection = journal journal_page.views.add_new(view_type='table') journal_page.title = journal_name with open(source_file) as fp: source_data = json.load(fp) for entry in source_data['entries']: if 'text' not in entry: continue text = entry['text'].splitlines(keepends=False) title = text[0] body = "\n".join(text[1:])
client = NotionClient(token_v2=token_v2) # Access a database using the URL of the database page or the inline block page = client.get_block(page_link) page.title = "Kindle Highlights" page.set( "format.page_cover", "https://www.incimages.com/uploaded_files/image/1920x1080/getty_598063032_349381.jpg" ) page.icon = "https://i.pinimg.com/originals/2c/fc/93/2cfc93d7665f5d7728782700e50596e3.png" cvb = page.children.add_new(CollectionViewBlock) cvb.collection = client.get_collection( client.create_record("collection", parent=cvb, schema=get_collection_schema())) cvb.title = "Read them!" view = cvb.views.add_new(view_type="table") gallery_view = cvb.views.add_new(view_type="gallery") for book in data.keys(): print(book) #col=collection. row = cvb.collection.add_row() row.name = book book_description, image_link, book_author, page_count, average_rating, book_categories = search( book) if image_link.startswith("'") and image_link.endswith("'"): image_link = image_link[1:-1] if image_link.startswith('"') and image_link.endswith('"'):
class NotionCTF: def _get_status(status): status = status.lower() if status.find("stuck") >= 0: return ChallState.Stuck elif status.find("solve") >= 0: return ChallState.Solved else: return ChallState.Progress STATUS_TOSTR = ['in progress 🤔', 'stuck 😣', 'solved 😎'] def _set_status(status): return NotionCTF.STATUS_TOSTR[status.value] def _get_subscribers(client, token_v2): r = requests.post("https://www.notion.so/api/v3/getSubscriptionData", \ json=({'spaceId': client.current_space.id}), \ cookies={'token_v2':token_v2}) ret = dict() for user in r.json()['members']: user_ = User(client=client, id=user['userId']) ret[user_.given_name] = user_ return ret def __init__(self, token_v2, url): self.token_v2 = token_v2 self.client = NotionClient(token_v2=token_v2) self.users = NotionCTF._get_subscribers(self.client, token_v2) self.cv = self.client.get_collection_view(url) # update all challenges, except those in `updated`, # into Python def update_from_notion(self, ctf, updated): if updated is None: updated = [] rows = self.cv.collection.get_rows() for row in rows: if row.Name is None or row.Type is None or row.Status is None or row.Type == []: continue if row.Type[0].lower() + '-' + row.Name in updated: continue chall = ctf.get_chall(row.Type[0].lower(), row.Name) chall.solved = NotionCTF._get_status(row.Status) chall.workings = set(map(lambda x: x.given_name, row.Candidates)) def _get_user(self, name): r = self.users.get(name) if r: return r self.users = NotionCTF._get_subscribers(self.client, self.token_v2) return self.users.get(name) def _update_row(self, row, challenge): row.Status = NotionCTF._set_status(challenge.solved) candidates = [] for user in challenge.workings: r2 = self._get_user(user) if r2: candidates.append(r2) row.Candidates = candidates TO_CATEGORY = set([ 'reverse', 'misc', 'pwn', 'web', 'shellcoding', 'osint', 'reverse', 'programming', 'crypto' ]) CATEGORY_ALIAS = dict(rev='reverse', code='shellcoding', prog='programming') def _to_notion_category(category): category = category.lower() if category in NotionCTF.TO_CATEGORY: return category if NotionCTF.CATEGORY_ALIAS.get(category): return NotionCTF.CATEGORY_ALIAS.get(category) for c in NotionCTF.TO_CATEGORY: if c.find(category) == 0: return c return 'unknown' # update challenges in `to_update` to notion # if the challenge does not exist, create one first def update_to_notion(self, ctf, to_update): row_map = dict() rows = self.cv.collection.get_rows() visited = [False] * len(to_update) for row in rows: # iterate over current rows try: idx = to_update.index(row.Type[0].lower() + '-' + row.Name) challenge = ctf.get_chall(row.Type[0].lower(), row.Name) self._update_row(row, challenge) visited[idx] = True # update if in to_update except Exception as e: # not in to_update pass for i in range(0, len(visited)): if not visited[i]: idx = to_update[i].find('-') category, name = to_update[i][:idx], to_update[i][idx + 1:] challenge = ctf.get_chall(category, name) #row = self.cv.collection.add_row() row_id = self.client.create_record("block", self.cv.collection, type="page") row = CollectionRowBlock(self.client, row_id) row.Name = name row.Type = [NotionCTF._to_notion_category(category)] self._update_row(row, challenge)