Streaming API always returns 0 or 'undefined'


#1

I’m trying to get the “tweet.entities.media[0].media_url”, but no matter what its ‘undefined’ or 0!! How can I get this without getting these empty values!!!


#2

Are you able to provide any sample code demonstrating the issue? Do you have a tweet ID for a tweet which you think should contain that value, and have you checked the JSON structure using e.g. twurl?


#3
 if (tweet.user.id === 1583116764) { // twitter id

        //var twitMedia = tweet.entities.media;
        //var imgUrl = twitMedia[0].media_url;
        //console.log(imgUrl);

        var flattened = us.flatten(([tweet.entities, [tweet.entities.media], [tweet.entities.media[0], [[tweet.entities.media[0].media_url]]]]));  // using underscore to flatten the object since it always returns undefined
        var img;
        //console.log(flattened);

        if (flattened) {

            if (tweet.entities.media[0].media_url) {
                img = flattened[1].media_url;

                var blah = request.get(img).pipe(fs.createWriteStream(__dirname + '/' + 'public/img/test.jpg', []));
                
                console.log(blah);

            } else {
                img = undefined;
                console.log('its undefined');

            }

        }

    }

Output:

[ { hashtags: [ [Object] ],
    trends: [],
    urls: [],
    user_mentions: [],
    symbols: [],
    media: [ [Object] ] },
  { id: 522228566323245060,
    id_str: '522228566323245056',
    indices: [ 6, 28 ],
    media_url: 'http://pbs.twimg.com/media/Bz9UHTtCMAAAe7-.jpg',
    media_url_https: 'https://pbs.twimg.com/media/Bz9UHTtCMAAAe7-.jpg',
    url: 'http://t.co/esE8BhrZE6',
    display_url: 'pic.twitter.com/esE8BhrZE6',
    expanded_url: 'http://twitter.com/NousAcademy/status/522228567049261056/photo/1',
    type: 'photo',
    sizes: 
     { medium: [Object],
       small: [Object],
       thumb: [Object],
       large: [Object] } },
  { id: 522228566323245060,
    id_str: '522228566323245056',
    indices: [ 6, 28 ],
    media_url: 'http://pbs.twimg.com/media/Bz9UHTtCMAAAe7-.jpg',
    media_url_https: 'https://pbs.twimg.com/media/Bz9UHTtCMAAAe7-.jpg',
    url: 'http://t.co/esE8BhrZE6',
    display_url: 'pic.twitter.com/esE8BhrZE6',
    expanded_url: 'http://twitter.com/NousAcademy/status/522228567049261056/photo/1',
    type: 'photo',
    sizes: 
     { medium: [Object],
       small: [Object],
       thumb: [Object],
       large: [Object] } },
  'http://pbs.twimg.com/media/Bz9UHTtCMAAAe7-.jpg' ]

I’m able to download the image locally, but its always ‘undefined’ my writable stream gives me this:

{ _writableState: 
   { highWaterMark: 16384,
     objectMode: false,
     needDrain: false,
     ending: false,
     ended: false,
     finished: false,
     decodeStrings: true,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     sync: true,
     bufferProcessing: false,
     onwrite: [Function],
     writecb: null,
     writelen: 0,
     buffer: [],
     errorEmitted: false },
  writable: true,
  domain: null,
  _events: 
   { finish: { [Function: g] listener: [Function] },
     drain: [Function: ondrain],
     error: [Function: onerror],
     close: [Function: cleanup] },
  _maxListeners: 10,
  path: '/Users/boss/NetBeansProjects/project/app/public/img/test.jpg',
  fd: null,
  flags: 'w',
  mode: 438,
  start: undefined,
  pos: undefined,
  bytesWritten: 0 }

#4

I’m misunderstanding something here, apologies.

Your code grabs a tweet JSON object, flattens it using underscore, pulls out the media URL (successfully) into the img var, and then grabs the image via HTTP using the request module, and writes the data to disk.

When I run the code you’ve provided here I don’t see img being reported as undefined. I agree that when you log blah itself that it contains start and pos as undefined, but at that point the writeable stream is flushed to disk, so that makes sense, right?

What am I missing?

This code (replacing your flattened if block) would get you the image data back logged to console instead of saved to disk via the writeable stream:

if (tweet.entities.media[0].media_url) {
        img = flattened[1].media_url;
        request.get(img).on('response', function (response) {
          response.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
            });
            });
        }

Where is the issue you’re seeing with the Twitter Streaming API exactly?


#5

Correct it downloads it, but when I try to turn it into a readable stream it gives me undefined or says that the file doesnt exist. When I put a callback it just returns undefined. My main problem is writing it to disk and turning it into a readable stream.


#6

OK - I don’t think this is the correct place to find assistance in this case, as it is not an issue with the Twitter API - the tweet object is being returned correctly and you’re picking up the media URL successfully.

Maybe try Stack Overflow for some Javascripting support? My guess is that you need to look at how you’re using the request library, and see whether you can pipe the data to more than one location (disk and memory).


#7

Ok thanks for your help.