Beispiel #1
0
def test_client_request_auth(token):
    client = Client()

    with pytest.raises(APIResponseError):
        client.request("/users", "GET")

    response = client.request("/users", "GET", auth=token)
    assert response["results"]
Beispiel #2
0
def run_repeaters():
    logger = custom_logging.get_logger(__name__)
    client = Client(auth=NOTION_API_TOKEN, logger=logger)

    repeaters_data = client.databases.query(
        TASK_REPEATER_DATABASE_ID,
        filter={
            "property": "Active",
            "checkbox": {
                "equals": True
            }
        }
    )

    repeaters = [TaskRepeater(client, data=data) for data in repeaters_data['results']]
    for repeater in repeaters:
        repeater.execute()
Beispiel #3
0
def main(config):
    """
    Usage:
    
    update_properties.py -c config.ini
    """

    # Read config file
    cfg = configparser.ConfigParser()
    cfg.read(config)
    notion_token = cfg['Notion']['notion_token']
    notion_table_id = cfg['Notion']['notion_table_id']
    url_connected_papers = cfg['PDFs']['url_connected_papers']

    notion = Client(auth=notion_token)
    cursor = 1
    while cursor:
        if cursor == 1:
            cursor = None
        notion_records = notion_db_query(notion, notion_table_id, cursor)

        for nr in notion_records['results']:
            if nr['properties']['Graph']['url'] is None:
                title = nr['properties']['Title']['rich_text'][0]['plain_text']
                print(title)
                try:
                    url = urllib.parse.quote(title)
                    notion.pages.update(nr['id'],
                                        properties={
                                            "Graph": {
                                                'type': 'url',
                                                'url':
                                                url_connected_papers + url
                                            }
                                        })
                except:
                    continue

        cursor = notion_records['next_cursor']
def main(config, zotero_export):
    """
    Usage:
    
    update_crossref.py -c config.ini -e exported.txt
    """

    # Read config file
    config = os.path.expanduser(config)
    cfg = configparser.ConfigParser()
    cfg.read(config)
    impact_factor = cfg['Resources']['impact_factor']
    cas = cfg['Resources']['cas']
    notion_token = cfg['Notion']['notion_token']
    notion_table_id = cfg['Notion']['notion_table_id']
    zotero_library_id = cfg['Zotero']['zotero_library_id']
    zotero_api_key = cfg['Zotero']['zotero_api_key']
    pdf_local_folder = cfg['PDFs']['pdf_local_folder']
    pdf_local_url = cfg['PDFs']['pdf_local_url']
    pdf_remote_url = cfg['PDFs']['pdf_remote_url']
    supplementary_path = cfg['PDFs']['supplementary_path']

    df_parent2child =
    df_parent2relations = 
    df_child2relations = df_parent2child.merge(df_parent2relations, on="parent", how="inner")


    # Fetch records in zotero library 
    df = fetch_zotero_records(zotero_library_id, zotero_api_key, zotero_topn)
    # Change journal name to lowercase for easier matching with impact facter and CAS table
    df['journal'] = df.apply(lambda x:journal_lower(x), axis=1)

    # Link records with impact factor and CAS classification
    dfif = merge_IF_CAS(impact_factor, cas)
    tbl = df.merge(dfif, on='journal', how='left')

    # Add extra information based on zotero metadata
    tbl['Date_added'] = tbl.apply(lambda x: str(x['Date_added']), axis=1)
    tbl['Date_published'] = tbl.apply(lambda x: str(x['Date_published']), axis=1)
    tbl['Local_file'] = tbl.apply(lambda x: pdf_url(x, pdf_local_url), axis=1)
    tbl['Name'] = tbl.apply(lambda x: file_name(x), axis=1)
    tbl['Impact_factor'] = tbl.apply(lambda x:round(x['Impact_factor'], 1), axis=1)
    tbl['Authors'] = tbl.apply(lambda x:reformat_names(x['Author']), axis=1)
    tbl.rename(columns={"Publication Title": "Journal"}, inplace=True)
    tbl.Impact_factor.fillna(0, inplace=True)
    tbl.CAS.fillna("NA", inplace=True)
    tbl.Subject.fillna("NA", inplace=True)
    tbl.fillna('', inplace=True)

    # Some journals in zotero are not in CAS or JCR, set them as NA
    # todo: manually check these recores, because most of them are false renamed by zotero
    tbl['CAS'] = tbl.apply(lambda x: x['CAS'] if len(x['CAS'])==2 else 'NA', axis=1)
    tbl['PDF'] = tbl.apply(lambda x: x['Local_file'].replace(pdf_local_url, pdf_remote_url), axis=1)


    # Fetch recent 100 records from notion table
    notion = Client(auth=notion_token)
    notion_records = notion.databases.query(**{"database_id": notion_table_id,
                             "sort": [{"timestamp":"Date_added", "direction":"descending"}]})

    # If the database is empty, create necessary columns
    if len(notion_records['results'])==0:
        print("This is an empty notion database, please ensure you set columns properly")
        # ------ TODO: set columns --------
        # Delete all columns in database
        # schema = {}
        # cv.collection.set(path=['schema'], value=schema)

#         schema = cv.collection.get()['schema']
#         columns_existed = [x['name'] for x in schema.values()]
#         columns_to_be_added = {x:properties[x] for x in properties if x not in columns_existed}
        
