Created
June 19, 2020 16:02
-
-
Save korakot/8b2ac01f0be8fd4d30fe4e5c82d8e10f to your computer and use it in GitHub Desktop.
Search Gitlab project for notebook files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests import get | |
project_id = '16454639' | |
def gen_items(project_id, path=''): | |
""" List all files in this folder """ | |
url = f'https://gitlab.com/api/v4/projects/{project_id}/repository/tree' | |
params = {'page': page, 'per_page': 100} | |
if path: | |
params['path'] = path | |
while True: | |
items = get(url, params).json() | |
if not items: break | |
yield from items | |
params['page'] += 1 | |
def search_ipynb(project_id): | |
""" Search only 3 levels to avoid thousands files """ | |
dirs = [] | |
# search root dir | |
for item in gen_items(project_id): | |
if item['name'].endswith('.ipynb'): | |
yield item['path'] | |
if item['type']=='tree': | |
dirs.append(item['path']) | |
# search 1st subdir | |
dirs2 = [] | |
for d in dirs: | |
for item in gen_items(project_id, d): | |
if item['name'].endswith('.ipynb'): | |
yield item['path'] | |
if item['type']=='tree': | |
dirs2.append(item['path']) | |
# search 2nd subdir | |
for d in dirs2: | |
for item in gen_items(project_id, d): | |
if item['name'].endswith('.ipynb'): | |
yield item['path'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment