Skip to content

Instantly share code, notes, and snippets.

@urpylka
Last active December 2, 2020 22:37
Show Gist options
  • Save urpylka/9a404991b28aeff006a34fb64da12de4 to your computer and use it in GitHub Desktop.
Save urpylka/9a404991b28aeff006a34fb64da12de4 to your computer and use it in GitHub Desktop.
Script prepare bash-file for adding all forks
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:set ts=4 sw=4 et:
# Copyright 2019 Artem Smirnov
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import sys
try:
import requests
except:
print("execute: pip install requests")
exit(1)
def get_fork(username, repo, forks, auth=None):
page = 1
while 1:
r = None
request = "https://api.github.com/repos/{}/{}/forks?page={}&per_page=100".format(username, repo, page)
if auth is None: r = requests.get(request)
else: r = requests.get(request, auth=(auth['login'], auth['secret']))
j = r.json()
r.close()
if 'message' in j:
print("username: {}, repo: {}".format(username, repo))
print(j['message'] + " " + j['documentation_url'])
if str(j['message']) == "Not Found": break
else: exit(1)
if len(j) == 0: break
else: page += 1
for item in j:
forks.append({'user': item['owner']['login'], 'repo': item['name']})
if auth is None:
get_fork(item['owner']['login'], item['name'], forks)
else:
get_fork(item['owner']['login'], item['name'], forks, auth)
def main():
if len(sys.argv) < 2:
print("Use: ./get-forks.py user/repo login secret")
exit(1)
username, repo = sys.argv[1].split('/')
if len(sys.argv) > 2:
auth = {}
auth['login'] = sys.argv[2]
auth['secret'] = sys.argv[3]
forks = list()
forks.append({'user': username, 'repo': repo})
if len(sys.argv) < 3:
get_fork(username, repo, forks)
else:
get_fork(username, repo, forks, auth)
filename = username + "-forks.sh"
print("Found {} forks!".format(len(forks)))
print("Use: /bin/bash " + filename)
filename = username + "-forks.sh"
with io.open(filename, "w") as f:
for item in forks:
f.write("git remote add {} https://github.com/{}/{}.git\n".format(item['user'], item['user'], item['repo']))
if __name__ == "__main__":
main()
@urpylka
Copy link
Author

urpylka commented Jul 9, 2019

Ideas:

  • time counter
  • ^T - statistics (counts, time)
  • print format for repo not founded
  • realtime file adding instead list in RAM
  • parallel requests
  • wait if API Rate limit reached

@Taher-Ghaleb
Copy link

You may add '&per_page=100' to the query to make it faster (100 items per page instead of the default of 30). So, Line 35 would look like:

    request = "https://api.github.com/repos/{}/{}/forks?page={}&per_page=100".format(username, repo, page)

Thanks.

@urpylka
Copy link
Author

urpylka commented Dec 2, 2020

@Taher-Ghaleb thanks!

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