cstephen / hashtag-count

Count hashtag occurrences over time using Twitter's Streaming API.
MIT License
7 stars 4 forks source link

Script exits on JSON parsing error #6

Open cstephen opened 7 years ago

cstephen commented 7 years ago

The following error caused my script to exit after running for ~3 weeks:

Error: Error parsing twitter reply: `89793","indices":[13,36],"media_url":"http:\/\/pbs.twimg.com\/ext_tw_video_thumb\/895412798070689793\/pu\/img\/M4Y57VvnZ40Z52rj.jpg","media_url_https":"https:\/\/pbs.twimg.com\/ext_tw_video_thumb\/895412798070689793\/pu\/img\/M4Y57VvnZ40Z52rj.jpg","url":"https:\/\/t.co\/sPQAK28OrF","display_url":"pic.twitter.com\/sPQAK28OrF","expanded_url":"https:\/\/twitter.com\/DiazKai\/status\/895412845818503170\/video\/1","type":"video","sizes":{"small":{"w":340,"h":604,"resize":"fit"},"large":{"w":720,"h":1280,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":1067,"resize":"fit"}},"video_info":{"aspect_ratio":[9,16],"duration_millis":6167,"variants":[{"bitrate":320000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/ext_tw_video\/895412798070689793\/pu\/vid\/180x320\/tYenM019Ngjn0n61.mp4"},{"bitrate":832000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/ext_tw_video\/895412798070689793\/pu\/vid\/360x640\/j0W-x7R89z8JW0ti.mp4"},{"bitrate":2176000,"content_type":"video\/mp4","url":"https:\/\/video.twimg.com\/ext_tw_video\/895412798070689793\/pu\/vid\/720x1280\/1cdqK9nqtd9pXiNV.mp4"},{"content_type":"application\/x-mpegURL","url":"https:\/\/video.twimg.com\/ext_tw_video\/895412798070689793\/pu\/pl\/NZfk3ZWWZ_pHusHU.m3u8"}]}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":true,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[{"url":"","expanded_url":null,"indices":[134,134]}],"user_mentions":[{"screen_name":"SouljaHoward","name":"\u3164\u3164\u3164*","id":323369295,"id_str":"323369295","indices":[3,16]}],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1502948583929"}`, error message `SyntaxError: Unexpected string in JSON at position 5`
    at Parser.parse (~/hashtag-count/node_modules/twit/lib/parser.js:44:28)
    at Gunzip.<anonymous> (~/hashtag-count/node_modules/twit/lib/streaming-api-connection.js:139:21)
    at emitOne (events.js:96:13)
    at Gunzip.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Gunzip.Readable.push (_stream_readable.js:134:10)
    at Gunzip.Transform.push (_stream_transform.js:128:32)
    at Zlib.callback (zlib.js:598:14)

hashtag-count needs to be modified to handle/ignore errors like this so they don't kill the process.

cstephen commented 7 years ago

Error events raised by the parser are already caught in the twit module and reemitted as parser-error events, so the error message above was not caused by an uncaught error event.

It looks like this chunk of code in hashtag-count.js is the culprit:

self.stream.on('parser-error', function (err) {
  self.error = err;
});

I had been using this chunk of code to catch Twitter authorization errors since they show up as parser-error events too. Setting self.error causes the script to exit later in hashtag-count.js:

async.until(
  function () {
    if (self.error !== undefined) {
      return true;
    }
    ...
  },
  function (next) {
    ...
  },
  function () {
    ...
  }
);

Again, this was done intentionally to exit on Twitter authorization errors. But we need to add some code to do something different when JSON parser errors are recognized. It's not worth quitting out of the script for something so minor.