Dr. Drang helped me realize that I wasn't making use of some of the easier parsing methods that the Squarespace docs describe. Thanks to the Doc's help I was able to greatly reduce the amount of code needed to fetch the latest Squarespace blog entry's URL, and extend it to grab the latest blog entry's title as well. I was also able to turn it into more of an object oriented data structure, and make it entirely self-contained so that if you'd like to import this as a module you very well can and all the dependencies will come along with it.
The code below is set up in a file called squarespace.py. The reason for this is that with this file naming convention you can start to build up a 'utility' module that is specific to your Squarespace needs. Then, if need be, you can import the specific utility functions you need (IE): “import utilityMethod from squarespace”
To note before reading the code itself:
- It is set up to use Editorial, as the previous version of this was as well. This file will output whatever interface method you call in an Editorial workflow step, and then you can save it as a variable name using another workflow step and then ultimately populate a Tweet or anything you'd like using said variable name.
- The squarespace class, as you can tell, takes one paramater: your Squarespace blog URL.
- Due to the fact that we are using the official Squarespace documentation to build this parsing, we no longer run into the problem of needing to filter out the nav bar or other custom HTML attributes. This will always get the latest post, for any Squarespace blog.
# Editorial Module import workflow class Squarespace(): def __init__(self, blogBaseUrl): import json, urllib2 self.blogBaseUrl = blogBaseUrl self.response = urllib2.urlopen(self.blogBaseUrl + '?format=json-pretty').read() self.jsonResponse = json.loads(self.response) def gatherLatestBlogPostUrl(self): path = self.jsonResponse['items']['urlId'] url = self.blogBaseUrl + path return url def gatherLatestBlogTitle(self): title = self.jsonResponse['items']['title'] return title # Interface: if __name__ == "__main__": squarespace = Squarespace('http://wrappedthoughts.com/') workflow.set_output(squarespace.gatherLatestBlogPostUrl()) workflow.set_output(squarespace.gatherLatestBlogTitle())
Take a look at the original version if you are interested in seeing the difference reading developer documentation can make.
[UPDATE]: Check out Dr Drang's contention with my script. He makes a lot of really great points. I originally wrote the parsing script as a way to learn more python and didn't consider looking at the docs as I would have learned considerably less that way. But he showed me the error of my ways in that the alternative provided by Squarespace is much easier. Due to that wake up call I switched over to their API and built an object oriented script around it.
Why did I choose to make it object oriented? Well to me, object oriented scripting is perfect for a select few cases - and in my opinion one of them this: will you be providing this script to others? Do you not want others to have to modify it? And if so do you want to provide them the easiest most readable way to do so? In those situations I personally believe it's a perfect case for object orientation. In this specific example I provide a class they can simply pass their blog URL to and then some nice interface class methods they can call on it. To me, that seems ideal. And while I agree with the fact that it is just as readable as calling the dictionary items directly, the methods I do believe helps with readability.
With all that being said, Dr Drang's does bring up a ton of good points that I think we all could learn from. Productivity and speed are definitely a cost when we program in an OO manner. And finding the balance along that line is tough. Thanks Dr. Good wake up call for my scripting.