diff --git a/doc/changelog.rst b/doc/changelog.rst index cb3464f0..b16eb615 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -99,3 +99,5 @@ ______________ * Updated examples, specifically ``examples/twitter-to-xhtml.py``, ``examples/view_friends.py``, ``examples/shorten_url.py`` * Updated ``get_access_token.py`` script to be python3 compatible. + +* :py:func:`twitter.api.Api.GetStreamFilter()` now accepts an optional languages parameter as a list. diff --git a/examples/streaming/track_users.py b/examples/streaming/track_users.py index fab36d55..faa37565 100644 --- a/examples/streaming/track_users.py +++ b/examples/streaming/track_users.py @@ -16,9 +16,9 @@ # ---------------------------------------------------------------------- -# This file demonstrates how to track mentions of a specific set of users and -# archive those mentions to a local file. The output file will contain one -# JSON string per line per Tweet. +# This file demonstrates how to track mentions of a specific set of users in +# english language and archive those mentions to a local file. The output +# file will contain one JSON string per line per Tweet. # To use this example, replace the W/X/Y/Zs with your keys obtained from # Twitter, or uncomment the lines for getting an environment variable. If you @@ -52,6 +52,10 @@ '@twitterapi', '@support'] +# Languages to filter tweets by is a list. This will be joined by Twitter +# to return data mentioning tweets only in the english language. +LANGUAGES = ['en'] + # Since we're going to be using a streaming endpoint, there is no need to worry # about rate limits. api = Api(CONSUMER_KEY, @@ -64,7 +68,7 @@ def main(): with open('output.txt', 'a') as f: # api.GetStreamFilter will return a generator that yields one status # message (i.e., Tweet) at a time as a JSON dictionary. - for line in api.GetStreamFilter(track=USERS): + for line in api.GetStreamFilter(track=USERS, languages=LANGUAGES): f.write(json.dumps(line)) f.write('\n') diff --git a/twitter/api.py b/twitter/api.py index 4de7f376..c1b2f9ed 100644 --- a/twitter/api.py +++ b/twitter/api.py @@ -4452,6 +4452,7 @@ def GetStreamFilter(self, follow=None, track=None, locations=None, + languages=None, delimited=None, stall_warnings=None): """Returns a filtered view of public statuses. @@ -4468,6 +4469,10 @@ def GetStreamFilter(self, Specifies a message length. [Optional] stall_warnings: Set to True to have Twitter deliver stall warnings. [Optional] + languages: + A list of Languages. + Will only return Tweets that have been detected as being + written in the specified languages. [Optional] Returns: A twitter stream @@ -4486,6 +4491,8 @@ def GetStreamFilter(self, data['delimited'] = str(delimited) if stall_warnings is not None: data['stall_warnings'] = str(stall_warnings) + if languages is not None: + data['language'] = ','.join(languages) resp = self._RequestStream(url, 'POST', data=data) for line in resp.iter_lines():