plugins/twitter/twitter.js
function normalizeTwitterData(data,req,res){
  
  var normalized = {
    error: false,
    error_message: '',
    media: []
  }
  
  // console.dir(req.session.twitter)
  
  if( isTwitterOverCapacity() ){
    normalized.error = true
    normalized.error_message = "It appears Twitter is over capacity. Try again." 
  }
  else{
    
    console.log("Twitter Response array length: %s\n", data.length)
    // If the media response is empty...
    if(!data.length){
      normalized.error = true
      normalized.error_message = "We were unable to fetch any images from your media timeline."
      console.dir(data) 
      return processNormalizedResponse(normalized, req, res)
    }
    
    // We use a for loop so we can break out if
    // the cache is met
    for(i=0, len = data.length; i<len; i++){
      
      var el = data[i]

      // console.dir(el)
      // TODO: Session won't stash this much data so switch this logic to pull from redis.
      if( req.session.media_cache && isItemFirstCacheItem(el.id, req.session.media_cache.latest_id ) ){
        console.log('\n\nCache is same so we are breaking...\n\n')
        // update latest_id in cache
        normalized.latest_id = el.id
        // Now process the the response with new updated normalized data
        processNormalizedResponse(normalized, req, res)
        break
      }
      
      // The first time we run this, we need to stash the latest_id
      if( i === len-1 ){
        normalized.latest_id = data[0].id
      }
      
      // In the case of just pic.twitter.com....
      if(el.entities && el.entities.media && el.entities.media.length){

        var twitMediaObj = el.entities.media[0]

        var item = {
          full_url: twitMediaObj.media_url+":large",
          thumb_url: twitMediaObj.media_url+":thumb",
        }
        
        normalized.media.push(item)          

      }
      // Otherwise, we have 3rd part attributed providers.
      else if(el.entities && el.entities.urls && el.entities.urls.length && el.entities.urls[0].display_url){

        var url = el.entities.urls[el.entities.urls.length-1].display_url

        if(url.indexOf('instagr.am') > -1){

          var item = {
            full_url: "https://" + url + "/media/?size=l",
            thumb_url: "https://" + url + "/media/?size=t",
          }
          
          normalized.media.push(item)          
        
        }
        if(url.indexOf('twimg.com') > -1){
      
        // https://p.twimg.com/A128d2CCIAAPt--.jpg:small
        // https://p.twimg.com/A128d2CCIAAPt--.jpg:large
        
        }
        if(url.indexOf('flickr.com') > -1){
          /*
        
          <div class="thumbnail-wrapper" data-url="http://flickr.com/groups/dalekcakes">
            <img class="scaled-image" src="//farm7.static.flickr.com/6140/5917491915_8e92c65aa8.jpg"></div>
        
          */
        
          // WILL NEED TO FIGURE OUT FLICKR RESPONSE
        
          // Requires API call via photo ID, or just use twitter:
        
          /*
          https://widgets.platform.twitter.com/services/rest?jsoncallback=jQuery15205886323538143188_1346683837796&format=json&api_key=2a56884b56a00758525eaa2fee16a798&method=flickr.photos.getInfo&photo_id=7275880290&_=1346683837809
          */
        
          console.dir(el)
        }
        if(url.indexOf('twitpic.com') > -1){
      
          // POP OFF ID OF END OF TWITPIC URL
          // http://twitpic.com/9k68zj

          var url_id = url.split('com')[1] // includes slash

          var item = {
            full_url: "https://twitpic.com/show/iphone" + url_id,
            thumb_url: "https://twitpic.com/show/iphone" + url_id,
          }

          normalized.media.push(item)          
      
        }
        // if(url.indexOf('etsy.am') > -1){}

        if(url.indexOf('twitgoo.com') > -1){
          // http://twitgoo.com/66akxy/img

          var item = {
            full_url: url + '/img',
            thumb_url: url + '/img',
          }

          normalized.media.push(item)          

        }
        // Daily booth closed down....
        // if(url.indexOf('dailybooth.com') > -1){
        //   console.log(url)
        //   request({
        //     followRedirect: false,
        //     uri: 'http://'+url,
        //     callback: function(e,r,b){
                // need to handle errors better...
        //       if(e) console.error(e)
        //       
        //       console.dir(r)
        // 
        //       var id = r.headers.location.split('/').pop()
        //       
        //       request({
        //         uri: "https://api.dailybooth.com/v1/picture/" + id + ".json",
        //         json: true,
        //         callback: function(e,r,b){
        //           if(e) return console.error(e)
        //           /*
        //           urls: 
        //             { tiny: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/tiny/78e4b486c80b29435504d94fdf626b7c_29230840.jpg',
        //               small: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/small/78e4b486c80b29435504d94fdf626b7c_29230840.jpg',
        //               medium: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/medium/78e4b486c80b29435504d94fdf626b7c_29230840.jpg',
        //               large: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/large/78e4b486c80b29435504d94fdf626b7c_29230840.jpg' 
        //             }
        //           
        //           { error: 
        //              { error: 'rate_limit',
        //                error_description: 'Rate limit exceeded.',
        //                error_code: 412 } }
        //           var item = {
        //             full_url: b.urls.large + '/img',
        //             thumb_url: b.urls.small + '/img',
        //           }
        //           */
        //           
        //           if( (r.statusCode >= 400) || b.error) {
        //             console.log('Fail. '.red +'Status Code for Daily Booth request:  %s', r.statusCode)
        //             console.error(b.error)
        //           }
        //           else{
        //             
        //             var item = {
        //               full_url: b.urls.large,
        //               thumb_url: b.urls.small,
        //             }
        //             
        //             normalized.media.push(item)          
        //           }
        //           
        //           delay = false
        //           
        //           processNormalizedResponse(normalized, req, res)
        //           
        //         } // end inner request callback
        //       }) // end request()
        //     } // end outer request callback
        //   }) // end request()
        //   
        //   // Because of the async nature of the above request() calls,
        //   // we set the delay here and mark it false in the last callback
        //   if(!delay) delay = true
        //   
        // }
        if(url.indexOf('yfrog.com') > -1){
          // console.dir(el)

          var item = {
            full_url: "https://" + url + ":tw1",
            thumb_url: "https://" + url + ":tw1",
          }

          normalized.media.push(item)          
        
        }
        // if(url.indexOf('lockerz.am') > -1){}
        // if(url.indexOf('kiva.am') > -1){}

        // if(url.indexOf('kickstarter.com') > -1){}
        if(url.indexOf('dipdive.com') > -1){
          isVideo = false || true; // could be photo
        
        }
        // if(url.indexOf('photobucket.com') > -1){}
        // if(url.indexOf('with.me') > -1){}
        // if(url.indexOf('facebook.com') > -1){}
        if(url.indexOf('deviantart.com') > -1 || url.indexOf('fav.me') > -1){
        
          // TODO: IMAGES DON'T SHOW UP?
        
        }
      
      } // end if el.entities.url

    } // end forloop
    
  } // end else capacity
  
  return processNormalizedResponse(normalized, req, res)
  
}
function normalizeTwitterData(data,req,res){
  
  var normalized = {
    error: false,
    error_message: '',
    media: []
  }
  
  // console.dir(req.session.twitter)
  
  if( isTwitterOverCapacity() ){
    normalized.error = true
    normalized.error_message = "It appears Twitter is over capacity. Try again." 
  }
  else{
    
    console.log("Twitter Response array length: %s\n", data.length)
    // If the media response is empty...
    if(!data.length){
      normalized.error = true
      normalized.error_message = "We were unable to fetch any images from your media timeline."
      console.dir(data) 
      return processNormalizedResponse(normalized, req, res)
    }
    
    // We use a for loop so we can break out if
    // the cache is met
    for(i=0, len = data.length; i<len; i++){
      
      var el = data[i]

      // console.dir(el)
      // TODO: Session won't stash this much data so switch this logic to pull from redis.
      if( req.session.media_cache && isItemFirstCacheItem(el.id, req.session.media_cache.latest_id ) ){
        console.log('\n\nCache is same so we are breaking...\n\n')
        // update latest_id in cache
        normalized.latest_id = el.id
        // Now process the the response with new updated normalized data
        processNormalizedResponse(normalized, req, res)
        break
      }
      
      // The first time we run this, we need to stash the latest_id
      if( i === len-1 ){
        normalized.latest_id = data[0].id
      }
      
      // In the case of just pic.twitter.com....
      if(el.entities && el.entities.media && el.entities.media.length){

        var twitMediaObj = el.entities.media[0]

        var item = {
          full_url: twitMediaObj.media_url+":large",
          thumb_url: twitMediaObj.media_url+":thumb",
        }
        
        normalized.media.push(item)          

      }
      // Otherwise, we have 3rd part attributed providers.
      else if(el.entities && el.entities.urls && el.entities.urls.length && el.entities.urls[0].display_url){

        var url = el.entities.urls[el.entities.urls.length-1].display_url

        if(url.indexOf('instagr.am') > -1){

          var item = {
            full_url: "https://" + url + "/media/?size=l",
            thumb_url: "https://" + url + "/media/?size=t",
          }
          
          normalized.media.push(item)          
        
        }
        if(url.indexOf('twimg.com') > -1){
      
        // https://p.twimg.com/A128d2CCIAAPt--.jpg:small
        // https://p.twimg.com/A128d2CCIAAPt--.jpg:large
        
        }
        if(url.indexOf('flickr.com') > -1){
          /*
        
          <div class="thumbnail-wrapper" data-url="http://flickr.com/groups/dalekcakes">
            <img class="scaled-image" src="//farm7.static.flickr.com/6140/5917491915_8e92c65aa8.jpg"></div>
        
          */
        
          // WILL NEED TO FIGURE OUT FLICKR RESPONSE
        
          // Requires API call via photo ID, or just use twitter:
        
          /*
          https://widgets.platform.twitter.com/services/rest?jsoncallback=jQuery15205886323538143188_1346683837796&format=json&api_key=2a56884b56a00758525eaa2fee16a798&method=flickr.photos.getInfo&photo_id=7275880290&_=1346683837809
          */
        
          console.dir(el)
        }
        if(url.indexOf('twitpic.com') > -1){
      
          // POP OFF ID OF END OF TWITPIC URL
          // http://twitpic.com/9k68zj

          var url_id = url.split('com')[1] // includes slash

          var item = {
            full_url: "https://twitpic.com/show/iphone" + url_id,
            thumb_url: "https://twitpic.com/show/iphone" + url_id,
          }

          normalized.media.push(item)          
      
        }
        // if(url.indexOf('etsy.am') > -1){}

        if(url.indexOf('twitgoo.com') > -1){
          // http://twitgoo.com/66akxy/img

          var item = {
            full_url: url + '/img',
            thumb_url: url + '/img',
          }

          normalized.media.push(item)          

        }
        // Daily booth closed down....
        // if(url.indexOf('dailybooth.com') > -1){
        //   console.log(url)
        //   request({
        //     followRedirect: false,
        //     uri: 'http://'+url,
        //     callback: function(e,r,b){
                // need to handle errors better...
        //       if(e) console.error(e)
        //       
        //       console.dir(r)
        // 
        //       var id = r.headers.location.split('/').pop()
        //       
        //       request({
        //         uri: "https://api.dailybooth.com/v1/picture/" + id + ".json",
        //         json: true,
        //         callback: function(e,r,b){
        //           if(e) return console.error(e)
        //           /*
        //           urls: 
        //             { tiny: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/tiny/78e4b486c80b29435504d94fdf626b7c_29230840.jpg',
        //               small: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/small/78e4b486c80b29435504d94fdf626b7c_29230840.jpg',
        //               medium: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/medium/78e4b486c80b29435504d94fdf626b7c_29230840.jpg',
        //               large: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/large/78e4b486c80b29435504d94fdf626b7c_29230840.jpg' 
        //             }
        //           
        //           { error: 
        //              { error: 'rate_limit',
        //                error_description: 'Rate limit exceeded.',
        //                error_code: 412 } }
        //           var item = {
        //             full_url: b.urls.large + '/img',
        //             thumb_url: b.urls.small + '/img',
        //           }
        //           */
        //           
        //           if( (r.statusCode >= 400) || b.error) {
        //             console.log('Fail. '.red +'Status Code for Daily Booth request:  %s', r.statusCode)
        //             console.error(b.error)
        //           }
        //           else{
        //             
        //             var item = {
        //               full_url: b.urls.large,
        //               thumb_url: b.urls.small,
        //             }
        //             
        //             normalized.media.push(item)          
        //           }
        //           
        //           delay = false
        //           
        //           processNormalizedResponse(normalized, req, res)
        //           
        //         } // end inner request callback
        //       }) // end request()
        //     } // end outer request callback
        //   }) // end request()
        //   
        //   // Because of the async nature of the above request() calls,
        //   // we set the delay here and mark it false in the last callback
        //   if(!delay) delay = true
        //   
        // }
        if(url.indexOf('yfrog.com') > -1){
          // console.dir(el)

          var item = {
            full_url: "https://" + url + ":tw1",
            thumb_url: "https://" + url + ":tw1",
          }

          normalized.media.push(item)          
        
        }
        // if(url.indexOf('lockerz.am') > -1){}
        // if(url.indexOf('kiva.am') > -1){}

        // if(url.indexOf('kickstarter.com') > -1){}
        if(url.indexOf('dipdive.com') > -1){
          isVideo = false || true; // could be photo
        
        }
        // if(url.indexOf('photobucket.com') > -1){}
        // if(url.indexOf('with.me') > -1){}
        // if(url.indexOf('facebook.com') > -1){}
        if(url.indexOf('deviantart.com') > -1 || url.indexOf('fav.me') > -1){
        
          // TODO: IMAGES DON'T SHOW UP?
        
        }
      
      } // end if el.entities.url

    } // end forloop
    
  } // end else capacity
  
  return processNormalizedResponse(normalized, req, res)
  
}
function normalizeTwitterData ( data , req , res ) { var normalized = { error : false , error_message : '' , media : [ ] } // console.dir(req.session.twitter) if ( isTwitterOverCapacity ( ) ) { normalized . error = true normalized . error_message = "It appears Twitter is over capacity. Try again." } else { console . log ( "Twitter Response array length: %s\n" , data . length ) // If the media response is empty... if ( ! data . length ) { normalized . error = true normalized . error_message = "We were unable to fetch any images from your media timeline." console . dir ( data ) return processNormalizedResponse ( normalized , req , res ) } // We use a for loop so we can break out if // the cache is met for ( i = 0 , len = data . length ; i < len ; i ++ ) { var el = data [ i ] // console.dir(el) // TODO: Session won't stash this much data so switch this logic to pull from redis. if ( req . session . media_cache && isItemFirstCacheItem ( el . id , req . session . media_cache . latest_id ) ) { console . log ( '\n\nCache is same so we are breaking...\n\n' ) // update latest_id in cache normalized . latest_id = el . id // Now process the the response with new updated normalized data processNormalizedResponse ( normalized , req , res ) break } // The first time we run this, we need to stash the latest_id if ( i === len - 1 ) { normalized . latest_id = data [ 0 ] . id } // In the case of just pic.twitter.com.... if ( el . entities && el . entities . media && el . entities . media . length ) { var twitMediaObj = el . entities . media [ 0 ] var item = { full_url : twitMediaObj . media_url + ":large" , thumb_url : twitMediaObj . media_url + ":thumb" , } normalized . media . push ( item ) } // Otherwise, we have 3rd part attributed providers. else if ( el . entities && el . entities . urls && el . entities . urls . length && el . entities . urls [ 0 ] . display_url ) { var url = el . entities . urls [ el . entities . urls . length - 1 ] . display_url if ( url . indexOf ( 'instagr.am' ) > - 1 ) { var item = { full_url : "https://" + url + "/media/?size=l" , thumb_url : "https://" + url + "/media/?size=t" , } normalized . media . push ( item ) } if ( url . indexOf ( 'twimg.com' ) > - 1 ) { // https://p.twimg.com/A128d2CCIAAPt--.jpg:small // https://p.twimg.com/A128d2CCIAAPt--.jpg:large } if ( url . indexOf ( 'flickr.com' ) > - 1 ) { /*
        
          <div class="thumbnail-wrapper" data-url="http://flickr.com/groups/dalekcakes">
            <img class="scaled-image" src="//farm7.static.flickr.com/6140/5917491915_8e92c65aa8.jpg"></div>
        
          */ // WILL NEED TO FIGURE OUT FLICKR RESPONSE // Requires API call via photo ID, or just use twitter: /*
          https://widgets.platform.twitter.com/services/rest?jsoncallback=jQuery15205886323538143188_1346683837796&format=json&api_key=2a56884b56a00758525eaa2fee16a798&method=flickr.photos.getInfo&photo_id=7275880290&_=1346683837809
          */ console . dir ( el ) } if ( url . indexOf ( 'twitpic.com' ) > - 1 ) { // POP OFF ID OF END OF TWITPIC URL // http://twitpic.com/9k68zj var url_id = url . split ( 'com' ) [ 1 ] // includes slash var item = { full_url : "https://twitpic.com/show/iphone" + url_id , thumb_url : "https://twitpic.com/show/iphone" + url_id , } normalized . media . push ( item ) } // if(url.indexOf('etsy.am') > -1){} if ( url . indexOf ( 'twitgoo.com' ) > - 1 ) { // http://twitgoo.com/66akxy/img var item = { full_url : url + '/img' , thumb_url : url + '/img' , } normalized . media . push ( item ) } // Daily booth closed down.... // if(url.indexOf('dailybooth.com') > -1){ //   console.log(url) //   request({ //     followRedirect: false, //     uri: 'http://'+url, //     callback: function(e,r,b){ // need to handle errors better... //       if(e) console.error(e) //        //       console.dir(r) //  //       var id = r.headers.location.split('/').pop() //        //       request({ //         uri: "https://api.dailybooth.com/v1/picture/" + id + ".json", //         json: true, //         callback: function(e,r,b){ //           if(e) return console.error(e) //           /* //           urls:  //             { tiny: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/tiny/78e4b486c80b29435504d94fdf626b7c_29230840.jpg', //               small: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/small/78e4b486c80b29435504d94fdf626b7c_29230840.jpg', //               medium: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/medium/78e4b486c80b29435504d94fdf626b7c_29230840.jpg', //               large: 'http://d1oi94rh653f1l.cloudfront.net/15/pictures/large/78e4b486c80b29435504d94fdf626b7c_29230840.jpg'  //             } //            //           { error:  //              { error: 'rate_limit', //                error_description: 'Rate limit exceeded.', //                error_code: 412 } } //           var item = { //             full_url: b.urls.large + '/img', //             thumb_url: b.urls.small + '/img', //           } //           */ //            //           if( (r.statusCode >= 400) || b.error) { //             console.log('Fail. '.red +'Status Code for Daily Booth request:  %s', r.statusCode) //             console.error(b.error) //           } //           else{ //              //             var item = { //               full_url: b.urls.large, //               thumb_url: b.urls.small, //             } //              //             normalized.media.push(item)           //           } //            //           delay = false //            //           processNormalizedResponse(normalized, req, res) //            //         } // end inner request callback //       }) // end request() //     } // end outer request callback //   }) // end request() //    //   // Because of the async nature of the above request() calls, //   // we set the delay here and mark it false in the last callback //   if(!delay) delay = true //    // } if ( url . indexOf ( 'yfrog.com' ) > - 1 ) { // console.dir(el) var item = { full_url : "https://" + url + ":tw1" , thumb_url : "https://" + url + ":tw1" , } normalized . media . push ( item ) } // if(url.indexOf('lockerz.am') > -1){} // if(url.indexOf('kiva.am') > -1){} // if(url.indexOf('kickstarter.com') > -1){} if ( url . indexOf ( 'dipdive.com' ) > - 1 ) { isVideo = false || true ; // could be photo } // if(url.indexOf('photobucket.com') > -1){} // if(url.indexOf('with.me') > -1){} // if(url.indexOf('facebook.com') > -1){} if ( url . indexOf ( 'deviantart.com' ) > - 1 || url . indexOf ( 'fav.me' ) > - 1 ) { // TODO: IMAGES DON'T SHOW UP? } } // end if el.entities.url } // end forloop } // end else capacity return processNormalizedResponse ( normalized , req , res ) }
Let's normalize the response of media data to only include the photos we want.
Let s normalize the response of media data to only include the photos we want .