Completed
Pull Request — master (#348)
by Tomaz
02:21
created

label_to_dict()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 8
rs 9.4286
1
from github import Github
2
import requests
3
from bs4 import BeautifulSoup
4
5
from st2actions.runners.pythonrunner import Action
6
7
__all__ = [
8
    'BaseGithubAction'
9
]
10
11
BASE_URL = 'https://github.com'
12
13
14
class BaseGithubAction(Action):
15
    def __init__(self, config):
16
        super(BaseGithubAction, self).__init__(config=config)
17
        token = self.config.get('token', None)
18
        token = token or None
19
        self._client = Github(token)
20
21
    def _web_session(self):
22
        '''Returns a requests session to scrape off the web'''
23
        login_url = BASE_URL + '/login'
24
        session = requests.Session()
25
        request = session.get(login_url).text
26
        html = BeautifulSoup(request)
27
        token = html.find('input', {'name': 'authenticity_token'}).attrs['value']
28
        commit_value = html.find('input', {'name': 'commit'}).attrs['value']
29
        session_path = html.find('form', {'method': 'post'}).attrs['action']
30
31
        login_data = {
32
            'login': self.config['user'],
33
            'password': self.config['password'],
34
            'commit': commit_value,
35
            'authenticity_token': token
36
        }
37
38
        session_url = BASE_URL + session_path
39
        session.post(session_url, data=login_data)
40
        return session
41
42
    def _get_analytics(self, category, repo):
43
        url = 'https://github.com/' + repo + '/graphs/' + category + '.json'
44
        s = self._web_session()
45
        response = s.get(url)
46
        return response.json()
47