draftbit / twitter-lite

A tiny, full-featured, flexible client / server library for the Twitter API
https://npm.im/twitter-lite
MIT License
794 stars 95 forks source link

Tweets don't meet track if location parameter is set #26

Closed griffobeid closed 6 years ago

griffobeid commented 6 years ago

I'm experiencing an issue in which the tweets that are being streamed do not meet the track that I feed the stream when I set the location parameter. I have a bounding box around the continental United States and a track of 77 words. When I remove the locations parameter everything works fine, but with the locations parameter set the stream seems to give me any tweet within the boundaries. Is anybody else having this problem? I had the same issue with the node-twitter and that is what brought me here.

tylerthebuildor commented 6 years ago

Would you set me up with all of the parameters your using @griffobeid? I can test some stuff out and get back to you.

griffobeid commented 6 years ago

@tylerbuchea Here's my parameters. const streamParams = { track: 'atmosphere,barometer,blizzard,breeze,climate,cloudy,condensation,cumulus,dewpoint,disturbance,downburst,downdraft,drizzle,drought,flash flood,flood,flurry,fog,forecast,freeze,frost,funnel cloud,global warming,greenhouse effect,gust,hail,heat,humid,humidity,hurricane,hydrologic cycle,hydrosphere,ice,lake effect,lightning,meteorologist,meteorology,monsoon,overcast,ozone,permafrost,polar,precipitation,prevailing wind,radar,rain,rainbow,shower,sky,sleet,smog,snow,snowfall,snowflake,snowstorm,storm,temperate,temperature,thermal,thunder,thunderstorm,warning,tstorm,t-storm,tornado,tornado warning,tropical,troposphere,turbulence,updraft,visibility,vortex,warm,weather,whiteout,wind,windchill,wind chill factor', locations: '-124.9257514528,23.6428469943,-66.5614257446,49.1686071266', // Bounding box - United States };

Passing them into the stream like so: client.stream('statuses/filter', streamParams)

I appreciate the quick response!

tylerthebuildor commented 6 years ago

You're welcome @griffobeid and thanks for the info. Hopefully I'll have an answer for you EOD today. Cheers!

tylerthebuildor commented 6 years ago

Sorry it took so long @griffobeid at first I was trying to debug your parameters locally then I realized it might be a Twitter API limitation and it looks like I was right.

Apparently the Twitter API doesn't allow you to filter with locations AND track. Both of your filters are working but the Twitter Streaming API can only process them as locations OR track and they have no intention of adding the "AND" functionality to the Streaming API as you can read in this official discussion

There are two proposed solutions:

  1. get all tweets with a global location range, then filter the text content on my search term.
  2. get all tweets for the term (track), then filter out those with a location.

I realize neither of these are good solutions but it is all we have until we implement the new Account Activity API which sounds like might be capable of what you're trying to do.

tylerthebuildor commented 6 years ago

I created an open issue #27 so you can subscribe to it if you'd like and keep up with our progress on implementing the new API.

I'm going to close this issue for the time being but if you find any new information regarding this please feel free to ping me and we can reopen it.

Also if you're feeling motivated we'd love a PR for Account Activity API support 😄

griffobeid commented 6 years ago

No problem, @tylerbuchea. Thanks for figuring that out for me. That really is an unfortunate limitation. I'll look into implementing the Account Activity API today if I get a chance. :grinning:

tylerthebuildor commented 6 years ago

To add on to this you can use AND for multiple values on the tracking parameter this doesn't solve your problem of doing track AND location, but I thought I would drop this here because it might be useful for some people.

Example:

// OR (commas)
client.stream("statuses/filter", { track: "#bitcoin,#litecoin,#monero" })
  .on("data", data => console.log("data", data.text)) // tweets with any three hashtags

// AND (spaces)
client.stream("statuses/filter", { track: "#bitcoin #litecoin #monero" }))
  .on("data", data => console.log("data", data.text)) // only tweets with all three hashtags

A comma-separated list of phrases which will be used to determine what Tweets will be delivered on the stream. A phrase may be one or more terms separated by spaces, and a phrase will match if all of the terms in the phrase are present in the Tweet, regardless of order and ignoring case. By this model, you can think of commas as logical ORs, while spaces are equivalent to logical ANDs (e.g. ‘the twitter’ is the AND twitter, and ‘the,twitter’ is the OR twitter).

https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/basic-stream-parameters

Also I just wanted to write this down so I don't forget myself. I should probably add it to the docs.