Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 8 additions & 4 deletions examples/streaming/track_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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')

Expand Down
7 changes: 7 additions & 0 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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():
Expand Down