#         # Create new columns
#         for column in columns_to_be_added:
#             schema[column] = {
#                 "name": properties[column]['name'],
#                 "type": properties[column]['type']
#             }
#         cv.collection.set(path=['schema'], value=schema)
    else:
        literature_existed = [x["properties"]["Name"]['title'][0]['text']['content'] for x in notion_records['results']]
        tbl = tbl[~tbl.Name.isin(literature_existed)]
        print("Updating {} files...".format(tbl.shape[0]))
    

    ## Add to notion
    if len(tbl) > 0:
        recs = tbl.to_dict(orient="records")
        for rec in recs:
            props = new_row(notion, notion_table_id, rec)
            notion.pages.create(parent={"database_id": notion_table_id}, properties=props)
    else:
        print("No new records in zotero library!")
    
    # Create folder for supplementary files
    add_supp_dir(pdf_local_folder, supplementary_path)
Beispiel #5
0
            logging.basicConfig(level=logging.WARN)

    return conf


################################################################################
## MAIN ENTRY

args = parse_args()
conf = load_config(args.config)
log = logging.getLogger(__name__)

# TODO consider switching to Notional ...
# client = notional.connect(auth=conf["auth_token"])

client = Client(auth=conf["auth_token"])
archive = builder.PageArchive(client, conf["import_page_id"])

notes = apple.Notes()
note_ids = notes.get_all_ids()

for note_id in note_ids:
    note = notes[note_id]

    # skip empty notes
    if note is None:
        log.warning("empty note; skipping")
        continue

    note_meta = note["meta"]
    note_name = note_meta["name"]
Beispiel #6
0
from settings import NOTION_API_TOKEN
from notion_client import Client

notion = Client(auth=NOTION_API_TOKEN)
result = notion.search(query='月亮小姐')
for o in result['results']:
    print(o['object'], o['id'])

# Doesn't return anything
print(notion.search(query='林孟璇'))

page = notion.pages.retrieve(page_id='8d422f94-c904-4113-a683-197ff00aef20')

db = notion.databases.retrieve(
    database_id='df4dfb3f-f36f-462d-ad6e-1ef29f1867eb')
sorts = []
# sorts = [{'direction': 'ascending', 'timestamp': 'created_time'}]
result = notion.databases.query(
    database_id='df4dfb3f-f36f-462d-ad6e-1ef29f1867eb', sorts=sorts)

for row in result['results']:
    title = row['properties']['Chinese']['title']
    if len(title) == 0:
        print('')
    else:
        print(title[0]['plain_text'])

# Doesn't work for database IDs
result = notion.blocks.children.list(
    block_id='df4dfb3f-f36f-462d-ad6e-1ef29f1867eb')
Beispiel #7
0
try:
    from dotenv import load_dotenv
except ModuleNotFoundError:
    print("Could not load .env because python-dotenv not found.")
else:
    load_dotenv()

NOTION_TOKEN = os.getenv("NOTION_TOKEN", "")

while NOTION_TOKEN == "":
    print("NOTION_TOKEN not found.")
    NOTION_TOKEN = input("Enter your integration token: ").strip()

# Initialize the client
notion = Client(auth=NOTION_TOKEN)

# Search for an item
print("\nSearching for the word 'People' ")
results = notion.search(query="People").get("results")
print(len(results))
result = results[0]
print("The result is a", result["object"])
pprint(result["properties"])

database_id = result[
    "id"]  # store the database id in a variable for future use

# Create a new page
your_name = input("\n\nEnter your name: ")
gh_uname = input("Enter your github username: ")
def add_to_synchronization_error_database(
    exception: Exception,
    provider_name: str,
    venue_id: int,
    venue_id_at_offer_provider: str,
):
    if settings.IS_DEV:
        return

    try:
        notion = Client(auth=settings.NOTION_TOKEN)
        notion.pages.create(
            parent={"database_id": PROVIDER_API_ERRORS_DATABASE_ID},
            properties={
                "Détail": {
                    "title": [{
                        "text": {
                            "content": str(exception)[:2000]
                        }
                    }],
                },
                "Type": {
                    "select": {
                        "name": exception.__class__.__name__
                    }
                },
                "Provider": {
                    "select": {
                        "name": provider_name
                    }
                },
                "VenueId": {
                    "rich_text": [{
                        "text": {
                            "content": str(venue_id)
                        }
                    }],
                },
                "VenueIdAtOfferProvider": {
                    "rich_text": [{
                        "text": {
                            "content": str(venue_id_at_offer_provider)
                        }
                    }]
                },
                "Environnement": {
                    "select": {
                        "name": settings.ENV
                    }
                },
                "Statut": {
                    "select": {
                        "name": "New"
                    }
                },
            },
        )
    except APIResponseError as error:
        logger.exception(
            "Could not create page to notion database",
            extra={
                "error": str(error),
                "error_code": error.code,
            },
        )
Beispiel #9
0
def client(token):
    return Client({"auth": token})
def test_initialize_client():
    client = Client({"auth": "foo"})
    assert client

    client = AsyncClient({"auth": "foo"})
    assert client
Beispiel #11
0
import os

from notion_client import Client
from notion_client.helpers import get_id

NOTION_TOKEN = os.getenv("NOTION_TOKEN", "")

while NOTION_TOKEN == "":
    print("NOTION_TOKEN not found.")
    NOTION_TOKEN = input("Enter your integration token: ").strip()

# Initialize the client
notion = Client(auth=NOTION_TOKEN)


def manual_inputs(parent_id="", db_name="") -> tuple:
    """
    Get values from user input
    """
    if parent_id == "":
        is_page_ok = False
        while not is_page_ok:
            input_text = input("\nEnter the parent page ID or URL: ").strip()
            # Checking if the page exists
            try:
                if input_text[0:4] == "http":
                    parent_id = get_id(input_text)
                    print(f"\nThe ID of the target page is: {parent_id}")
                else:
                    parent_id = input_text
                notion.pages.retrieve(parent_id)