Skip to content

Instantly share code, notes, and snippets.

@austin-meier
Created July 11, 2022 19:34
Show Gist options
  • Select an option

  • Save austin-meier/8f7cec2425d8bd1745ce56c06a7de40e to your computer and use it in GitHub Desktop.

Select an option

Save austin-meier/8f7cec2425d8bd1745ce56c06a7de40e to your computer and use it in GitHub Desktop.
RemoveTimelines.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "RemoveTimelines.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyNiC3E6EELNMqLsX6WPmzwh",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/austin-meier/8f7cec2425d8bd1745ce56c06a7de40e/removetimelines.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"Click the ![image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB8AAAAgCAYAAADqgqNBAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAFzSURBVFhH7ZYhjoQwFIbfrsSCBY0Fi+YOoLkDnIFDoOEO6LFgkQQsWJC7+UknmYT20WV2BjNfQmibtF9pH3396vv+hy7iW7wv4SO/hEvlf4r22+1GXdfRMAw0juPWZts2OY5DnueR7/tbmy5a8qZpqCxLmudZtMgxTZPiONaexKEc0rquRU2PMAy3SRzB7vkZMUAf9D1CKcdSnxHfQV+MwaGUV1UlSnsQZDocfb1UjqiepknU9mRZRkEQiJoaBCj39VI5ficOwzAoSZLtQZmjbVtR2iOV3//hI/D1aZqy24AzQcVTcoADhtsGbixlwL0DqVw3mgGWNc/zLUhlcGM9JYcQYm5psS0qpHLXdUVJzrIsVBTF9qzrKlrlIOGokMoRPJZlidoebpkfwRhcklEGXBRForRH92/gxgBKOWaM7HQW9D1KrUo5QFo8MwHdlKp9mUCi4c57gD3GUv/bZeIRTAJn9VuvUa+C3fNX85FfwoVyol+Qn6yTybe1bwAAAABJRU5ErkJggg==) button below to install the chilitools python package we will be using to facilitate this process\n",
"\n",
"\n",
"---\n",
"\n",
"\n",
"*Note: If you see this PIP error, you can safely ignore it.*\n",
"\n",
"*ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.*"
],
"metadata": {
"id": "YrEKUOxWG9BJ"
}
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XmFpDOQdGfID",
"outputId": "2dc4bf96-af23-4675-ab10-3811528d9eea"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
"Requirement already satisfied: chilitools in /usr/local/lib/python3.7/dist-packages (0.1.58)\n",
"Collecting chilitools\n",
" Downloading chilitools-0.1.59-py3-none-any.whl (16 kB)\n",
"Requirement already satisfied: pyperclip<2.0.0,>=1.8.2 in /usr/local/lib/python3.7/dist-packages (from chilitools) (1.8.2)\n",
"Requirement already satisfied: requests<3.0.0,>=2.27.1 in /usr/local/lib/python3.7/dist-packages (from chilitools) (2.28.1)\n",
"Requirement already satisfied: xmltodict<0.13.0,>=0.12.0 in /usr/local/lib/python3.7/dist-packages (from chilitools) (0.12.0)\n",
"Requirement already satisfied: coloredlogs<16.0.0,>=15.0.1 in /usr/local/lib/python3.7/dist-packages (from chilitools) (15.0.1)\n",
"Requirement already satisfied: humanfriendly>=9.1 in /usr/local/lib/python3.7/dist-packages (from coloredlogs<16.0.0,>=15.0.1->chilitools) (10.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0,>=2.27.1->chilitools) (3.3)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0,>=2.27.1->chilitools) (2022.6.15)\n",
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0,>=2.27.1->chilitools) (1.26.10)\n",
"Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.7/dist-packages (from requests<3.0.0,>=2.27.1->chilitools) (2.1.0)\n",
"Installing collected packages: chilitools\n",
" Attempting uninstall: chilitools\n",
" Found existing installation: chilitools 0.1.58\n",
" Uninstalling chilitools-0.1.58:\n",
" Successfully uninstalled chilitools-0.1.58\n",
"Successfully installed chilitools-0.1.59\n"
]
}
],
"source": [
"!pip install --upgrade chilitools"
]
},
{
"cell_type": "code",
"source": [
"# The full URL to your Backoffice (ends with interface.aspx)\n",
"# ex: https://ft-nostress.chili-publish-sandbox.online/ft-nostress/interface.aspx\n",
"backofficeURL = 'https://pagedrivebeta.qg.com/Raley_Dev/interface.aspx'\n",
"\n",
"# Credentials for the CHILI BackOffice\n",
"# NOTE: This user requires document permissions (+api permissions). It is easiest to make a temporary user and set them as an environment administrator, then remove after finished.\n",
"ChiliUsername = ''\n",
"ChiliPassword = ''\n",
"\n",
"# List of documents IDs you would like to remove timeline tags from\n",
"documentIDs = [\n",
" '7cf45605-8991-42aa-b777-4d3d68a7e95c',\n",
" '2957bdf3-76aa-487f-af6d-0872456c82f5',\n",
" '000f1f09-491a-48d7-bb02-35608dc13db2',\n",
" 'd10d6489-548d-449a-8d22-7c0f78762c61'\n",
"]\n",
"\n",
"# The suffix added after the document name for the documents with the timeline removed\n",
"# Default: <document_name>_NoTimelineTag\n",
"docSuffix = '_NoTimelineTag'"
],
"metadata": {
"id": "m7k-3P8CHoHA"
},
"execution_count": 33,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from chilitools.api.connector import ChiliConnector\n",
"from chilitools.utilities.xmltools import removeTimelineTags\n",
"\n",
"# Create the CHILI Connector from the chilitools python package \n",
"chili = ChiliConnector(\n",
" backofficeURL=backofficeURL,\n",
" username=ChiliUsername,\n",
" password=ChiliPassword\n",
")\n",
"\n",
"# Get the source XML for a document \n",
"def getDocumentXML(docID: str) -> str:\n",
" return chili.resources.ResourceItemGetXML(\n",
" resourceType='documents',\n",
" itemID=docID\n",
" ).text\n",
"\n",
"# Returns a tuple of the document name, and relative folder path on the BackOffice\n",
"def getDocumentInfo(docID: str) -> tuple:\n",
" resp = chili.resources.ResourceItemGetDefinitionXML(\n",
" resourceType='documents',\n",
" itemID=docID\n",
" )\n",
"\n",
" if resp.success:\n",
" path = resp.content['item']['@relativePath'].split('.xml')[0].split('\\\\')\n",
" return ('/'.join(path[:-1])+'/', path[-1])\n",
" \n",
" return f\"There was a problem getting the path for the document: {resp.text}\"\n",
"\n",
"def processDocument(docID: str) -> tuple:\n",
"\n",
" print(f\"Processing Document: {docID}\")\n",
"\n",
" docInfo = getDocumentInfo(docID)\n",
"\n",
" # Get the document XML and remove the timeline tags\n",
" docXML = removeTimelineTags(getDocumentXML(docID))\n",
"\n",
" # Send request to CHILI to upload the fixed document\n",
" resp = chili.resources.ResourceItemAdd(\n",
" resourceType='documents',\n",
" folderPath=docInfo[0],\n",
" newName=docInfo[1]+docSuffix,\n",
" xml=docXML\n",
" )\n",
"\n",
" if resp.success: \n",
" newDocID = resp.content['item']['@id']\n",
" return (newDocID, True)\n",
" print(f\"There was an issue removing the timeline tags for document: {docID} - {resp.text}\")\n",
" return (docID, False)\n",
"\n",
"#####################\n",
"\n",
"results = list(map(processDocument, documentIDs))\n",
"\n",
"print(results)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bWFjdku2ITEb",
"outputId": "55626d18-cc68-4db1-d7d1-a698d1fd6333"
},
"execution_count": 34,
"outputs": [
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment