{ "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 ghapi.core import *\n", "from ghapi.page import *\n", "\n", "import time\n", "from itertools import islice" ] }, { "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(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) or (repo and not owner): 'Must pass both repo and owner, if passing either'\n", " if owner: return api.activity.list_public_events_for_repo_network,{'owner':owner,'repo':repo}\n", " if org: return api.activity.list_public_org_events,{'org':org}\n", " if username: return api.activity.list_public_events_for_user,{'username':username}\n", " return api.activity.list_public_events,{}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "@patch\n", "def list_events(self:GhApi, username=None, org=None, owner=None, repo=None, per_page=30, page=1):\n", " \"Fetch public events for repo network, org, user, or all\"\n", " oper,kwargs = _list_events(username=username, org=org, owner=owner, repo=repo)\n", " return oper(per_page=per_page, page=page, **kwargs)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "@patch\n", "def list_events_parallel(self:GhApi, username=None, org=None, owner=None, repo=None, per_page=30, n_pages=8):\n", " \"Fetch as many events from `list_events` in parallel as available\"\n", " oper,kwargs = _list_events(username=username, org=org, owner=owner, repo=repo)\n", " return pages(oper, n_pages, per_page=per_page, **kwargs).concat()" ] }, { "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", "def fetch_events(types=None):\n", " \"Generate an infinite stream of events optionally filtered to `types`\"\n", " if types: types=setify(types)\n", " seen = set()\n", " while True:\n", " evts = [o for o in api.list_events_parallel(n_pages=2) if o.id not in seen]\n", " print('\\n***', len(evts))\n", " for o in evts:\n", " seen.add(o.id)\n", " if not types or o.type in types: yield o\n", " time.sleep(0.6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for o in islice(fetch_events('PullRequestEvent'), 100): print(o.id, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "next(fetch_events('PullRequestEvent'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "evts = GhEvents(types=('IssuesEvent','ReleaseEvent','PullRequestEvent'), bots=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for ev in evts: ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for b in islice(evts,10): ..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export -" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Converted 00_core.ipynb.\n", "Converted 01_actions.ipynb.\n", "Converted 02_auth.ipynb.\n", "Converted 03_page.ipynb.\n", "Converted 04_event.ipynb.\n", "Converted 10_cli.ipynb.\n", "Converted 50_fullapi.ipynb.\n", "Converted 80_tutorial_actions.ipynb.\n", "Converted 90_build_lib.ipynb.\n", "Converted index.ipynb.\n" ] } ], "source": [ "#hide\n", "from nbdev.export import notebook2script\n", "notebook2script()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }