From 3711944640aff8536cc10760ec56af33fd51fb93 Mon Sep 17 00:00:00 2001 From: Sean Dockray Date: Mon, 8 Jun 2020 23:02:09 +1000 Subject: [PATCH] handling some more block types --- custom_syadmin/build_from_notion.py | 32 ++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/custom_syadmin/build_from_notion.py b/custom_syadmin/build_from_notion.py index dd11491..8628e54 100644 --- a/custom_syadmin/build_from_notion.py +++ b/custom_syadmin/build_from_notion.py @@ -5,6 +5,7 @@ import shutil import subprocess import re import stat +from urllib.parse import urlparse, parse_qs from notion.client import NotionClient from notion.markdown import notion_to_markdown @@ -13,7 +14,7 @@ from notion.markdown import notion_to_markdown # Set the variables we'll be allowing for (as CL arg or environment variable) vars = { "syllabus_db": "https://www.notion.so/memoryspace/594e40c85f2844b5911f14e7db21850f?v=2a2e8a86e8a74d29844f5eef41d18b3f", - "syllabus_title": "Pirate Care", + "syllabus_title": "Machine Listening", "token_v2": "Find the value of 'token_v2' in your browser cookies when logged into Notion", "hugo_path": "hugo", "hugo_site_path": "/mnt/d/dev/websites/arche-syllabus-test", # The Hugo site @@ -94,6 +95,25 @@ def hugo(hugo_site_path, dest, hugo_environment='gitea', base_url=None): hugo_output = cmd([HUGO_PATH, '-e', hugo_environment, '-d', dest, '--noTimes'], cwd=hugo_site_path, env=env) +def hugo_video_shortcode(url): + """ Returns the Hugo shortcode for a YouTube or Vimeo url """ + if "youtube.com" in url: + try: + url_data = urlparse(url) + query = parse_qs(url_data.query) + video = query["v"][0] + return "{{< youtube " + video + " >}}" + except: + pass + elif "vimeo.com" in url: + try: + video = urlparse(url).path.lstrip("/") + return "{{< vimeo " + video + " >}}" + except: + pass + return f"[{url}]({url})" + + def get_record_text(post, level=0): """ Generates the markdown text of a Notion page see: # https://github.com/brentbaum/brentbaum-notion-publishing/blob/master/notion/get_posts.py """ @@ -109,6 +129,12 @@ def get_record_text(post, level=0): text += f"![{caption}]({child.display_source})\n" else: text += f"![{caption}]({child.source})\n" + elif child.type == "video": + caption = f"\n_{child.caption}_" if child.caption else "" + text += f"{hugo_video_shortcode(child.source)}{caption}\n\n" + elif child.type == "bookmark": + caption = f"\n_{child.caption}_" if child.caption else "" + text += f"[{child.title}]({child.link}){caption}\n\n" elif child.type == "divider": text += "---" elif child.type == "bulleted_list": @@ -119,7 +145,7 @@ def get_record_text(post, level=0): else: prefix = prefixes.get(child.type, "") text += prefix + child.title.encode('utf-8').decode('utf-8') + "\n\n" - + print(text) text = re.sub('`(bib|session|topic):([a-zA-Z0-9-]+)`', r'![](\1:\2)', text) return text @@ -190,7 +216,7 @@ def notion_to_md(id, filepath=None, yaml_func=None): # print(page_data) if filepath: os.umask(0) - with open(os.open(filepath, os.O_CREAT | os.O_WRONLY, 0o777), 'w', encoding="utf-8") as f: + with open(os.open(filepath, os.O_CREAT | os.O_WRONLY, 0o777), 'w') as f: f.write(page_content) return page_data