sample2.ipynb 2.84 KB
Newer Older
Andreas's avatar
Andreas committed
1 2 3 4 5 6 7 8 9 10 11
{
 "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
12 13
   "execution_count": 6,
   "metadata": {},
Andreas's avatar
Andreas committed
14 15 16 17
   "outputs": [],
   "source": [
    "from webarchiv import WebarchivSession\n",
    "\n",
Andreas's avatar
Andreas committed
18
    "apikey = '2pm8i0hnmpcTK4Oj4CUeBoZd7vywrm4c'\n",
Andreas's avatar
Andreas committed
19 20 21 22 23 24 25 26 27 28 29 30
    "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
31
   "execution_count": 7,
Andreas's avatar
Andreas committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
   "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
48
   "execution_count": 8,
Andreas's avatar
Andreas committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
   "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
73
   "execution_count": 9,
Andreas's avatar
Andreas committed
74 75 76 77 78 79
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
Andreas's avatar
Andreas committed
80
      "2483\n"
Andreas's avatar
Andreas committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
     ]
    }
   ],
   "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
97 98
   "execution_count": 10,
   "metadata": {},
Andreas's avatar
Andreas committed
99 100 101 102 103 104 105 106 107 108
   "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
109
    "capturedate = response.json()['hits'][0]['c']\n",
Andreas's avatar
Andreas committed
110 111 112 113 114
    "\n",
    "captureurl = 'https://webarchiv.onb.ac.at/web/' + capturedate + '/' + url\n",
    "\n",
    "print (captureurl)"
   ]
onbpre's avatar
onbpre committed
115 116 117 118 119 120 121
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
Andreas's avatar
Andreas committed
122 123 124 125
  }
 ],
 "metadata": {
  "kernelspec": {
Andreas's avatar
Andreas committed
126
   "display_name": "Python 3",
Andreas's avatar
Andreas committed
127
   "language": "python",
Andreas's avatar
Andreas committed
128
   "name": "python3"
Andreas's avatar
Andreas committed
129 130 131 132
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
Andreas's avatar
Andreas committed
133
    "version": 3
Andreas's avatar
Andreas committed
134 135 136 137 138
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
Andreas's avatar
Andreas committed
139 140
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
Andreas's avatar
Andreas committed
141 142 143
  }
 },
 "nbformat": 4,
Andreas's avatar
Andreas committed
144
 "nbformat_minor": 1
Andreas's avatar
Andreas committed
145
}