Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def compression(data):
- ret = json.dumps(data, indent=4)
- if web.input().has_key('compression'):
- web.header('Content-Encoding', 'gzip')
- gzip_buffer = StringIO.StringIO()
- gzip_file = gzip.GzipFile(mode='wb',
- fileobj=gzip_buffer)
- gzip_file.write(ret)
- gzip_file.close()
- ret = gzip_buffer.getvalue()
- return ret
- class Error(web.HTTPError):
- '''HTTP errors.'''
- headers = {'Content-Type': 'application/json'}
- def __init__(self, note, headers=None, status='404 Not Found'):
- message = json.dumps([{'error': note}])
- web.HTTPError.__init__(self, status, headers or self.headers,
- unicode(message))
- class Github:
- def GET(self, query):
- params = web.input()
- if not params.has_key('repo'):
- raise Error('Repository must be specified with the repo parameter', status='422 Unprocessable Entity')
- if not params.has_key('lang'):
- language = ''
- else:
- language = 'language:{0}'.format(params.get('lang'))
- repo = 'repo:{0}'.format(params.get('repo'))
- where = '{0}+in:file'.format(query)
- query_to_github = ' '.join([query, language, repo])
- response = requests.get('https://api.github.com/search/code', params={'q': query_to_github}).json()
- ret = []
- for repo in response['items']:
- value = {'name': repo['name'], 'path': repo['path']}
- value['content'] = requests.get(repo['git_url']).content
- if value['content'].index('API rate limit exceeded for'):
- value.pop('content', None)
- logging.debug(repo.keys())
- location = 'https://github.com/{0}/raw/master/{1}'.format(repo['repository']['full_name'], value['path'])
- value['url'] = location
- ret.append(value)
- return compression(ret)
Advertisement
Add Comment
Please, Sign In to add comment