Skip to content

Instantly share code, notes, and snippets.

@jwoschitz
Created August 14, 2017 08:21
Show Gist options
  • Save jwoschitz/780bca65b5ba29f75084d0c655a950a8 to your computer and use it in GitHub Desktop.
Save jwoschitz/780bca65b5ba29f75084d0c655a950a8 to your computer and use it in GitHub Desktop.
HTTP GET with Python requests
#!/usr/bin/env python
import sys, argparse, logging, requests, json
def main(args, loglevel):
logging.basicConfig(format="%(levelname)s: %(message)s", level=loglevel)
repositories =[]
r = requests.get('https://api.github.com/orgs/{}/repos'.format(args.organization), auth=(args.user, args.token))
if r.status_code != 200:
print("Error while accessing github: " + str(r))
return 1
print(r.links["next"])
print(r.links["last"])
repositories.extend(json.loads(r.text))
print(len(repositories))
logging.info("You passed an argument.")
logging.debug("Your Argument: %s" % args.organization)
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description = "Analyzes a given GitHub organization and lists stale repositories and potential owner info",
epilog = "As an alternative to the commandline, params can be placed in a file, one per line, and specified on the commandline like '%(prog)s @params.conf'.",
fromfile_prefix_chars = '@' )
parser.add_argument(
"organization",
help = "GitHub organization name which should be analyzed",
metavar = "ORG")
parser.add_argument(
"user",
help = "GitHub user for API access",
metavar = "USER")
parser.add_argument(
"token",
help = "GitHub token for API access",
metavar = "TOKEN")
parser.add_argument(
"-v",
"--verbose",
help="increase output verbosity",
action="store_true")
args = parser.parse_args()
if args.verbose:
loglevel = logging.DEBUG
else:
loglevel = logging.INFO
exit(main(args, loglevel))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment