3

I have been up and down these pages looking for how to do this and there are many similar posts but I can't seem to get it to work, so I find myself having to ask specifically how to do this.

I am trying to gather metrics about my software project in git hub. For many of these metrics you can use the API. However, one of the most interesting items are the unique visitors and view count on the github graphs/traffic and unfortunately this info is not located in the Github API. So, to get this I am trying to log into my github account navigate to the site then get the numbers. Located below is my code. I can't seem to get logged into github to do anything however (my url request continues to show a login page rather then the traffic page). I think it probably has something to do with the variables that need to be posted but I'm not sure whats wrong with them.

from requests import session
from bs4 import BeautifulSoup as bs

USER = 'MYID'
PASSWORD = 'MYPASSWORD'

URL1 = 'https://github.com/login'
URL2 = 'https://github.com/MYPROJ/graphs/traffic'

with session() as s:

    req = s.get(URL1).text
    html = bs(req)
    token = html.find("input", {"name": "authenticity_token"}).attrs['value']
    com_val = html.find("input", {"name": "commit"}).attrs['value']         

    login_data = {'login_field': USER,
                  'password': PASSWORD,
                  'authenticity_token' : token,
                  'commit' : com_val}

    r1 = s.post(URL1, data = login_data)
    r2 = s.get(URL2)

    print(r2.url)
    print bs(r2.text).find('span', {'class':'num js-uniques uniques'})

Any help is appreciated.

Thanks, -Jeff

0

1 Answer 1

3

Figured it out.
I was using the wrong address to post my login and username, as well as some other wrong bits.
This is the updated code that worked for me:

from requests import session
from bs4 import BeautifulSoup as bs

USER = 'MyUserName'
PASSWORD = 'Mypassword'

URL1 = 'https://github.com/session'
URL2 = 'https://github.com/MyProj/graphs/traffic-data'


with session() as s:
                  
    req = s.get(URL1).text
    html = bs(req)
    token = html.find("input", {"name": "authenticity_token"}).attrs['value']
    com_val = html.find("input", {"name": "commit"}).attrs['value']        
    
    login_data = {'login': USER,
                  'password': PASSWORD,
                  'commit' : com_val,
                  'authenticity_token' : token}
                      
    r1 = s.post(URL1, data = login_data)
    r2 = s.get(URL2)

    Cut1 = r2.text.split(',"summary":{"total":',2)
    
    ViewsTot = Cut1[1].split(',"unique":',1)
    ViewsUnq = ViewsTot[1].split('}}',1)
Sign up to request clarification or add additional context in comments.

1 Comment

I do not know about at the time of publishing, though now, Github requires cookies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.