Skip to content

Instantly share code, notes, and snippets.

@skshetry
Created December 31, 2020 07:23
Show Gist options
  • Save skshetry/94d04f5b505d84ea614834617d647cde to your computer and use it in GitHub Desktop.
Save skshetry/94d04f5b505d84ea614834617d647cde to your computer and use it in GitHub Desktop.
Fetch all files matching query provided.
import sys
from github import Github
from pathlib import Path
token = Path(".token").read_text().strip()
g = Github(token)
args = sys.argv
if len(args) < 2:
raise Exception("Query not provided")
query = " ".join(sys.argv[1:])
print(f"Querying with '{query}' for files and downloading ...")
for file in g.search_code(query=query):
repo_path = Path(file.repository.full_name)
file_path = repo_path / file.path
file_path.parent.mkdir(parents=True, exist_ok=True)
file_path.write_bytes(file.decoded_content)
@skshetry
Copy link
Author

skshetry commented Dec 31, 2020

Pre-requisites:

  1. Copy the script in a new directory.
  2. Generate a new token.
  3. Save the token in a file .token next to the script.
  4. pip install PyGithub

Example Usage: python fetch_files_from_query.py filename:dvc.yaml
Any other valid Github search query could be provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment