{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Change an ATOM entry on the catalog\n",
"\n",
"In this scenario, we download an ATOM entry that exists on the catalog, change it locally and and re-upload the changed entry. This can be useful when an automatically generated entry (see *post-atom.ipynb*) needs to be amended.\n",
"\n",
"## 1. Set the necessary variables\n",
"\n",
"The following section defines all the necessary information as variables so the code below can be easily reused."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import getpass\n",
"\n",
"# Set the credentials (Ellip username and API key)\n",
"username = raw_input(\"What is your Ellip username? \")\n",
"api_key = getpass.getpass(\"What is your Ellip API key? \")\n",
"\n",
"# Set the name of the destination index on the Terradue catalog\n",
"index_name = raw_input(\"What is the destination index name? (press Enter to confirm default [{0}]) \".format(username))\n",
"\n",
"if not index_name:\n",
" index_name = username\n",
"\n",
"# Set the catalog endpoint URL\n",
"endpoint = \"https://catalog.terradue.com/{0}\".format(index_name)\n",
"\n",
"# Set the identifier of the entry to be changed\n",
"uid = 'data-publication-sample'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Define a class for ATOM manipulation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import lxml.etree as etree\n",
"import sys\n",
"import os\n",
"import string\n",
"import hashlib\n",
"import urllib2\n",
"import base64\n",
"import time\n",
"\n",
"class Atom:\n",
" tree = None\n",
" root = None\n",
" entry = None\n",
" \n",
" def __init__(self, root):\n",
" self.root = root\n",
" self.tree = root\n",
" self.links = self.root.xpath('/a:feed/a:entry/a:link', namespaces={'a':'http://www.w3.org/2005/Atom'})\n",
" entries = self.root.xpath('/a:feed/a:entry', namespaces={'a':'http://www.w3.org/2005/Atom'})\n",
" if len(entries) > 0:\n",
" self.entry = entries[0]\n",
" \n",
" @staticmethod\n",
" def from_template():\n",
" template = \"\"\"\n",
"
%s
\" % text\n", "\n", " self.set_summary_text(html_summary)\n", "\n", "\n", " def to_string(self, pretty_print = True):\n", " \n", " return etree.tostring(self.tree, pretty_print=pretty_print)\n", " \n", " def clear_enclosures(self):\n", " \n", " links = self.get_links(\"enclosure\")\n", " for link in links:\n", " link.getparent().remove(link) \n", " \n", " def get_extensions(self, name, namespace):\n", " \n", " return self.root.xpath('/a:feed/a:entry/e:{0}'.format(name), \n", " namespaces={'a':'http://www.w3.org/2005/Atom',\n", " 'e':namespace})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Load the ATOM feed of a product and change its entry\n", "\n", "We download the feed containing one entry and change the title of the entry." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import datetime\n", "\n", "atom = Atom.load(\"{0}/search?q={1}\".format(endpoint, uid), username, api_key);\n", "\n", "\n", "\n", "# Change a property (the title)\n", "atom.set_title_text(\"---- CHANGED ----- Title for data-publication-sample\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Post ATOM feed\n", "\n", "We post the ATOM feed back to the same index on the catalog." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "request = requests.post(endpoint,\n", " data=atom.to_string(),\n", " headers={\"Content-Type\": \"application/atom+xml\", \"Accept\": \"application/xml\"},\n", " auth=(username, api_key))\n", "\n", "if request.status_code == 200:\n", " print('Data item updated at {0}/search?uid={1}&apikey={2} ({3})'.format(endpoint, atom.get_identifier(), api_key, str(request.status_code)))\n", "else:\n", " print('Data item NOT updated at {0}/search?uid={1}&apikey={2} ({3})'.format(endpoint, atom.get_identifier(), api_key, str(request.status_code)))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If a product URL with status 200 is displayed, the ATOM feed has been successfully updated and the title has been changed.\n", "\n", "**END**" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 2 }