5 "execution_count": null,
14 "execution_count": null,
18 "def get_article_revisions(title):\n",
21 " # create a base url for the api and then a normal url which is initially\n",
22 " # just a copy of it\n",
23 " # The following line is what the requests call is doing, basically.\n",
24 " # \"http://en.wikipedia.org/w/api.php/?action=query&titles={0}&prop=revisions&rvprop=flags|timestamp|user|size|ids&rvlimit=500&format=json&continue=\".format(title)\n",
25 " wp_api_url = \"http://en.wikipedia.org/w/api.php/\"\n",
27 " parameters = {'action' : 'query',\n",
28 " 'titles' : title,\n",
29 " 'prop' : 'revisions',\n",
30 " 'rvprop' : 'flags|timestamp|user|size|ids',\n",
31 " 'rvlimit' : 500,\n",
32 " 'format' : 'json',\n",
33 " 'continue' : '' }\n",
35 " # we'll repeat this forever (i.e., we'll only stop when we find\n",
36 " # the \"break\" command)\n",
38 " # the first line open the urls but also handles unicode urls\n",
39 " call = requests.get(wp_api_url, params=parameters)\n",
40 " api_answer = call.json()\n",
42 " # get the list of pages from the json object\n",
43 " pages = api_answer[\"query\"][\"pages\"]\n",
45 " # for every page, (there should always be only one) get its revisions:\n",
46 " for page in pages.keys():\n",
47 " query_revisions = pages[page][\"revisions\"]\n",
49 " # for every revision, first we do some cleaning up\n",
50 " for rev in query_revisions:\n",
52 " # let's continue/skip this revision if the user is hidden\n",
53 " if \"userhidden\" in rev:\n",
56 " # 1: add a title field for the article because we're going to mix them together\n",
57 " rev[\"title\"] = title\n",
59 " # 2: let's \"recode\" anon so it's true or false instead of present/missing\n",
60 " if \"anon\" in rev:\n",
61 " rev[\"anon\"] = True\n",
63 " rev[\"anon\"] = False\n",
65 " # 3: let's recode \"minor\" in the same way\n",
66 " if \"minor\" in rev:\n",
67 " rev[\"minor\"] = True\n",
69 " rev[\"minor\"] = False\n",
71 " # we're going to change the timestamp to make it work a little better in excel/spreadsheets\n",
72 " rev[\"timestamp\"] = rev[\"timestamp\"].replace(\"T\", \" \")\n",
73 " rev[\"timestamp\"] = rev[\"timestamp\"].replace(\"Z\", \"\")\n",
75 " # finally, save the revisions we've seen to a varaible\n",
76 " revisions.append(rev)\n",
78 " # 'continue' tells us there's more revisions to add\n",
79 " if 'continue' in api_answer:\n",
80 " # replace the 'continue' parameter with the contents of the\n",
81 " # api_answer dictionary.\n",
82 " parameters.update(api_answer['continue'])\n",
86 " # return all the revisions for this page\n",
87 " return(revisions)\n"
92 "execution_count": null,
96 "category = \"Harry Potter\"\n",
98 "# we'll use another api called catscan2 to grab a list of pages in\n",
99 "# categories and subcategories. it works like all the other apis we've\n",
102 "# The following requests call basically does the same thing as this string:\n",
103 "# \"http://tools.wmflabs.org/catscan2/catscan2.php?depth=10&categories={0}&doit=1&format=json\".format(category)\n",
104 "url_catscan = \"https://petscan.wmflabs.org/\"\n",
106 "parameters = {'depth' : 10,\n",
107 " 'categories' : category,\n",
108 " 'format' : 'json',\n",
111 "# r = requests.get(\"http://tools.wmflabs.org/catscan2/catscan2.php?depth=10&categories=Harry Potter&doit=1&format=json\"\n"
116 "execution_count": null,
120 "r = requests.get(url_catscan, params=parameters)"
125 "execution_count": null,
129 "articles_json = r.json()\n",
130 "articles = articles_json[\"*\"][0][\"a\"][\"*\"]"
135 "execution_count": null,
139 "# open a file to print the header\n",
140 "output_file = open(\"hp_wiki.tsv\", \"w\", encoding='utf-8')\n",
141 "print(\"\\t\".join([\"title\", \"user\", \"timestamp\", \"size\", \"anon\", \"minor\", \"revid\"]), file=output_file)\n"
146 "execution_count": null,
150 "# for every article\n",
151 "for article in articles[0:10]:\n",
152 " # skip this until it's an article\n",
153 " if article[\"namespace\"] != 0:\n",
156 " # first grab the article's title\n",
157 " title = article[\"title\"]\n",
160 " # get the list of revisions from our function and then iterate through it,\n",
161 " # printing it to our output file\n",
162 " revisions = get_article_revisions(title)\n",
163 " for rev in revisions:\n",
164 " print(\"\\t\".join([rev[\"title\"], rev[\"user\"], rev[\"timestamp\"],\n",
165 " str(rev[\"size\"]), str(rev[\"anon\"]),\n",
166 " str(rev[\"minor\"]), str(rev[\"revid\"])]),\n",
172 "execution_count": null,
176 "# close the file, we're done here!\n",
177 "output_file.close()"
182 "execution_count": null,
190 "display_name": "Python 3",
191 "language": "python",
199 "file_extension": ".py",
200 "mimetype": "text/x-python",
202 "nbconvert_exporter": "python",
203 "pygments_lexer": "ipython3",