Shop Forum More Submit  Join Login by photofroggy by photofroggy
This little piece of Python code grabs the ID number for a random deviation from a particular users gallery. DiFi is used to grab the deviation ID numbers. Created by me.

This was something I decided to create after doing some research into the DiFi used in deviantART. I discovered a way to grab the favourites of a particular user through DiFi, and wondered if I could do anything else involving the gallery of a deviant.

DiFi works by sending a URL with your request in it, and you are given a response in a human-readable table, or another format if you choose it. The URL contains the query as c[]=class;method;parameters. After a while of playing around I found that when using DiFi to get deviations listed in a users gallery, class "Aggregations" and method "get_galleries_initial" are used. The parameters are 3 numbers separated by commas. The first parameter is always the user's ID number, so I used my own when testing.

After playing around even more, I discovered that the second parameter generally refers to either the user's main gallery or their favourites, 20 and 21 respectively. The third parameter defines what is returned from those particular places. 0 returns a list of all the deviation IDs stored, and 1 will return extra information and ID numbers.

Using Aggregations;get_galleries_initial;1570401,20,1 will return the ID numbers for my personal galleries (excluding favourites) and the HTML code used, I assume, to display the small section in the side bar which links to each gallery.

To get the full list of deviations in my gallery I would use Aggregations;get_galleries_initial;1570401,20,0. That was all well and good, but I wanted to be able to get deviations from the galleries of other users too, so I needed to be able to get the user ID number of any deviant.

I opened up the source code for my deviantART page and did a quick search to find the first instance of my user ID number. The first instance of my user ID was actually in some JavaScript that uses DiFi, and I thought that it would not be a good idea to use that as it looked as though that was only available when logged in, and would only allow one ID number to be used. I searched again and found an appropriate instance. The "first" instance appears in an HTML tag as below:

<div class="thought block" id="c759213177" typeid="4" itemid="1570401">

I needed to slice out the ID number, and I wasn't going to waste time searching for the whole tag in my Python code, so I decided only to search for 'itemid="' and '"' in that order. I made sure to confirm that this was also the first instance of itemid before doing so. On line 21 you can see the code used to slice out the ID number.

Seeing as I had a way to grab a user's ID number easily, I thought I should get on and do the part which actually gets a deviation ID number. Cue DiFi.

I needed the data returned by the DiFi request to be parsable, so returning it as xml was the best and only format to use without having to download alternate extensions for Python. So, the DiFi request I am using is below:


"t" refers to "type", so the code requests the data to be returned in a format type of "xml". Simple enough! But then I have to parse the xml.

The way python is parsed in Python makes it quite awkward to return the values without the xml tags attached, but simple slicing the strings in the right places seems to work fine.

Deviation ID numbers are wrapped in <ids></ids> in the returned code, but the numeric order of the deviation ID is also wrapped in the same way, which again, makes things awkward. It's easy to tell, however, that the deviation ID numbers are in even tags, and the order in odd tags. So the deviation ID numbers are stored in <ids> 2, 4, 6, 8, 10, 12 etc. To get around this, when generating a random number, I add 1 to it if the number is odd. This means I can get the right number without reading it to check.

Finally, I decided to put this code into a function so that it can be easily reused.

Thanks for reading! I hope you feel more knowledgeable about grabbing random deviation ID numbers through DiFi in Python!

If you want to read more about DiFi you can visit the botdom documentation Wiki to read our (limited) documentation on DiFi ;p

<EDIT id='1'>
I've changed it so that it uses the JSON returned by DiFi. This means no more parsing XML tags and all that stuff! I've also made it possible to return more than one thumb id number at a time, and all of them are returned in a list!

<EDIT id='2'>
I've changed the last bit of the code. There used to be around five lines of code, and I've reduced it to one line. Python is quite nifty methinks.

<EDIT id='3'>
It now works with Python 3.0! And I'm using the JSON decoder rather than eval. Using eval is naughty!

Add a Comment:

The Artist has requested Critique on this Artwork

Please sign up or login to post a critique.

OrrinFox Featured By Owner Mar 5, 2012
im gonna implement this into Komodo :B
photofroggy Featured By Owner Mar 11, 2012  Hobbyist Artist
Don't. This is terrible code that was made a long time ago. It is more than possible to get a better solution.
OrrinFox Featured By Owner Mar 11, 2012
actually now that im trying it out, some of it is not trying to work :lol: so yeah maybe your right
CaptainRedmuff Featured By Owner Jun 23, 2008
Very nice work photofroggy, i'd be interested to read the source for it all :)
photofroggy Featured By Owner Jun 23, 2008  Hobbyist Artist
That is all of it! It's only one function here =p What would you want to read the source code of?
CaptainRedmuff Featured By Owner Jun 23, 2008
oh lol :giggle:

I saw the other tab "" and assumed it was part of it :D

nvm :doh:
photofroggy Featured By Owner Jun 23, 2008  Hobbyist Artist
Oh, lol, that was a bot I was working on... meh
CodeIsArt Featured By Owner Jun 23, 2008
Cool, and very nice descriptions.
I've never used Python before personally, so it's good to see some.
But where's the art here? Your description lacks information about the challenge that was faced here and how you've solved this problem in an elegant, artful way? What hurdles have you overcome?

Secondly, you're assigning many temporary(?) variables. With some rewording (if the language allows), it could be shortened. For example, I would write:

xmlnode = minidom.parse (
    urllib.urlopen("...", 'POST')

Which would remove the need for `f`

Many thanks for posting this :)
photofroggy Featured By Owner Jun 23, 2008  Hobbyist Artist
Thanks for the feedback! I've updated the code itself, the screenshot, and the description, as per requested! I hope it's more fitting :)
Vayde Featured By Owner Jun 22, 2008
Nice noodling there son :#1:
photofroggy Featured By Owner Jun 22, 2008  Hobbyist Artist
Thanks :paranoid:
Add a Comment:

Featured in Collections


Submitted on
June 22, 2008
Image Size
77.5 KB


7 (who?)


Creative Commons License
Some rights reserved. This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.