def update_tracker(): today = date.today() tracker_schema_rows = update_tracker_schema(today) weekly_tracker_db = NotionDB(CONFIG[NOTION_TRACKING_WEEKLY_URL]) daily_tracker_db = NotionDB(CONFIG[NOTION_TRACKING_DAILY_URL]) weeks = {week.id: week for week in weekly_tracker_db.get_all()} current_dict = defaultdict(list) previous_dict = defaultdict(list) for row in daily_tracker_db.rows.values(): week_id = row.Week[0].id week = weeks[week_id] if _is_current_week(today, week): _dict_to_update = current_dict elif _is_previous_week(today, week): _dict_to_update = previous_dict else: continue property_slugs = row._get_property_slugs() for property_slug in property_slugs: if property_slug in {'title', 'name', 'date', 'week', 'final'}: continue _dict_to_update[property_slug].append(getattr(row, property_slug, 0)) for title, tracker_schema_row in tracker_schema_rows.items(): print("*** Updating", title) current_info = current_dict[title] boolean_add = tracker_schema_row.boolean_add current_agg = calculate(current_info, boolean_add) previous_info = previous_dict[title] previous_agg = calculate(previous_info, boolean_add) tracker_schema_row.current_agg = current_agg tracker_schema_row.previous_agg = previous_agg
def update_daily_tracker_from_tasks(): today = date.today() daily_tracker_db = NotionDB(CONFIG[NOTION_TRACKING_DAILY_URL]) tasks_db = NotionDB(CONFIG[NOTION_TASKS_URL]) trackable_things_db = NotionDB(CONFIG[NOTION_TRACKABLE_THINGS_URL]) for trackable_things in trackable_things_db.get_all(): task_name = getattr(trackable_things, 'tasks_name') if not trackable_things.boolean_add: print(f"Skipping as {task_name} is not boolean") continue if not task_name: print("Skipping as task name is not matched") continue task = tasks_db.get(task_name) if not task: print("Skipping the task", task_name) continue start_date = task.scheduled.start if isinstance(start_date, datetime): start_date = start_date.date() if not task.done: print("Skipping the task not done", task.title) continue daily_tracked = daily_tracker_db.get(start_date.strftime("%b %d")) if not daily_tracked: print("Skipping the task as not on daily_tracked", task.title) continue print("Setting", trackable_things.title, task.done) setattr(daily_tracked, trackable_things.title, task.done)
def update_tasks(): task_db = NotionDB(CONFIG[NOTION_TASKS_URL]) project_db = NotionDB(CONFIG[NOTION_PROJECT_URL]) update_notion_jira_tasks(task_db, project_db) for task in task_db.get_all(): if task.done: task.status = 'Done' if task.parent_name and not task.parent_task: parent_task = task_db.get_or_create(task.parent_name) task.parent_task = parent_task update_from_hiring_board()
def create_calendar_from_tasks(): task_db = NotionDB(CONFIG[NOTION_TASKS_URL]) def create_summary(task): title = task.title context = f' @ {task.context}' if task.context else '' return f"{title}{context}" def create_description(task): summary = f' ({task.summary})' if task.summary else task.title if task.link: summary = f'<a href="{task.link}">{summary}</a>' return summary def ensure_datetime(dt): if isinstance(dt, date): dt = datetime(dt.year, dt.month, dt.day) return dt def ensure_date(dt): if isinstance(dt, datetime): return dt.date() return dt def to_date_time(start_date, end_date): if end_date and ensure_date(start_date) != ensure_date(end_date): return ensure_date(start_date), ensure_date(end_date) if isinstance(start_date, datetime): if not end_date: end_date = start_date + timedelta(seconds=1800) else: end_date = ensure_datetime(end_date) return start_date, end_date return ensure_date(start_date), ensure_date(end_date) calendar = Calendar() for task in task_db.get_all(): if not task.scheduled or task.done or task.task_type == 'Event': continue event = Event() print("[*]", task.title) event['uid'] = str(task.id) start, end = to_date_time(task.scheduled.start, task.scheduled.end) event.add('dtstart', start) if end: event.add('dtend', end) event['summary'] = create_summary(task) event['description'] = create_description(task) calendar.add_component(event) return calendar.to_ical()
def update_daily_hotspots(): end_date = datetime.now(tz=pytz.FixedOffset(330)) start_date = end_date - timedelta(days=7) daily_tracking_db = NotionDB(daily_tracking_url) while start_date <= end_date: set_final = False if start_date >= end_date else True st = start_date.date() row = daily_tracking_db.find_one_by('Date', st) if row and not row.Final: print('Updating:', row.title, st) data = get_data(st) for k, v in data.items(): setattr(row, k, v) if set_final: row.Final = True else: print("Skipping", st) start_date += timedelta(days=1)
def update_from_hiring_board(): task_db = NotionDB(CONFIG[NOTION_TASKS_URL]) hiring_project_url = "https://www.notion.so/Hiring-4b932bc27b434d9d8700488acbc0f463" hiring_project = task_db.client.get_block(hiring_project_url) def setup(_task, board): title = f"[{board}] {_task.title}" print(f'[*] {title}') task_row = task_db.get_or_create(title) task_row.link = _task.get_browseable_url() task_row.project = hiring_project.id task_row.context = 'Office' task_row.task_type = 'Story' task_row.link = _task.get_browseable_url() if getattr(_task, 'Scheduled', None) and isinstance( _task.Scheduled, NotionDate): task_row.scheduled = NotionDate(_task.Scheduled.start, _task.Scheduled.end, DEFAULT_TIMEZONE) if task.Status in {'Dropped', 'Offered'}: task_row.done = True else: task_row.done = False qa_board = NotionDB(CONFIG[NOTION_QA_HIRING_URL]) be_board = NotionDB(CONFIG[NOTION_BE_HIRING_URL]) tasks_on_hiring_board = {} for task in qa_board.get_all(): tasks_on_hiring_board[task.get_browseable_url()] = ('QA', task) for task in be_board.get_all(): tasks_on_hiring_board[task.get_browseable_url()] = ('BE', task) for task in task_db.get_all(): if not task.link or not tasks_on_hiring_board.get(task.link): continue board_type, _task = tasks_on_hiring_board.get(task.link) if not _task: continue if not any(task_db.client.current_user == u for u in _task.Assign) or \ _task.Status in {"Dropped", "Offered"}: task.remove() else: setup(_task, board_type)
def update_tracker_schema(today: date): today_title = today.strftime("%b %d") daily_tracker_db = NotionDB(CONFIG[NOTION_TRACKING_DAILY_URL]) trackable_things_db = NotionDB(CONFIG[NOTION_TRACKABLE_THINGS_URL]) row = daily_tracker_db.get(today_title) if not row: raise Exception("Not the right time to be doing this!") property_slugs = row._get_property_slugs() for property_slug in property_slugs: if property_slug in {'title', 'name', 'date', 'week', 'final'}: continue r = trackable_things_db.get_or_create(property_slug) if not r.boolean_add and type(getattr(row, property_slug, None)) == bool: r.boolean_add = True return {u.title: u for u in trackable_things_db.get_all()}
def update_all_events_from_primary(): task_db = NotionDB(CONFIG[NOTION_TASKS_URL]) p_calendar = GoogleCalendar(PERSONAL, CONFIG[GOOGLE_CREDS_PERSONAL]) o_calendar = GoogleCalendar(OFFICE, CONFIG[GOOGLE_CREDS_OFFICE]) from_date = datetime.now( tz=pytz.timezone('Asia/Kolkata')).today() - timedelta(days=7) to_date = datetime.now( tz=pytz.timezone('Asia/Kolkata')).today() + timedelta(days=7) events = chain(o_calendar.get_events('primary', from_date, to_date), p_calendar.get_events('primary', from_date, to_date)) not_any_more_on_calendar = set() for event in sorted(events, key=lambda u: conv(u.scheduled_start)): print("[*] Updating event:", event.name, event.scheduled_start) update_task_on_notion(task_db, event) not_any_more_on_calendar.add(event.name) for task_name in task_db.rows: if task_name in not_any_more_on_calendar: continue task = task_db.rows[task_name] if task.task_type != 'Event': continue print("[*] Deleting event:", task_name) task.remove()
def update_calendar_times(): task_db = NotionDB(CONFIG[NOTION_TASKS_URL]) p_calendar = GoogleCalendar(PERSONAL, CONFIG[GOOGLE_CREDS_PERSONAL]) o_calendar = GoogleCalendar(OFFICE, CONFIG[GOOGLE_CREDS_OFFICE]) all_events = {} all_tasks = {} def update_tasks(name): task = task_db.get_or_create(name) event = all_events[name] task.scheduled.start = event.scheduled_start task.scheduled.end = event.scheduled_end task.calendar_link = event.link task.context = event.context if event.description: task.summary = event.description if event.recurring: task.done = False def update_calendar(name): task = task_db.get_or_create(name) if name not in all_events: event = GoogleCalendarData( task.title, task.scheduled.start, task.scheduled.end, task.context, 'confirmed', None, task.summary, ) method = 'create_event' else: event = all_events[name] event.name = task.title event.scheduled_start = task.scheduled.start event.scheduled_end = task.scheduled.end event.description = task.scheduled.description event.context = task.context event.status = 'confirmed' method = 'update_event' if task.context == PERSONAL: event = getattr(p_calendar, method)(event, 'primary') elif task.context == OFFICE: event = getattr(o_calendar, method)(event, 'primary') else: raise Exception(f"Invalid context {task.context}") task.calendar_link = event.id all_tasks.update({task.title: task for task in task_db.get_all() if task.scheduled and not task.done}) all_events.update({e.name: e for e in list( p_calendar.get_events())}) all_events.update({e.name: e for e in list( o_calendar.get_events())}) tasks_to_add, events_to_add = get_updatable_entities(all_tasks, all_events) for name in tasks_to_add: update_tasks(name) for name in events_to_add: update_calendar(name)
def update_notion(): task_db = NotionDB(CONFIG[NOTION_TASKS_URL]) for event in calendar_primary_events(): update_event(task_db, event)
TASK_GOAL_PROPERTY_MAP = { 'goal': '@qm>', 'description': '9iqC', 'type': 'qrM3', 'epic': '5fSo', 'scheduled': 'WZBl', 'status': ':tjE', 'minor': '<4L(', 'link': 'f$el', 'init': '=tG;', 'done': 'h,N?', 'next': 'fQ&A', 'waiting_on': 'WdYM', 'dependents_finished': 'j)H^', } BOOKS_GOAL_PROPERTY_MAP = { 'goals': '*fnW' } VIDEOS_GOAL_PROPERTY_MAP = { 'goals': '&r0m' } db = NotionDB(url) for p in db.view._get_record_data()['format'][ 'table_properties']: if p['visible']: print(p['property'])
from notion_api import NotionDB TOKEN = "76c45addb4795e4e324231c33610975e4aadf213f43b062bac51316343bfc7001e17053c3c65e87bc4375b9a525f9cd4f544cf9b1dc7b5cadcf534851e03105b4ffed4e936726b948d4d5048c405" #1812e4316f612e3740c5686bc75bd2e3057c1722f88099f755ed4a3b79b64bcd11ea5c720cc263da7ace59300868947cf84d480c62af42d233e13f0a6c13127836b8f58427d8539e3f50483cf291 URL = "https://www.notion.so/a2c28de35349469caedf8cbc78ef4404?v=f55491f003c14d0b9d7ff6f5ede4ccac" file_path = '/Users/arshad/Desktop/Gaurav_resume.pdf' db = NotionDB(URL, token=TOKEN) title = 'File Upload Example' field = 'Files' with open(file_path, 'rb') as data: db.upload_file(title, field, 'application/pdf', 'Gaurav_resume.pdf', data) row = db.get_or_create(title) print(row.title, row.Files)
def update_calendar_times(): task_db = NotionDB(CONFIG[NOTION_TASKS_URL]) p_calendar = GoogleCalendar(PERSONAL, CONFIG[GOOGLE_CREDS_PERSONAL]) o_calendar = GoogleCalendar(OFFICE, CONFIG[GOOGLE_CREDS_OFFICE]) all_events = {} all_tasks = {} def get_calendar_by_context(context, is_primary=False): if context == PERSONAL: return (p_calendar, 'primary' if is_primary else PERSONAL_NOTION) elif context == OFFICE: return (o_calendar, 'primary' if is_primary else OFFICE_NOTION) raise Exception(f"Invalid context provided: {context}") key_gen = lambda c, u: f"{c}-{u}" all_tasks.update( {task.title: task for task in task_db.get_all() if task.scheduled}) all_events.update({ key_gen(e.context, e.id): e for e in p_calendar.get_events(PERSONAL_NOTION) }) all_events.update({ key_gen(e.context, e.id): e for e in o_calendar.get_events(OFFICE_NOTION) }) for task in all_tasks.values(): if task.context not in {PERSONAL, OFFICE}: print("[*] Task does not have context", task.title, task.context) continue calendar, calendar_id = get_calendar_by_context(task.context) event_key = key_gen(task.context, task.calendar_id) if task.task_type == 'Event': print("[*] Task is event", task.title, task.context) continue if task.done: print("[*] Task is done", task.title, task.context) if task.calendar_id and event_key in all_events: all_events.pop(event_key) _event = calendar.get_event(task.calendar_id, calendar_id) if _event: calendar.delete_event(task.calendar_id, calendar_id) print("\t... deleted from calendar...") task.calendar_id = None print("\t... skipped...") continue try: if not task.calendar_id: print(f"[{task.name}] Creating new event") created_event = create_calendar_event(task, calendar, calendar_id) task.calendar_id = created_event.id else: event = all_events.get(event_key) update_calendar_or_notion(task, event, calendar, calendar_id) except: print(task.title, all_events.get(event_key)) raise update_all_events_from_primary()