diff --git a/update_repos b/update_repos index b3702203c8fe69a51785f119c6895b4295087754..7286cdd0f6ccd9a7fb09bd7eb38a78767e420d26 100755 --- a/update_repos +++ b/update_repos @@ -1,13 +1,15 @@ #!/usr/bin/python # encoding: utf-8 +import base64 import sys import os import argparse from collections import namedtuple from subprocess import Popen, PIPE -from urllib2 import urlopen +from urllib import urlencode +from urllib2 import Request, urlopen import json from urlparse import urlparse @@ -15,8 +17,8 @@ WHITELIST=[] BLACKLIST=[] LISTINGS_PER_PAGE = 100 -ACCESS_TOKEN_PARAM = '?access_token=%s' -LISTING_PAGE_PARAM = '&per_page=%d&page=%d' +LISTINGS_PER_PAGE_PARAM = 'per_page' +LISTING_PAGE_PARAM = 'page' GITHUB_API_HOST = 'https://api.github.com' GIT_CLONE_CMD = 'git clone %s %s %s' @@ -73,12 +75,23 @@ class AttributeDict(dict): self[attr] = value def read_api_uri(uri, config, page=1): - uri += ACCESS_TOKEN_PARAM % config.token + LISTING_PAGE_PARAM % (LISTINGS_PER_PAGE, page) + params = { + LISTINGS_PER_PAGE_PARAM: LISTINGS_PER_PAGE, + LISTING_PAGE_PARAM: page, + } + uri += '?' + urlencode(params) + + creds = base64.b64encode(config.token.encode('utf-8')).decode('ascii') + basic_auth = 'Basic {}'.format(creds) + headers = { + 'Authorization': basic_auth, + } if config.debug: - print "Trying:", uri + print "Trying URI {} with headers {}".format(uri, headers) - return urlopen(uri).read() + req = Request(uri, headers=headers) + return urlopen(req).read() def get_json(uri, config, obj_type=AttributeDict, page=1): return json.loads(read_api_uri(uri, config, page), object_hook=obj_type)