Tweepy: Search in stream api - twitter

In Tweepy api for twitter I know we can search tweets by -
api.search(q="a and b")
Here it will search for both a and b appearing anywhere in the status in any order. However I need to do same for the Tweepy stream api, Is there any way to do that?
I know there is track field -
stream.filter(track=['a','b'])
But this would return status containing either of a or b, I need both of the keywords in any order.
We could also do like, search for only a then manually filtering out status which contain b, but then we would be discarding huge no. of tweets as stream api gives only 1% of the tweets

Yes, this can be done easily. Looking at the docs for the Twitter API track paramenter:
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. For example, ‘the twitter’ is (the AND twitter), and ‘the,twitter’ is (the OR twitter).
By this logic, to filter by a and b:
stream.filter(track=['a b'])

Related

How to track and stream tweets for keywords with AND operator using phirehose library?

I am trying to connect to streaming API of twitter and retrieve tweets keywords using specific keywords. I am using the phirehose library for the same. It says in the twitter documentation that "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)."
But I want to search for keywords with AND operator even if there are other words in between. Meaning if we want to search for tweets having Keyword1 AND Keyword2, tweets which have only one keyword should not be retrieved.
Using the settrack function of the phirehose library -
setTrack(array('the , twitter'));
retrieves tweets with either the OR twitter while
setTrack(array('the twitter'));
retrieves tweets with the phrase the twitter and does not retrieve tweets like the busy twitter for example.
Please help.
140dev by Adam Green gives a solution for this by using ``typeenum('words','phrase') NOT NULL DEFAULT 'words'
Please see - http://140dev.com/twitter-api-programming-blog/streaming-api-enhancements-part-2-keyword-collection-database-changes/ and
http://140dev.com/twitter-api-programming-blog/streaming-api-enhancements-part-3-collecting-tweets-based-on-table-of-keywords/

Twitter API - Search latin special characters

How can I perform a search in Twitter API that matches with special chars like Ñ or Ç?
Even web search seems to be confused with those characters
Ex:
https://twitter.com/search?q=u%C3%B1as&src=typd
It returns a list of tweets matching with 'unas' and 'uñas', and the same result is returned if an API search is performed
And it happens in inverse way, if you search 'barca' twitter will return tweets matching with 'barça' and 'barca'
The only way I found to ensure that the keyword, and only the keyword, is in tweets, is a programming filtering when results are fetched. Is there any better way?

Youtube API automatically filters out "alphabet/number-added to search string"?

Let's say I search Youtube API for "Metric"
But I don't want search results like "econometrics" "metrics"
So I mean NO alphabet or number should be added to the left or right to the search string.
A space can be put to the left or right to the search string, like "Fun Metric course".
I was thinking about this.
But interestingly, when I search Youtube website (not API), if I search for "Metric", then only search results with "Metric" but no "econometrics" are displayed.
Another example: If I search for "microecon", then no search results with "microeconomics" are displayed.
Does Youtube website automatically filters out such words (like econometrics which have alphabets added to original search string "metric") even if I don't ask it to do so?
Similarly, does Youtube API (NOT website) automatically filters out such words (like econometrics which have alphabets added to original search string "metric") even if I don't ask it to do so??
It may be different for api version 3, but the documentation only refers to "The q parameter specifies the query term to search for".
But there is some brief documentation for this q paramter in api version 2.
See:https://developers.google.com/youtube/2.0/reference#qsp
Since it's very brief, here it is also:
The q parameter specifies a search query term. YouTube will search all video metadata for videos matching the term. Video metadata includes titles, keywords, descriptions, authors' usernames, and categories.
Note that any spaces, quotes or other punctuation in the parameter value must be URL-escaped.
To search for an exact phrase, enclose the phrase in quotation marks. For example, to search for videos matching the phrase "spy plane", set the q parameter to %22spy+plane%22.
Your request can also use the Boolean NOT (-) and OR (|) operators to exclude videos or to find videos that are associated with one of several search terms. For example, to search for videos matching either "boating" or "sailing", set the q parameter to boating%7Csailing. (Note that the pipe character must be URL-escaped.)
Similarly, to search for videos matching either "boating" or "sailing" but not "fishing", set the q parameter to boating&7Csailing+-fishing.

Searching two channels from youtube

I am trying to search for "Food+Show" from two youtube channels. ABCNetwork and FoxBroadcasting. The query I gave is
http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=Food+Show&max-results=3&authors=ABCNetwork,FoxBroadcasting&prettyprint=true
The first result I got was id UKfLsIgJB1g where uploader is wafelsanddinges and not ABC or Fox. Please tell me why my query is not retuning correct result.
The parameter for the v2 data API is "author," not "authors." Unfortunately, fixing that won't solve the problem, as the retrieval of videos from a particular channel can only accept one author at a time. This is also true for v3 of the API.
The reason behind this is that the comma is treated as a concatenator, looking for a video that was published on FoxBroadcasting AND ABCNetwork (the use case for having multiple authors in that parameter is if you are retrieving activity feeds, in which case you want both feeds so having the comma serve as an AND is correct).
So for now, the only solution is two separate calls.

Retrieve most retweeted tweets for a given hashtag

I'd like to retrieve the tweets for given a hashtag and sort them from the most retweeted to the less retweeted.
The closest thing I've found is using the search call and use the type tag:
E.g.: http://search.twitter.com/search.json?q=TheHashTagHere&result_type=popular
However, I'm not sure on how "popular" option works.
For instance, if it finds 100 tweets with that hashtag I believe it should show the X most retweeted tweets, and if none of those tweets have been retweeted then it should show X of them randomly (or sorted in some other way like the most recent).
Unfortunately, if follows some kind of unknown rule to identify what's popular and what not and even hashtags with thousands of tweets might return only one or two results.
I hope I made myself clear. Thanks in advance :)
PS: I'll use PHP but I think that shouldn't affect the question?
Results will sometimes contain a
result_type field into the metadata
with a value of either "recent" or
"popular". Popular results are derived
by an algorithm that Twitter computes,
and up to 3 will appear in the default
mixed mode that the Search API
operates under. Popular results
include another node in the metadata
called recent_retweets. This field
indicates how many retweets the Tweet
has had.
Source (Emphasis are mine)
Just call with result_type=popular and check the recent_retweets node to see how popular it is. result_type=popular will become the default in an upcome release so beware if you omit this parameter.
Results with popular tweets aren't ordered chronologically. *
If you would like to always have results to show, use result_type=mixed: they will have the result_type in the "metadata" section with a value of "recent", and popular results will have "popular". A small reference about result_types:
mixed: Include both popular and real time results in the response.
recent: return only the most recent results in the response
popular: return only the most popular results in the response.
If a search query has any popular results, those will be returned at the top, even if they are older than the other results. *
*[Twitter API Announcements]
This isn't a programmatic method but rather works in the browser with a chrome extension (HackyBird) :
Install the extension
Search for a phrase e.g. #Social (twitter.com/search?q=%23Social)
Click the extension to sort it (you can adjust the ratio of retweets/likes used for sorting in extension options).
P.S. It'll also sort your or any other user's timeline.

Resources