<jupyter_start><jupyter_text>Github OrgStats ![CI](https://github.com/Living-with-machines/gh_orgstats/workflows/CI/badge.svg) `gh_orgstats` is intended to provide some easy ways of getting stats for a GitHub org. `gh_orgstats` does this by wrapping some functions around [PyGithub](https://github.com/PyGithub/PyGithub). This code is mainly intended to help generate reports as part of a GitHub actions pipeline to update stats for a funder. For an example of this being used for generating a weekly report see the [Living with Machines Github stats report](https://github.com/Living-with-machines/github_stats_report) To use `PyGithub` we need to authenticate with GitHub this is done via a token. This token is used to authenticate access and requires at least scope for public repos. See https://github.com/settings/tokens to register a token.<jupyter_code>from dotenv import load_dotenv
import os<jupyter_output><empty_output><jupyter_text>In this case we use `dotenv` to load the token from a `.env` files.<jupyter_code>load_dotenv()
GH_TOKEN = os.getenv("GH_TOKEN")<jupyter_output><empty_output><jupyter_text>Currently all functionality is contained within the `stats` module.<jupyter_code>from gh_orgstats.stats import *<jupyter_output><empty_output><jupyter_text>The `OrgStats` class is used to get stats for a GitHub organization. To create an instance of this class we need to pass a GitHub token to authenticate and the name of the Organization you want stats for.<jupyter_code>test_org = OrgStats(GH_TOKEN, "ghorgstatstestorg")
test_org<jupyter_output><empty_output><jupyter_text>Organization repositories As a start we can grab the repositories for an organization via the `repos` attribute of our OrgStats instance<jupyter_code>test_org.repos<jupyter_output><empty_output><jupyter_text>We can also get a sense of what is in the repository by looking at the file extensions for each repository.   Repository file types<jupyter_code>test_org.get_org_file_ext_frequency()<jupyter_output><empty_output><jupyter_text>Filtering by publication status We can also filter this by publication status<jupyter_code>test_org.get_org_file_ext_frequency(pub_status='public')<jupyter_output><empty_output><jupyter_text>Snapshot statsSnapshot stats are captured based on the current view and aren't updated. These include forks and clones<jupyter_code>test_org.snapshot_stats.to_dict()<jupyter_output><empty_output><jupyter_text>Traffic statsWe can also get a longer view by using traffic stats for views and clones<jupyter_code>test_org.get_org_views_traffic(save_dir='readme_dir')<jupyter_output><empty_output><jupyter_text>`get_org_views_traffic` will grab data via the GitHub api and update a CSV for each repository under the organization (by default only public) with views counts. This is largely intended to be used to semi-regularly update these stats by running this code as part of a GitHub Action or cron job.If you want to load a DataFrame of traffic you can pass `load=True`<jupyter_code>test_org.get_org_views_traffic(save_dir='readme_dir', load=True).to_dict()<jupyter_output><empty_output><jupyter_text>Similarly the same can be done for clones<jupyter_code>#hide
!rm -r readme_dir<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06974581525108493, 'fraction_numerical': 0.0, 'mean_word_length': 7.211195928753181, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '638399', 'license': 'Apache-2.0', 'n_tokens_mistral': 1001, 'n_tokens_neox': 966, 'n_words': 334, 'path': 'nbs/index.ipynb', 'repo_name': 'Living-with-machines/gh_orgstats', 'size': '3226'}