RepoRoulette 🎲: 从 GitHub 随机抽样代码仓库

转动轮盘,看看你能获得哪些 GitHub 仓库!

PyPI version License Downloads Python application

🚀 安装

# 使用 pip
pip install reporoulette
# 从源码安装
git clone https://github.com/gojiplus/reporoulette.git
cd reporoulette
pip install -e .

📖 抽样方法

RepoRoulette 提供了三种不同的方法用于随机 GitHub 代码仓库抽样:

1. 🎯 基于 ID 的抽样

使用 GitHub 的顺序代码仓库 ID 系统,通过探测有效 ID 范围内的随机 ID 来生成真正随机的样本。 使用这种方法的缺点是命中率可能很低(因为许多 ID 无效,部分原因是代码仓库是私有的或被废弃的等)。 并且对代码仓库特征的任何过滤都必须等到你有了名称之后才能进行。

该函数将继续抽样,直到达到 max_attemptsn_samples。 你可以传递 seed 来实现可重复性。

from reporoulette import IDSampler
# 初始化抽样器
sampler = IDSampler(token="your_github_token")
# 获取 50 个随机代码仓库
repos = sampler.sample(n_samples=50)
# 打印基本统计信息
print(f"成功率: {sampler.success_rate:.2f}%")
print(f"收集到的样本: {len(repos)}")

2. ⏱️ 时间抽样

在指定范围内随机选择时间点(日期/小时组合),然后检索这些期间更新的代码仓库。

from reporoulette import TemporalSampler
from datetime import datetime, timedelta
# 定义一个日期范围(最近 3 个月)
end_date = datetime.now()
start_date = end_date - timedelta(days=90)
# 初始化抽样器
sampler = TemporalSampler(
  token="your_github_token",
  start_date=start_date,
  end_date=end_date
)
# 获取 100 个随机代码仓库
repos = sampler.sample(n_samples=100)
# 获取具有特定特征的代码仓库
filtered_repos = sampler.sample(
  n_samples=50,
  min_stars=10,
  languages=["python", "javascript"]
)

3. 🔍 BigQuery 抽样

BigQuerySampler 利用 Google BigQuery 的公共 GitHub 数据集来抽样代码仓库,具有高级过滤功能。

BigQuery 抽样器设置

  1. 创建一个 Google Cloud Platform (GCP) 项目 :
  2. 启用 BigQuery API :
    • 在你的项目中,转到 "API & 服务" > "库"
    • 搜索 "BigQuery API" 并启用它
  3. 创建服务账号 :
    • 转到 "IAM & 管理" > "服务账号"
    • 创建一个新的服务账号
    • 授予它 "BigQuery 用户" 角色
    • 创建并下载一个 JSON 密钥文件
  4. 安装所需的依赖项 :
pip install google-cloud-bigquery google-auth

  1. 使用 BigQuerySampler :
from reporoulette import BigQuerySampler
# 使用服务账号凭据初始化
sampler = BigQuerySampler(
  credentials_path="path/to/your-service-account-key.json",
  project_id="your-gcp-project-id",
  seed=42
)
# 抽样过去一年有提交的活跃代码仓库
active_repos = sampler.sample(
  n_samples=50,
  population="active",
  languages=["Python", "JavaScript"] # 可选的语言过滤器
)
# 跨随机日期抽样代码仓库
random_repos = sampler.sample_by_day(
  n_samples=50,
  days_to_sample=10,
  years_back=5
)
# 获取抽样代码仓库的语言信息
languages = sampler.get_languages(random_repos)
# 打印结果
for repo in random_repos:
  print(f"代码仓库: {repo['full_name']}")
  repo_languages = languages.get(repo['full_name'], [])
  if repo_languages:
    print(f"主要语言: {repo_languages[0]['language']}")
  print("---")

优点:

缺点:

4. GH Archive Sampler

GHArchiveSampler 通过从 GitHub Archive(一个记录公共 GitHub 时间线的项目)中抽样事件来获取代码仓库。

from reporoulette import GHArchiveSampler
# 使用可选参数初始化
sampler = GHArchiveSampler(seed=42) # 设置 seed 以实现可重复性
# 抽样代码仓库
repos = sampler.sample(
  n_samples=100,      # 要抽样的代码仓库数量
  days_to_sample=5,    # 要抽样的随机天数
  repos_per_day=20,    # 每天要抽样的代码仓库数量
  years_back=2,      # 要回溯多少年
  event_types=["PushEvent", "CreateEvent", "PullRequestEvent"] # 要考虑的事件类型
)
# 访问结果
for repo in repos:
  print(f"代码仓库: {repo['full_name']}")
  print(f"事件类型: {repo['event_type']}")
  print(f"抽样自: {repo['sampled_from']}")
  print("---")

📊 示例用例

🤝 贡献

欢迎贡献! 请随时提交 Pull Request。

📜 许可证

该项目已获得 MIT 许可证的许可 - 有关详细信息,请参见 LICENSE 文件。

🔗 相关项目

Built with ❤️ by Gojiplus