{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Comparing CDX APIs\n", "\n", "

New to Jupyter notebooks? Try Using Jupyter notebooks for a quick introduction.

\n", "\n", "This notebook documents differences between the Internet Archive's CDX API and the CDX API available from PyWb systems such as the UK Web Archive and the National Library of Australia.\n", "\n", "For more details on the data available from the CDX APIs see [Exploring the Internet Archive's CDX API](exploring_cdx_api.ipynb).\n", "\n", "For examples using CDX APIs to harvest capture data see:\n", "\n", "* [Find all the archived versions of a web page](find_all_captures.ipynb)\n", "* [Harvesting data about a domain using the IA CDX API](harvesting_domain_data.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Documentation\n", "\n", "* [Wayback CDX API](https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server)\n", "* [PyWb CDXJ Server API](https://pywb.readthedocs.io/en/latest/manual/cdxserver_api.html)\n", "* [PyWb indexes](https://pywb.readthedocs.io/en/latest/manual/indexing.html)\n" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json\n", "import re\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [], "source": [ "APIS = {\n", " 'ia': {'url': 'http://web.archive.org/cdx/search/cdx', 'type': 'wb'},\n", " 'nla': {'url': 'https://web.archive.org.au/awa/cdx', 'type': 'pywb'},\n", " 'bl': {'url': 'https://www.webarchive.org.uk/wayback/archive/cdx', 'type': 'pywb'}\n", "}\n", "\n", "def raw_cdx_query(api, url, **kwargs):\n", " params = kwargs\n", " params['url'] = url\n", " params['output'] = 'json'\n", " response = requests.get(APIS[api]['url'], params=params)\n", " return response" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Differences between PyWb and IA Wayback" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### JSON results format\n", "\n", "As with Timemaps, requesting `json` formatted results from IA and Pywb CDX servers returns different data structures. IA results are an array of arrays, with the field labels in the first array. Pywb results are formatted as NDJSON (Newline Delineated JSON) – each capture is a JSON object, separated by a line break." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Internet Archive (Wayback)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['urlkey',\n", " 'timestamp',\n", " 'original',\n", " 'mimetype',\n", " 'statuscode',\n", " 'digest',\n", " 'length'],\n", " ['au,com,discontents)/',\n", " '19981206012233',\n", " 'http://www.discontents.com.au:80/',\n", " 'text/html',\n", " '200',\n", " 'FQJ6JMPIZ7WEKYPQ4SGPVHF57GCV6B36',\n", " '1610']]" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw_cdx_query('ia', 'discontents.com.au', limit=1, format='json').json()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'urlkey': 'au,com,discontents)/',\n", " 'timestamp': '19981206012233',\n", " 'url': 'http://www.discontents.com.au/',\n", " 'mime': 'text/html',\n", " 'status': '200',\n", " 'digest': 'FQJ6JMPIZ7WEKYPQ4SGPVHF57GCV6B36',\n", " 'offset': '59442416',\n", " 'filename': 'NLA-EXTRACTION-1996-2004-ARCS-PART-00309-000001.arc.gz',\n", " 'source': 'awa',\n", " 'source-coll': 'awa'}" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.loads(raw_cdx_query('nla', 'discontents.com.au', limit=1, format='json').text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Field labels\n", "\n", "As with Timemaps, some of the field labels are different between the two systems:\n", "\n", "|IA|PyWb|\n", "|---|---|\n", "|`original`|`url`|\n", "|`statuscode`|`status`|\n", "|`mimetype`|`mime`|" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Internet Archive (Wayback)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['urlkey',\n", " 'timestamp',\n", " 'original',\n", " 'mimetype',\n", " 'statuscode',\n", " 'digest',\n", " 'length']" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "raw_cdx_query('ia', 'discontents.com.au', limit=1, format='json').json()[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['urlkey',\n", " 'timestamp',\n", " 'url',\n", " 'mime',\n", " 'status',\n", " 'digest',\n", " 'offset',\n", " 'filename',\n", " 'source',\n", " 'source-coll']" ] }, "execution_count": 111, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(json.loads(raw_cdx_query('nla', 'discontents.com.au', limit=1, format='json').text).keys())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Match types\n", "\n", "From the documentation it seems that you should be able to supply a `matchType` or use url wildcards on both systems. But there seem to be some inconsistences. In summary:\n", "\n", "* UKWA needs **both** the url wildcard and the `matchType` parameter to work correctly\n", "* domain queries do not work with NLA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)\n", "\n", "Prefix queries work as expected, Domain queries do not work." ] }, { "cell_type": "code", "execution_count": 179, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "472" ] }, "execution_count": 179, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Look for an exact url\n", "len(raw_cdx_query('nla', 'http://nla.gov.au/', filter='status:200', format='json').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "19936" ] }, "execution_count": 181, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Prefix query using url wildcard works as expected\n", "len(raw_cdx_query('nla', 'http://nla.gov.au/*', filter='status:200', format='json').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "19936" ] }, "execution_count": 210, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Prefix query using matchType=prefix works as expected\n", "len(raw_cdx_query('nla', 'http://nla.gov.au/', filter='status:200', format='json', matchType='prefix').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['{\"message\": \"Internal Error: tuple index out of range\"}']" ] }, "execution_count": 184, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Domain query using matchType parameter causes error\n", "raw_cdx_query('nla', 'nla.gov.au', filter='status:200', format='json', matchType='domain').text.splitlines()" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['{\"message\": \"Internal Error: tuple index out of range\"}']" ] }, "execution_count": 211, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Domain query using url wildcard causes error\n", "raw_cdx_query('nla', '*.nla.gov.au', filter='status:200', format='json').text.splitlines()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### UKWA (PyWb)\n", "\n", "Domain and prefix queries need **both** the `matchType` parameter and a url wildcard." ] }, { "cell_type": "code", "execution_count": 212, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 212, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Look for an exact url\n", "len(raw_cdx_query('bl', 'anjackson.net', filter='status:200', format='json').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 213, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 213, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Domain query using url wildcard has no effect\n", "len(raw_cdx_query('bl', '*.anjackson.net', filter='status:200', format='json').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 214, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 214, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Domain query using matchType parameter has no effect\n", "len(raw_cdx_query('bl', 'anjackson.net', filter='status:200', format='json', matchType='domain').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 215, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "31045" ] }, "execution_count": 215, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Domain query using *both* matchType parameter and url wildcard performs domain search\n", "len(raw_cdx_query('bl', '*.anjackson.net', filter='status:200', format='json', matchType='domain').text.splitlines())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Collapse\n", "\n", "PyWb doesn't support the `collapse` parameter. So if you want to remove duplicates, you'll need to use something like Pandas `.drop_duplicates()` after the results have arrived. However, `collapse` only works on adjacent index entries, so if only having unique values is important, you'll probably want to run `.drop_duplicates()` on it anyway," ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Internet Archive (Wayback)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "311" ] }, "execution_count": 120, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Without collapse -- total number of results (subtract one for the header row)\n", "len(raw_cdx_query('ia', 'discontents.com.au', format='json').json()) - 1" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# With collapse -- should only be one result as we're collapsing on urlkey and searching for an exact url\n", "len(raw_cdx_query('ia', 'discontents.com.au', format='json', collapse='urlkey').json()) - 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 117, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "142" ] }, "execution_count": 117, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Without collapse\n", "len(raw_cdx_query('nla', 'discontents.com.au', format='json').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "142" ] }, "execution_count": 118, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# With collapse\n", "len(raw_cdx_query('nla', 'discontents.com.au', collapse='urlkey', format='json').text.splitlines())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "De-duplicate results using Pandas." ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = [json.loads(line) for line in raw_cdx_query('nla', 'discontents.com.au', fields='urlkey', format='json').text.splitlines()]\n", "df = pd.DataFrame(data).drop_duplicates(subset=['urlkey'])\n", "df.shape[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Sort and Closest\n", "\n", "IA doesn't support `sort` or the `closest` parameter. To implement something similar, I suppose you could use `from` and `to` to set a window around a date, and then process the results to calculate time deltas and sort by 'closeness'." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Limiting fields\n", "\n", "The parameter used for limiting the fields returned from a query is different. The IA server expects `fl`, while PyWb uses `fields` (the PyWb documentation says `fl`, but it doesn't work).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'urlkey': 'au,com,discontents)/',\n", " 'timestamp': '19981206012233',\n", " 'url': 'http://www.discontents.com.au/',\n", " 'mime': 'text/html',\n", " 'status': '200',\n", " 'digest': 'FQJ6JMPIZ7WEKYPQ4SGPVHF57GCV6B36',\n", " 'offset': '59442416',\n", " 'filename': 'NLA-EXTRACTION-1996-2004-ARCS-PART-00309-000001.arc.gz',\n", " 'source': 'awa',\n", " 'source-coll': 'awa'}" ] }, "execution_count": 112, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.loads(raw_cdx_query('nla', 'discontents.com.au', limit=1, fl='urlkey', format='json').text)" ] }, { "cell_type": "code", "execution_count": 113, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'urlkey': 'au,com,discontents)/'}" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.loads(raw_cdx_query('nla', 'discontents.com.au', limit=1, fields='urlkey', format='json').text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Comparison operators in filters\n", "\n", "This seems to create the most potential for confusion. In PyWb, the `filter` parameter uses a number of different operators to indicate the type of match required. IA only uses `!`. There's no way of indicating a query should be treated as a regular expression in IA, therefore, all queries are treated as regular expressions.\n", "\n", "| Operator | Example | Result |\n", "|---|---|---|\n", "| no operator | `filter=mime:html` | `mime` field contains 'html'|\n", "| `=` | `filter==mime:text/html` | `mime` field matches 'text/html' exactly |\n", "| `~` | `filter=~status:30\\d{1}` | `status` field matches any 3 digit code starting with 30|\n", "| `!` | `filter=!mime:html` | `mime` field doesn't contain 'html' |\n", "| `!=` | `filter=!=mime:text/html` | `mime` field doesn't match 'text/html' exactly |\n", "| `!~` | `filter=!~status:30\\d{1}` | `status` field doesn't match any 3 digit codes starting with 30 |\n", "\n", "IA filter queries look for an exact match (which could be a reguklar expression) by default. This can be negated by using the `!` operator.\n", "\n", "| Operator | Example | Result |\n", "|---|---|---|\n", "| no operator | `filter=mimetype:text/html` | `mimetype` field matches 'text/html'|\n", "| `!` | `filter=!mimetype:text/html` | `mimetype` field doesn't match 'text/html' exactly |\n", "\n", "In IA you need to use a regular expression to find a field containing a particular value. So these two expressions should result in the same matching behaviour:\n", "\n", "| PyWb | IA |\n", "|---|---|\n", "|`filter=mime:powerpoint`|`filter=mimetype:.*powerpoint.*`|\n", "\n", "For interoperability, it seems easiest to always use regular expressions, inserting the `~` operator for PyWb systems. So: \n", "\n", "| PyWb | IA |\n", "|---|---|\n", "|`filter=~mime:.*powerpoint.*`|`filter=mimetype:.*powerpoint.*`|\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Internet Archive (Wayback)" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(raw_cdx_query('ia', 'defence.gov.au/*', filter='mimetype:.*powerpoint.*', format='json', collapse='urlkey').json()) - 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "177" ] }, "execution_count": 147, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(raw_cdx_query('nla', 'defence.gov.au/*', filter='mime:powerpoint', format='json').text.splitlines())" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "177" ] }, "execution_count": 149, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(raw_cdx_query('nla', 'defence.gov.au/*', filter='~mime:.*powerpoint.*', format='json').text.splitlines())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Pagination\n", "\n", "Both IA and PyWb *can* support pagination or results, however, it's not available by default in PyWb. It's only available if repositories are [using ZipNum indexes](https://pywb.readthedocs.io/en/latest/manual/cdxserver_api.html#pagination-api). Neither the UKWA or National Library of Australia CDX APIs support pagination. This means that queries to these systems will return **all** matching results in one hit (unless there is a system defined limit). This is something to bear in mind as large requests might be slow and prone to breakage." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Internet Archive (Wayback)" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 135, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(raw_cdx_query('ia', 'discontents.com.au', showNumPages='true', format='json').text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'urlkey': 'au,com,discontents)/',\n", " 'timestamp': '19981206012233',\n", " 'url': 'http://www.discontents.com.au/',\n", " 'mime': 'text/html',\n", " 'status': '200',\n", " 'digest': 'FQJ6JMPIZ7WEKYPQ4SGPVHF57GCV6B36',\n", " 'offset': '59442416',\n", " 'filename': 'NLA-EXTRACTION-1996-2004-ARCS-PART-00309-000001.arc.gz',\n", " 'source': 'awa',\n", " 'source-coll': 'awa'}" ] }, "execution_count": 137, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# NLA CDX server just ignores the showNumPages parameter and performs the query as normal\n", "json.loads(raw_cdx_query('nla', 'discontents.com.au', showNumPages='true', format='json').text.splitlines()[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "### Fuzzy matching\n", "\n", "If your query to a PyWb CDX API returns no matches, the system will use regular expressions to broaden your search and return a set of 'fuzzy' matches. These results will include an `is_fuzzy` field set to a value of `1`. This is not supported in IA.\n", "\n", "While fuzzy matching is useful for discovery, it might not be what you want if you're assembling a specific dataset. In this case you'd need to filter the results to remove the `is_fuzzy` matches." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Internet Archive (Wayback)" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 132, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This should return no results\n", "raw_cdx_query('ia', 'discontents.com.au', limit=1, filter='statuscode:666', format='json').json()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'urlkey': 'au,com,discontents)/',\n", " 'timestamp': '19981206012233',\n", " 'url': 'http://www.discontents.com.au/',\n", " 'mime': 'text/html',\n", " 'status': '200',\n", " 'digest': 'FQJ6JMPIZ7WEKYPQ4SGPVHF57GCV6B36',\n", " 'offset': '59442416',\n", " 'filename': 'NLA-EXTRACTION-1996-2004-ARCS-PART-00309-000001.arc.gz',\n", " 'source': 'awa',\n", " 'source-coll': 'awa',\n", " 'is_fuzzy': '1'}" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This would return no results except for fuzzy matching\n", "# Note the status value in the result and the 'is_fuzzy' field\n", "json.loads(raw_cdx_query('nla', 'discontents.com.au', limit=1, filter='status:666', format='json').text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Normalising queries\n", "\n", "It would be possible to wrap some code around queries that simulated `collapse` and `closest` across the two systems, but for the moment I'll just focus on some basic normalisation of query parameters and results. The functions below:\n", "\n", "* Normalise field names in queries and results\n", "* Convert results into a list of dictionaries" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [], "source": [ "def normalise_filter(api, f):\n", " sys_type = APIS[api]['type']\n", " if sys_type == 'pywb':\n", " f = f.replace('mimetype:', 'mime:')\n", " f = f.replace('statuscode:', 'status:')\n", " f = f.replace('original:', 'url:')\n", " f = re.sub(r'^(!{0,1})(\\w)', r'\\1~\\2', f)\n", " elif sys_type == 'wb':\n", " f = f.replace('mime:', 'mimetype:')\n", " f = f.replace('status:', 'statuscode:')\n", " f = f.replace('url:', 'original:')\n", " return f\n", "\n", "def normalise_filters(api, filters):\n", " if isinstance(filters, list):\n", " normalised = []\n", " for f in filters:\n", " normalised.append(normalise_filter(api, f))\n", " else:\n", " normalised = normalise_filter(api, filters)\n", " return normalised\n", "\n", "def convert_lists_to_dicts(results):\n", " '''\n", " Converts IA style timemap (a JSON array of arrays) to a list of dictionaries.\n", " Renames keys to standardise IA with other Timemaps.\n", " '''\n", " if results:\n", " keys = results[0]\n", " results_as_dicts = [dict(zip(keys, v)) for v in results[1:]]\n", " else:\n", " results_as_dicts = results\n", " for d in results_as_dicts:\n", " d['status'] = d.pop('statuscode')\n", " d['mime'] = d.pop('mimetype')\n", " d['url'] = d.pop('original')\n", " return results_as_dicts\n", "\n", "def query_cdx(api, url, **kwargs):\n", " params = kwargs\n", " if 'filter' in params:\n", " params['filter'] = normalise_filters(api, params['filter'])\n", " params['url'] = url\n", " params['output'] = 'json'\n", " response = requests.get(APIS[api]['url'], params=params)\n", " print(response.url)\n", " response.raise_for_status()\n", " response_type = response.headers['content-type'].split(';')[0]\n", " print(response_type)\n", " if response_type == 'text/x-ndjson':\n", " data = [json.loads(line) for line in response.text.splitlines()]\n", " elif response_type == 'application/json':\n", " data = convert_lists_to_dicts(response.json())\n", " return data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's some examples – note that the parameters and their values are unchanged, you can just switch repositories." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Internet Archive (Wayback)" ] }, { "cell_type": "code", "execution_count": 157, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "http://web.archive.org/cdx/search/cdx?filter=mimetype%3A.%2Apdf&filter=statuscode%3A200&limit=1&url=defence.gov.au%2F%2A&output=json\n", "application/json\n" ] }, { "data": { "text/plain": [ "[{'urlkey': 'au,gov,defence)/28sqn/ad097.pdf',\n", " 'timestamp': '20140304175138',\n", " 'digest': 'AQBSAVSJJYOYKKLW7GM36PDCYDREFQXA',\n", " 'length': '141731',\n", " 'status': '200',\n", " 'mime': 'application/pdf',\n", " 'url': 'http://www.defence.gov.au/28sqn/AD097.pdf'}]" ] }, "execution_count": 157, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query_cdx('ia', 'defence.gov.au/*', filter=['mimetype:.*pdf', 'status:200'], limit=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### NLA (PyWb)" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "https://web.archive.org.au/awa/cdx?filter=~mime%3A.%2Apdf&filter=~status%3A200&matchType=prefix&limit=1&url=defence.gov.au&output=json\n", "text/x-ndjson\n" ] }, { "data": { "text/plain": [ "[{'urlkey': 'au,gov,defence)/28sqn/ad097.pdf',\n", " 'timestamp': '20140304175138',\n", " 'url': 'http://www.defence.gov.au/28sqn/AD097.pdf',\n", " 'mime': 'application/pdf',\n", " 'status': '200',\n", " 'digest': 'AQBSAVSJJYOYKKLW7GM36PDCYDREFQXA',\n", " 'offset': '398397913',\n", " 'filename': 'NLA-GOV-CRAWL-02-05-2014-20140304154236912-01070-27401~wbgrp-crawl010.us.archive.org~8443.warc.gz',\n", " 'source': 'awa',\n", " 'source-coll': 'awa'}]" ] }, "execution_count": 209, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query_cdx('nla', 'defence.gov.au', filter=['mimetype:.*pdf', 'status:200'], matchType='prefix', limit=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----\n", "Created by [Tim Sherratt](https://timsherratt.org) for the [GLAM Workbench](https://glam-workbench.github.io).\n", "\n", "Work on this notebook was supported by the [IIPC Discretionary Funding Programme 2019-2020](http://netpreserve.org/projects/)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }