Skip to content

Commit

Permalink
don't constantly repull images from notion
Browse files Browse the repository at this point in the history
  • Loading branch information
Highfire1 committed Apr 18, 2024
1 parent 9fb17f1 commit 4886210
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion notion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from dotenv import load_dotenv

from os import environ
from os.path import exists
import json
import requests
from datetime import datetime

from notion_client import Client
from pydantic import BaseModel
Expand Down Expand Up @@ -74,6 +76,11 @@ def updateDataFromNotion(writeLocation="data/"):
if not os.path.exists(writeLocation + "json/"):
os.makedirs(writeLocation + "json/")

if exists(f"{writeLocation}/json/execs_export.json"):

with open(f"{writeLocation}/json/execs_export.json", "r") as fi:
cached_data = json.loads(fi.read())

load_dotenv()
if "NOTION_API_TOKEN" not in environ:
raise Exception("Please provide a Notion integration token.")
Expand Down Expand Up @@ -139,6 +146,20 @@ def multiplePropTextExtractor(property:dict) -> list[str] | None:

p = page["properties"]

last_updated = page["last_edited_time"]
student_id = propTextExtractor(p["Student ID"]),

# don't go through the trouble of everything if the page hasn't changed
# right now everything is simply downloading the exec images
stale_data = False

for exec in cached_data:
if exec["student_id"] == student_id[0]:
if last_updated == exec["last_updated"]:
stale_data = True
break


# special code is needed to handle relations in the db
current_roles = multiplePropTextExtractor(p["Role"])
past_roles = multiplePropTextExtractor(p["Prior Roles"])
Expand All @@ -160,7 +181,7 @@ def multiplePropTextExtractor(property:dict) -> list[str] | None:
# download the image for each exec, if available
student_id = propTextExtractor(p["Student ID"])

if (p["Profile Picture"]["files"] != []):
if (not stale_data and p["Profile Picture"]["files"] != []):
file_name:str = p["Profile Picture"]["files"][0]["name"]
file_url:str = p["Profile Picture"]["files"][0]["file"]["url"]
file_extension:str = file_name.split('.')[-1]
Expand Down

0 comments on commit 4886210

Please sign in to comment.