I’ve been curious about learning leathercrafting for a while now but I haven’t known where to start. I was suggested to check out a Society for Creative Anachronism but I couldn’t find anything local.
I found the very helpful Leathercraft subreddit and through them I discovered Tandy Leather.
The cool thing about Tandy Leather is they provide classes on leatherworking! Some are free and some cost money but they are priced reasonably. A listing of their stores and the upcoming classes are found at http://www.tandyleather.com/en/leathercraft-classes.
Tandy does have an email but it’s not store specific so I won’t get the upcoming courses for the store closest to me (Richmond, VA). There is way to solve this though!
We’re going to scrape the page, grab the information for the Richmond store, email it to ourselves, and keep the script on an internet connected server that will allow us to set a cron job to run this weekly.
We’ll review how to do this in Python and Ruby.
Python
The script for this is very short and easy. For scraping we’re going to use BeautifulSoup. And to email the results to ourselves we’re going to use Mandrill.
We’re going to use the great requests
library to make the connection to the page then put the response into a BeautifulSoup object.
At a command line (preferably in a virtualenv) let’s install what we need for now.
1 2 3 |
|
Then in our script let’s add:
1 2 3 4 5 6 7 |
|
Next we want to find the Richmond information on the page. The html for that looks like:
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 |
|
Looking at the html the only identifier for the Richmond block is <a name="id_631"></a>
which doesn’t exist in the class store-class and there is no unique identifier for Richmond inside the div.store-class
So what we have to do is identify the name="id_631"
then grab the next div
whose class is store-class
.
We can do that with one line of code courtesy of BeautifulSoup.
1
|
|
That line finds the a
whose name
is id_631
then finds the next sibling that is a div
whose class
is store-class
.
And that will give us the entire div with all of the information we want!
But there is a caveat. The object is a BeautifulSoup object so we’re going to need to convert that to a string.
1
|
|
That’s it!
We’ve got the information now we just need to get it to us.
For that I’m using Mandrill. Mandrill is a transaction email service made by Mailchimp. Just like MailChimp there is a free tier that will be more than enough for us.
Go to Mandrill, sign up, and get developer key.
At a command line let’s install the mandrill library.
1
|
|
Now to make our connection:
1
|
|
And let’s build out our message.
1 2 3 4 5 6 |
|
What we’re doing here is setting the html body of the message to the information we scraped from the web page. We give our email a subject, set the from email address, and the to.
Then we make a call to messages.send:
1
|
|
There is a return result so if you don’t get the email you can output result
and see what error you received.
Our final script looks like this:
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 |
|
Notice I did include the location for where our python is located. This is required on our server to set a cron job to automatically email ourselves the information.
A cron tutorial is a little outside the scope of this post.
I did promise to show how to do this in Ruby also didn’t I?
Ruby
The Ruby script is VERY similar to the Python version.
We’re going to need to install the following gems:
1 2 3 |
|
We then require our libraries:
1 2 3 |
|
We use httparty to get the page:
1
|
|
Put that into a nokogiri object:
1
|
|
Create our connection to mandrill
1
|
|
We find the section we’re looking for, create a new empty string to hold the information, but then there’s an extra step with nokogiri.
1 2 3 4 5 6 7 8 |
|
We have to loop over our id_631 to get the next element, but that’s an <hr>
, so we have to get the next element after that which is the div.store-class
.
We build the message the same way as before but using Ruby hashrocket syntax.
1 2 3 4 5 6 7 |
|
Then make the call to mandrill to send.
1
|
|
The entire script is:
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 |
|
And that’s it!
Summary
This was a quick and fun project to put together. And useful! I set my cron job to run every Sunday morning at 8am. I tested out my cron job and it was working when I set it a minute into the future, but the real test will be tomorrow morning!