Skip to content
sample2.ipynb 2.84 KiB
Newer Older
onbpre's avatar
onbpre committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a WebarchivSession Object with convenience methods for easy access with your API-Key "
   ]
  },
  {
   "cell_type": "code",
Andreas's avatar
Andreas committed
   "execution_count": 6,
   "metadata": {},
onbpre's avatar
onbpre committed
   "outputs": [],
   "source": [
    "from webarchiv import WebarchivSession\n",
    "\n",
Andreas's avatar
Andreas committed
    "apikey = '2pm8i0hnmpcTK4Oj4CUeBoZd7vywrm4c'\n",
onbpre's avatar
onbpre committed
    "w = WebarchivSession(apikey)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Submit a URL search to get all archived capturedates of the requested URL"
   ]
  },
  {
   "cell_type": "code",
Andreas's avatar
Andreas committed
   "execution_count": 7,
onbpre's avatar
onbpre committed
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"http://www.onb.ac.at\"\n",
    "response = w.wayback_search(url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The search always returns the full response. Checking for status_code 200 before extracting the response is always a good idea"
   ]
  },
  {
   "cell_type": "code",
Andreas's avatar
Andreas committed
   "execution_count": 8,
onbpre's avatar
onbpre committed
   "metadata": {},
   "outputs": [],
   "source": [
    "if response.status_code != 200:\n",
    "    print(\"Something went wrong ...\")\n",
    "    exit(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now it is safe to extract the response\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the total number of captures of the requested URL"
   ]
  },
  {
   "cell_type": "code",
Andreas's avatar
Andreas committed
   "execution_count": 9,
onbpre's avatar
onbpre committed
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Andreas's avatar
Andreas committed
      "2483\n"
onbpre's avatar
onbpre committed
     ]
    }
   ],
   "source": [
    "print(response.json()['total'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get the Archiveurl of the oldest Capuredate of the requested URL"
   ]
  },
  {
   "cell_type": "code",
Andreas's avatar
Andreas committed
   "execution_count": 10,
   "metadata": {},
onbpre's avatar
onbpre committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://webarchiv.onb.ac.at/web/20090916183601/http://www.onb.ac.at\n"
     ]
    }
   ],
   "source": [
Andreas's avatar
Andreas committed
    "capturedate = response.json()['hits'][0]['c']\n",
onbpre's avatar
onbpre committed
    "\n",
    "captureurl = 'https://webarchiv.onb.ac.at/web/' + capturedate + '/' + url\n",
    "\n",
    "print (captureurl)"
   ]
onbpre's avatar
onbpre committed
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
onbpre's avatar
onbpre committed
  }
 ],
 "metadata": {
  "kernelspec": {
Andreas's avatar
Andreas committed
   "display_name": "Python 3",
onbpre's avatar
onbpre committed
   "language": "python",
Andreas's avatar
Andreas committed
   "name": "python3"
onbpre's avatar
onbpre committed
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
Andreas's avatar
Andreas committed
    "version": 3
onbpre's avatar
onbpre committed
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
Andreas's avatar
Andreas committed
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
onbpre's avatar
onbpre committed
  }
 },
 "nbformat": 4,
Andreas's avatar
Andreas committed
 "nbformat_minor": 1
onbpre's avatar
onbpre committed
}