Scraping data from the web for import into WordPress (Illozoo.com Case Study)

By May 26, 2015 Web No Comments

Introduction

Illozoo.com is an exellent resource for hiring illustrators or using it as an inspiration.

Why cargo collective was failing

When my client first called me, he asked me to add a profile image to an existing artist.  He further explained to me that the interface does not support artist image (the one you see on mouse over). I checked to see how it’s being done and to my surprise it was hard-coded in the style sheets. I agreed to maintain the website even though its ‘updating method’ was a terrible practice and I knew the site was living on borrowed time because of that. It was a short-lived, hackey, quick ‘n ‘dirty fix. Like the american economy.

No thumbnails support

cargo_thumbnailsIn addition to that, the way Cargo handles thumbnails is, ummm… by not handling thumbnails. What I mean is that the images are loaded full-size and then re-sized on the front-end using CSS. What that results in, is a large page size that takes 5 seconds to fully load for the first time. If you are familiar with images on the web you should know that that is a big No, No. (Unless preloading everything is exactly what you want to accomplish) Take a look here!

 

No user-friendly interface

cargo_shortcodesRe-arranging the portfolio images was another nightmare. There is no drag-n-drop interface. To make a slideshow you will need to select all the shortcodes for the images (ex {image1},{image2}, etc)  and click on slideshow button. That would wrap the shortcodes in another {slideshow} {slideshow/} shortcode. Check it out for yourself.

Moto for 2014: No end-user deserves to copy and paste shortcodes to change the order of their portfolio!

No access to back-end code

2014-09-06_164638Lastly, after a few months of maintaining the website, I was asked to add sortable categories feature. So I eagerly agreed to do it, even though I had no idea how to at the time. I just knew it was on the web, therefore it was possible. Well, Cargo does not provide such feature, so I was at least hoping that I’ll have access to the back-end code of the theme. No such luck. The theme was too old and I was told by cargo that to do this, I’d need to change the theme to a different, newer one (Developer theme). I tried that and it messed up the site pretty entirely. A lot more work would have been required to get the website back to where it was, leave alone the sortable categories. I reverted back to the old theme.

I didn’t give up just yet. I ended up implementing another quick ‘n’ dirty fix. Below is a jQuery code that has a JSON object array with all the artist’s names and their respective category. It adds class names to the DIVs right before initiation of the isotopes plugin. All on the front-end, all hard-coded.

Adding a new artist now required editing style sheets and javascript code.

I shall name this script: Front-end Categories Script  for jQuery Isotopes using JSON Array

 

Why did I choose WordPress?

A) It is user-friendly.

B) There are a lot of plugins that could do exactly what I needed.

C) Because creating my own CMS would take much longer

The easy Part:

Re-creating Illozoo in WordPress

I started by looking for a really simple theme which would get me as close as possible to what I was looking for. Then I went on a plugin shopping-spree!

Here’s what I end up with:

Theme: Hatch – Free, simple, and optimized for mobile.

Plugins:

Easy Gallery – Drag and drop to re-arrange images. One click to delete. 

Galleriffic – Creates thumbnails and slideshow. It can even paginate the thumbnails and add play/pause slideshow buttons.

Multiple Post Thumbnails – Self-explanatory. Duh! Provides more than one featured images per post. One for the Artwork and one for the Artist (on mouse over)

Portfolio Post Type – Adds Portfolio Type Posts (So we don’t need to use actual blog posts). Comes with portfolio categories too.

Simple Custom Post Order – Enables user-friendly drag-n-drop re-arrangement/re-ordering of the above Portfolio Post type. (or any other custom-post type)

MP Isotopes – Adds the cool, animated filtering of categories feature. (The plugin didn’t work off the box. Custom jQuery setup was needed)

Team Manager – Used for the WHO section. Add a new member simply by dropping a picture and a title. 

 

—————-

 

 

Continued from part 1

Transferring the data (The hard part)

You may think that building the website would be 95% of the work and then transferring the data is like “just a few hours of work”, right?

I thought the same thing right up until I actually got my hands on it.

 

Doing it manually (aka Dream on Miro!)

o-ANGRY-EMAIL-facebookMy first thought was: I’ll just do it manually one by one. It can’t be that bad… right?

Actually, it can be – there are 95 artists with an average of ~36 images per artists (a total of ~3420 images) and each artist has ~3 categories that s/he belongs to. Good luck, have fun!

Nope! Not me! I just HATE doing repetitive work, which I know beyond doubt, the computer can automate and do for me. So, my second thought was: I’ll just use a macros app to record the steps/actions for creating a single artist:

