ehime
ehime

Reputation: 8375

Issues with parseJson when scraping instagram

I'm having issues with an unrecognized expression error while trying to convert a json text glob to a json object in my cors application. It looks like its having an issue with syntax but this linted ok. Can someone tell me what I'm missing?

<script  type="text/javascript">
  $(function()
  {
    /**
     * Test URI: http://localhost?url=//instagram.com/p/gPFGUruPP4&app=callback_#
     */
    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        return (null===results) ? null : results[1] || 0
    }

    if ($.urlParam('url') && $.urlParam('app')){
      var parts = {
        url: decodeURIComponent($.urlParam('url').replace('http://', '//')),
        app: "http://tool.throa.com/approve/" + $.urlParam('app')
      };

      $.ajaxPrefilter(function(options) {
        if(options.crossDomain && $.support.cors) {
          var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
          options.url = "http://cors.corsproxy.io/url=" + options.url;
        }
      });

      var cors = $.ajax({type: "GET", url: parts.url, async: false}).responseText;

      $(cors + ':contains("script")').each(function(){
           if($(this).children().length < 1 && $.trim($(this).text()).length != ''){
              var text = $(this).text();
              if (text.match(/^window._sharedData/) != null){
                var json = $.parseJson(text.replace('window._sharedData = ','').slice(0,-1));
                console.dir(json); // empty
              }
           } 
      });
    }
  });
</script>

complete json response per request

{"entry_data":{"DesktopPPage":[{"canSeePrerelease":false,"viewer":null,"media":{"code":"gPFGUruPP4","date":1383445488.0,"usertags":{"nodes":[{"position":{"y":0.59074074,"x":0.6027778},"user":{"username":"circuit_theory"}}]},"comments":{"nodes":[{"text":"@circuit_theory I got the first one to try prototyping with, two more otw. We just need the copies now","viewer_can_delete":false,"id":"580714412939735991","user":{"username":"ehimeprefecture","profile_pic_url":"http:\/\/images.ak.instagram.com\/profiles\/profile_253293643_75sq_1352937431.jpg"}},{"text":"great! I am going to check out the yours widebody kit tgis week","viewer_can_delete":false,"id":"580944641012593138","user":{"username":"circuit_theory","profile_pic_url":"http:\/\/photos-e.ak.instagram.com\/hphotos-ak-xpf1\/10375745_240330439497996_286604756_a.jpg"}}]},"caption":"Toys from Japan #s30 #240z #nismo #works","likes":{"count":2,"viewer_has_liked":false,"nodes":[{"user":{"username":"dukes71","profile_pic_url":"http:\/\/images.ak.instagram.com\/profiles\/profile_179270872_75sq_1340040612.jpg"}},{"user":{"username":"mushakes14","profile_pic_url":"http:\/\/photos-b.ak.instagram.com\/hphotos-ak-xpa1\/925577_1444243859169113_1542103582_a.jpg"}}]},"owner":{"username":"ehimeprefecture","requested_by_viewer":false,"profile_pic_url":"http:\/\/images.ak.instagram.com\/profiles\/profile_253293643_75sq_1352937431.jpg","id":"253293643","followed_by_viewer":false},"is_video":false,"id":"580705301711877112","display_src":"http:\/\/photos-a.ak.instagram.com\/hphotos-ak-xap1\/1389505_219275871583688_142904055_n.jpg"},"__get_params":{},"staticRoot":"\/\/d36xtkk24g8jdx.cloudfront.net\/bluebar\/29e365f","__query_string":"?","prerelease":false,"__path":"\/p\/gPFGUruPP4\/","shortcode":"gPFGUruPP4"}]},"hostname":"instagram.com","config":{"csrf_token":"ec1bf60a3aa673f7e637e2cd317e85c8"},"static_root":"\/\/d36xtkk24g8jdx.cloudfront.net\/bluebar\/29e365f"}

Upvotes: 1

Views: 851

Answers (1)

Shahar
Shahar

Reputation: 1677

The JSON seems fine. The problem is with the statement:

$(json).parseJson();

The $ makes the variable into a jQuery variable (a jQuery "object"). However, the parseJSON() method is a "static" method, it's not an "object" method (of course not the correct terminology but easier to think this way). In other words, it is supposed to be:

$.parseJSON(json);

The $ is the same as jQuery or "the class name" (wrong terminology again). Not to mention that you forgot the capital letters ^_^.

Upvotes: 2

Related Questions