24.07.2016 Views

www.allitebooks.com

Learning%20Data%20Mining%20with%20Python

Learning%20Data%20Mining%20with%20Python

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Clustering News Articles<br />

Now let's create the username and password:<br />

USERNAME = ""<br />

PASSWORD = ""<br />

Next, we are going to create a function to log with this information. The reddit login<br />

API will return a token that you can use for further connections, which will be the<br />

result of this function. The code is as follows:<br />

def login(username, password):<br />

First, if you don't want to add your password to the script, you can set it to None and<br />

you will be prompted, as explained previously. The code is as follows:<br />

if password is None:<br />

password = getpass.getpass("Enter reddit password for user {}:<br />

".format(username))<br />

It is very important that you set the user agent to a unique value, or your connection<br />

might be severely restricted. The code is as follows:<br />

headers = {"User-Agent": USER_AGENT}<br />

Next, we set up a HTTP authorization object to allow us to login at reddit's servers:<br />

client_auth = requests.auth.HTTPBasicAuth(CLIENT_ID, CLIENT_<br />

SECRET)<br />

To login, we make a POST request to the access_token endpoint. The data we send<br />

is our username and password, along with the grant type that is set to password for<br />

this example:<br />

post_data = {"grant_type": "password", "username": username,<br />

"password": password}<br />

Finally, we use the requests library to make the login request (this is done via a<br />

HTTP POST request) and return the result, which is a dictionary of values. One of<br />

these values is the token we will need for future requests. The code is as follows:<br />

response = requests.post("https://<strong>www</strong>.reddit.<strong>com</strong>/api/v1/access_<br />

token", auth=client_auth, data=post_data, headers=headers)<br />

return response.json()<br />

We can call now our function to get a token:<br />

token = login(USERNAME, PASSWORD)<br />

[ 214 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!