My DA Favourites
Activity for a Rainy Day by PerryGallagher
Update your links! My blogging continues at http://blog.microugly.com.
23 Feb, 2007

DeviantArt Favourites on Your Website

Well, if you look to the left you'll see that my site now displays one of my DeviantArt Favourites at random.  Clicking the thumbnail will take you to the full view on the DeviantArt website.  I think it makes my site that little bit more interesting.

So how did I do it?

DeviantArt doesn't provide any practical method to display thumbs from their site.  They do have what they call Pasties which are lines of Javascript you can paste into your site to display images.  It works - but unless you are subscriber you can't display your own work or your own faves, just the most recently added deviations or most the most popular for the day.  And even if you are a subscriber the pasties are still very limited.  In the "Web 2.0" age where so many services are offered for free and everyone encourages the use of RSS feeds, I think it's kinda tight of DeviantArt to be so un-sharing.

Having said that, because DeviantArt is trying to sell subscriptions that offer you more features, like this, I'm not going to post my code for how I'm displaying faves on my site.  But I'm happy to explain.

The only requirements is access to a web programming language (i.e. PHP) and a database (i.e. MySQL).

I created 2 functions - the first is FirstRun function and the second AddNew function.

The FirstRun function is exectured just once to populate the database.  The first thing it does is reads the contents of http://microugly.deviantart.com/favourites/?&offset=100000 .  Note the offset value in that URL.  I don't have 100000 favourites, but by viewing a page with an extremely large offset value you can easily see how many pages of faves I have.  If you use an offset less than the number of deviations you've faved you won't be able to identify the total number of pages you have.  I then use a regular expression to extract the last page number you see for the links of pages.

So now I know how many pages of Faves I have and I know there are 24 faves per page I can use this calculation to set the correct offset value for each page of faves I have.  So my FirstRun function then loops through each page and using another regular expression I pull out the details for each thumbnail: deviantion title; deviation ID number; thumbnail URL; thumbnail height and width; URL to the Deviation; artist name; artist URL; and if it's flagged as mature.  The deviation ID is used as the primary key.  I don't get the date of the deviation because I was too lazy to write code to interpret strings like "1d 9h ago".  Although strtotime() may have made it easy.  All these details are stored in my database along with the time they were added to the database.

Once the FirstRun function has been executed once the AddNew function is called periodically to add any new Faves.

The AddNew function will use the same regular expressions to extract details from the first page of faves (which contains the most recently faved deviations) but it compares the found deviation IDs with those in the database and if there is a match it will stop extracting and the new fave details are inserted into the database.  If a match is not found AddNew will read the next page and so on until a match is found.  The AddNew function then returns how many faves were found

The AddNew function is triggered when someone visits my site, but to ensure it doesn't execute too frequently, or too infrequently, I query the database to see what day the last favourite was added and if a day has passed since then the AddNew function will be triggered. If no new favourite is found the most recently added deviation is updated with the current time so the AddNew function won't keep executing if it has nothing to add.

And from there it's just a matter of querying the database and displaying the results.  I could also adapt these functions to display my own DeviantArt Gallery , but my work isn't that interesting.

The other method I seen someone using was similiar in the way it read a page and using regular expressions to extract the information but it didn't database the results.  Instead it took advantage of a 'limit' parameter in the URL for the deviation faves to display just one item on a page.  Combined with a random offset you could easily extract one random item from DeviantArt and display it on your page.  But this is slower tham my method, and I didn't like the idea of so many frequent requests on the DeviantArt servers.  Especially if the code became widely adopted.

Filed In:

Comments

1. Beephe says…

hello, maybe u can help me a little, did u know how to create a pastie with my own work on it?
If u know, please let me know...
I Know that it's only a suscribed featured but there's have to be a way so... Please =)

Posted on Sat 22 Mar, 2008

Add comment:


Submit Comment Preview Comment

Rules: Paragraphs and linebreaks are automatically created (two or more linebreaks create a paragraph). Linebreaks between code tags remain linebreaks. Block tags cannot be enclosed by inline tags. Red attributes are required and green is optional.

Use "&lt;" and "&gt;" for "<" and ">". Enclosing PHP code in <code> tags will highlight the code (i.e. <code>&lt;?php echo 'hello world'; ?&gt;</code>).

List of valid tags: