Question

I'm attempting to copy all of the actual content from my Tumblr blog using a script I wrote on a different web page, but I'm having a bit of trouble with gaining access to the content. My ajax call is as follows:

$.ajax({
     url: "http://solacingsavant.tumblr.com/",
     dataType: 'jsonp',
     success: function(data) {
          var elements = $("<div>").html(data)[0].getElementsByTagName("ul")[0].getElementsByTagName("li");
          for(var i = 0; i < elements.length; i++) {
               var theText = elements[i].firstChild.nodeValue;
               alert(theText); // Alert if I got something
              // This is where I'll strip the data for the items I want
          }
     }
});

but as it is the console gives me an error of "Resource interpreted as Script but transferred with MIME type text/html" which I looked into here and changed the corresponding meta tag in the HTML of my blog to <meta http-equiv="Content-Type" content="application/javascript; charset=utf-8" /> with no success

I also tried using dataType: 'html' (which makes more sense to me) but I was getting a console error of "Origin is not allowed by Access-Control-Allow-Origin" which I also looked into and added a meta tag to my Tumblr blog with <meta Access-Control-Allow-Origin="*" />, but again didn't succeed

Here is a jsFiddle to work with

Does my approach not work because Tumblr as a whole does not allow changes to Access-Control? If so, how might I work around the issue? If not, what am I doing wrong?

MAJOR EDIT (based on mikedidthis's helpful comments)

It seems that I am not able to do this without a Tubmlr API, so I obtained an API key and now have access to the json results that the API sends out. I am able to get a jsonp object using the API key to in the console. My javascript at the moment:

$.ajax({
    url: "http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/info?api_key=APIkeyGoesHeRe",
    dataType: 'jsonp',
    success: function(results){
        console.log(results); 
        // Get data from posts here
    }
});

This SO post was helpful in understanding how I can change data on my Tubmlr page from the source and find out basic information about the site, but not about how to obtain actual data from individual posts. I tried looking through the results object and was unable to find any data related to posts, nor was I able to append the results to the jsfiddle. So my questions now are, "Can I copy data (say the written text in a post) from individual posts using this approach? If so, how? If not, what other approach should I use?"

Was it helpful?

Solution

A really quick answer

The tumblr API documentation really covers using the API well, however, to give you a little start, lets grab all your Text Posts.

First you need to query the API for any of your post that are of the type Text.

The documentation states (http://www.tumblr.com/docs/en/api/v2#posts) that we should use the following url and specifying the type which we you will set to text:

api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts[/type]

And below is an example based on the OP fiddle.

$.ajax({
    url: "http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts/text?api_key=XXXXXXX",
    dataType: 'jsonp',
    success: function(data){
        posts = data.response.posts
        $.each(posts, function(i) {
            console.log( posts[i].title, posts[i].body )
        });
    }
});

So for each query of the API, we will receive back an object. You will need to filter this object to get the data you want from it.

In context of the post queries, you can get directly at your posts using data.response.posts object.

To find out what data is available for each post type, the documentation has it covered: http://www.tumblr.com/docs/en/api/v2#text-posts

To the content for each of the Text post types, you need to loop through the posts object and then grab the value for the key named title and body.

Example here: http://jsfiddle.net/ZpFwL/

Bonus Time It is possible to get posts for all types, by dropping the type from the URL:

http://api.tumblr.com/v2/blog/solacingsavant.tumblr.com/posts/?api_key=XXXXXXX"

Remember this is a really, quick example and not for the real world.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top