in bots/sdlc-slackbot/sdlc_slackbot/gdoc.py [0:0]
def gdoc_get(gdoc_url):
# https://docs.google.com/document/d/<ID>/edit
result = None
logger.info(gdoc_url)
if not gdoc_url.startswith("https://docs.google.com/document") and not gdoc_url.startswith(
"docs.google.com/document"
):
logger.error("Invalid google doc url")
return result
# This regex captures the ID after "/d/" and before an optional "/edit", "/" or the end of the string.
pattern = r"/d/([^/]+)"
match = re.search(pattern, gdoc_url)
if match:
document_id = match.group(1)
logger.info(document_id)
else:
logger.error("No ID found in the URL")
return result
creds = gdoc_creds()
try:
service = build("docs", "v1", credentials=creds)
# Retrieve the documents contents from the Docs service.
document = service.documents().get(documentId=document_id).execute()
logger.info("The title of the document is: {}".format(document.get("title")))
doc_content = document.get("body").get("content")
result = read_structural_elements(doc_content)
except HttpError as err:
logger.error(err)
return result