forked from austinhle/readme-analysis
/
api.py
57 lines (44 loc) · 1.69 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import ConfigParser
from requests.auth import AuthBase
import slumber
import os.path
import cache
logging.basicConfig(level=logging.INFO, format="%(message)s")
default_requests_session = cache.get_session(timeout=1)
default_requests_session.headers['User-Agent'] =\
"Andrew Head (for academic analysis) <andrewhead@eecs.berekeley.edu, Austin Le (for academic" +\
" analysis) <austinhle@berkeley.edu>"
lib_config = ConfigParser.ConfigParser()
lib_config.read(os.path.expanduser(os.path.join('~', '.libraries_config')))
libraries_io_api_key = lib_config.get('api', 'API_KEY')
class LibrariesIoAuth(AuthBase):
def __init__(self, api_key):
self.api_key = api_key
def __call__(self, request):
request.data['api_key'] = self.api_key
return request
class LibrariesIo(slumber.API):
def __init__(self, *args, **kwargs):
session = cache.get_session(timeout=1.5)
session.params['api_key'] = libraries_io_api_key
super(self.__class__, self).__init__(
'https://libraries.io/api',
session=session,
*args, **kwargs
)
gh_config = ConfigParser.ConfigParser()
gh_config.read(os.path.expanduser(os.path.join('~', '.github', 'github.cfg')))
gh_username = gh_config.get('auth', 'username')
gh_password = gh_config.get('auth', 'password')
class Github(slumber.API):
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(
'https://api.github.com',
auth=(gh_username, gh_password),
session=cache.get_session(timeout=1.0),
*args, **kwargs
)