{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#default_exp event" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The events API\n", "\n", "> Helpers for getting GitHub API events" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from fastcore.utils import *\n", "from fastcore.foundation import *\n", "from fastcore.meta import *\n", "from ghapi.core import *\n", "from ghapi.page import *\n", "from ghapi.actions import *\n", "\n", "import time,json,gzip\n", "from itertools import islice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#hide\n", "from nbdev.showdoc import show_doc\n", "from IPython.display import Markdown\n", "import matplotlib.pyplot as plt\n", "from collections import Counter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "api = GhApi()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _list_events(g, username=None, org=None, owner=None, repo=None):\n", " if (username or org or owner) and \\\n", " not (bool(username) ^ bool(org) ^ bool(owner)): raise Exception('Can not pass more than one of username, org, and owner')\n", " if (owner and not repo): owner,repo = repo.split('/')\n", " if owner: return g.list_public_events_for_repo_network,{'owner':owner,'repo':repo}\n", " if org: return g.list_public_org_events,{'org':org}\n", " if username: return g.list_public_events_for_user,{'username':username}\n", " return g.list_public_events,{}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "def _id2int(x):\n", " x.id = int(x.id)\n", " return x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "@patch\n", "@delegates(_list_events)\n", "def list_events(self:GhApi, per_page=30, page=1, **kwargs):\n", " \"Fetch public events for repo network, org, user, or all\"\n", " oper,kw = _list_events(self.activity, **kwargs)\n", " return oper(per_page=per_page, page=page, **kw).map(_id2int)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "@patch\n", "@delegates(_list_events)\n", "def list_events_parallel(self:GhApi, per_page=30, n_pages=8, **kwargs):\n", " \"Fetch as many events from `list_events` in parallel as available\"\n", " oper,kw = _list_events(self.activity, **kwargs)\n", " return pages(oper, n_pages, per_page=per_page, **kw).concat().map(_id2int)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`list_events` and `list_events_parallel` support the following:\n", "\n", "|Events from|Example|\n", "|:-|:-|\n", "|Organization|`api.list_events_parallel(org='fastai')`\n", "|User|`api.list_events_parallel(username='jph00')`\n", "|Repository network|`api.list_events_parallel(owner='fastai', repo='fastcore')`\n", "|All public|`api.list_events_parallel()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "_bot_re = re.compile('b[o0]t')\n", "def _want_evt(o, types, incl_bot):\n", " if not incl_bot and _bot_re.search(nested_attr(o, 'actor.login') or ''): return False\n", " if types and o.type not in types: return False\n", " return True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "class GhEvent(AttrDict):\n", " \"Class for events returned from `fetch_events\"\n", " pass\n", "\n", "for o in Event: exec(f'class {snake2camel(o)}Event(GhEvent):pass')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "
class
GhEvent
[source]GhEvent
() :: `AttrDict`\n",
"\n",
"Class for events returned from `fetch_events"
],
"text/plain": [
"