Copying the name; Downloading the images; Creating the artist in WP and pasting the name; Copying the categories and then turning them into checkbox clicks… somehow; Uploading the previously downloaded images; Hit Publish!

Then I’d just hit the play button and let the computer do it all… somehow! (Eat, Sleep, Rave, Repeat) Whatcha think? Do-able? No shot! Mission impossible! I couldn’t get it to create a single artist. Even the artist I originally recorded the actions from… leave alone using those same actions to create a different artist.

I gave up. For a split second I thought of calling my client and saying: “Here’s your new website! I’ve done the first artist for you, now you just need to do the other 94 yourself… ” Hahaha

 

Doing it like a boss (aka Put your big boy pants on Miro!)

coders-meme

 You know this saying “Primates are at their best when cornered”? That’s what happened. I got cornered. 

I came up with the following plan:

  1. Transfer all the images to my server
  2. Scrape all the artists’ names, image URLs, and categories as a multidimensional JSON array.
  3. Create a script to insert the artists and assign the images and categories using the data from the array.

 Sounds simple, right?

It fucking worked! Blood, sweat, and tears but it worked!

 

Step1 – Scrape image URLs and name of a single artist

Fortunately, Cargo’s domain policy appears to be Cross-origin resource sharing (CORS) which allows for AJAX calls from different domain.

In the following code I used a hardcoded URL for a single artist. The AJAX request gets a result with all the html  from that URL. jQuery can treat the result variable as plain html. That means that you can use $(result).find(‘.project_title’).text(); and it will find that class for you, even though it’s not in your local DOM. Pretty sweet. I output the name as H1 and put the images into a div just to test it here.

Here’s a working demo of the above script: (won’t work if the website is no longer live)

View Demo – jsFiddle

Here’s the result:

2014-09-09_115705

 

Step 2 – Get the URL of each artist

Time to get the links to each artist. (Then we’ll run the script from step 1 for each link in step 3.)

That was pretty simple. I made a copy of the homepage on my local machine and added a jQuery script at the bottom of it. Now that I think about it, I could have just copied the script and use RegEx.
Anyways, Here’s the script I added to get the URLs:

 

Here’s what I got as a result:
2014-09-09_103046

 

Step 3 – Scrape data from all the artists 

Time to run the script from step 1 recursively, feeding it with the URLs we gathered from step 2 and push() the data into an array instead of output it as html:

View Demo – jsFiddle

Now, we have all the artists names and their images as a multidimensional JSON array. It looks scary when not formatted but if you run it through this json prettifier/formatter it looks much more humanly readable.

View the Json array

json-prettifier

 

Step 4 – Download the images to my server

Now that we have all the URLs to every image, it’s time to download them to my server at mediatemple. PHP time!

Sidenote: Originally, in step 1, I collected all the images from all artists as one long simple array without knowing which artist the image belonged to. My plan was to just get the images to my server. It worked – but not quite. I ended up being short by approximately 60 images. How did I know that? The array.length (~3420) didn’t match the number of files I had in the folder at the end. On top of that, I didn’t know why. It drove me crazy! It turned out that some of the images had the same name for different artists. Simple names like 1.jpg 2.jpg 3.jpg. etc. Since Cargo uses different payloads (payload108, payload192) same filenames didn’t bother them, but I was downloading all the images in the same folder so the ones with the same names were getting overwritten. Which is why I ended up using multidimensional array with the artists’ names in it. Prior to saving the image on my server I added the name of the artist in front of the file name (as slug). That solved the problem. I was now saving john-smith-1.jpg and jane-doe-1.jpg while they both had just 1.jpg as image name. Still, It was not bullet-proof but what are the chances of having 2 artists with the exact same first and last name and same file-naming habit? (Adding an MD5 hash of the UNIX timestamp seemed like an overkill)

 

I chopped the array down to just 1 artist and start playing with the code until I got all of that artist’s images on my server.

The following code does 3 things simultaneously. It comes up with a new filename (firstname-lastname_filename.jpg). Then it downloads the image to my server using the new name, and finally it updates the php array with the new name and prints a new JSON array with updated local image filenames instead of remote URLs.

Here’s the new JSON. It’s just like the old one but instead of having names like: 
http://payload103.cargocollective.com/1/9/300860/4375976/Elizabeth L Cline_Shopping exces_c1.jpg
They are now just ionut-radulescu_Elizabeth L Cline_Shopping exces_c1.jpg

View the Json array

2014-09-10_012006

 

To be continued… (The WordPress Part)

Leave a Reply