Search code examples
pythonpython-3.xsessionbeautifulsoupflurry

Flurry Login Requests.Session() Python 3


So I had this question answered before here. However, something on the Flurry website has changed and the answer no longer works.

from bs4 import BeautifulSoup
import requests 

loginurl = "https://dev.flurry.com/secure/loginAction.do"
csvurl = "https://dev.flurry.com/eventdata/.../..."       #URL to get CSV
data = {'loginEmail': 'user', 'loginPassword': 'pass'}

with requests.Session() as session:
    session.headers.update({
         "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"})
    soup = BeautifulSoup(session.get(loginurl).content)
    name = soup.select_one("input[name=struts.token.name]")["value"]
    data["struts.token.name"] = name
    data[name] = soup.select_one("input[name={}]".format(name))["value"]
    login = session.post(loginurl, data=data)
    getcsv = session.get(csvurl)

The code above worked great for the last month and then it stopped working last week. For the life of me, I can't figure out what on the website has changed. ID Names and tokens all look correct, username and pass hasnt changed. Im at a loss.

If I login manually, I can download the csv just fine using the csvurl.

login.histroy shows:

[<Response [302]>, <Response [302]>, <Response [302]>, <Response [302]>, <Response [303]>]

If anyone could take a look and figure out where I am going wrong, I would greatly appreciate it.

Thanks.

UPDATE

So from the new login address, I see the post needs to be in this format:

{"data":{"type":"session","id":"bd7d8dc1-4a86-4aed-a618-0b2765b03fb7","attributes":{"scopes":"","email":"myemail","password":"mypass","remember":"false"}}}

What I can't figure out though is how they generated the id. Can anyone take a look?


Solution

  • You can offer up a dummy session id and it will log you in with a new one. Postman interceptor helped with the redirects.

    import requests
    import json
    
    def login(email, password, session, session_id=None):
        """ Authenticate with flurry.com, start a fresh session 
            if no session id is provided. """ 
        auth_url = 'https://auth.flurry.com/auth/v1/session'
        login_url = 'https://login.flurry.com'
        auth_method = 'application/vnd.api+json'
        if session_id is None:
            session_id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
        response = session.request('OPTIONS', auth_url, data='')
        headers = response.headers
        headers.update({'origin': login_url, 'referer': login_url,
                        'accept': auth_method, 'content-type': auth_method})
        data = {'data': {'type': 'session', 'id': session_id, 'attributes': {
                'scopes': '', 'email': email, 'password': password, 'remember': 'false'}}}
        payload = json.dumps(data)
        response = session.request('POST', auth_url, data=payload, headers=headers)
        return response
    
    email, password = 'your-email', 'your-password'
    session = requests.Session()
    response = login(email, password, session)
    # session_id = response.json()['data']['id']
    

    And then you can grab your csv data after hitting the old site:

    response = session.request('GET', 'https://dev.flurry.com/home.do')
    data = session.request('GET', your_csv_url).text