X Tutup
Skip to content

Commit 6ca83cb

Browse files
authored
Merge pull request bear#401 from chaitanya0411/master
Add language support to GetStreamFilter()
2 parents afe51f7 + 78d9976 commit 6ca83cb

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,5 @@ ______________
101101
* Updated examples, specifically ``examples/twitter-to-xhtml.py``, ``examples/view_friends.py``, ``examples/shorten_url.py``
102102

103103
* Updated ``get_access_token.py`` script to be python3 compatible.
104+
105+
* :py:func:`twitter.api.Api.GetStreamFilter()` now accepts an optional languages parameter as a list.

examples/streaming/track_users.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
# ----------------------------------------------------------------------
1818

19-
# This file demonstrates how to track mentions of a specific set of users and
20-
# archive those mentions to a local file. The output file will contain one
21-
# JSON string per line per Tweet.
19+
# This file demonstrates how to track mentions of a specific set of users in
20+
# english language and archive those mentions to a local file. The output
21+
# file will contain one JSON string per line per Tweet.
2222

2323
# To use this example, replace the W/X/Y/Zs with your keys obtained from
2424
# Twitter, or uncomment the lines for getting an environment variable. If you
@@ -52,6 +52,10 @@
5252
'@twitterapi',
5353
'@support']
5454

55+
# Languages to filter tweets by is a list. This will be joined by Twitter
56+
# to return data mentioning tweets only in the english language.
57+
LANGUAGES = ['en']
58+
5559
# Since we're going to be using a streaming endpoint, there is no need to worry
5660
# about rate limits.
5761
api = Api(CONSUMER_KEY,
@@ -64,7 +68,7 @@ def main():
6468
with open('output.txt', 'a') as f:
6569
# api.GetStreamFilter will return a generator that yields one status
6670
# message (i.e., Tweet) at a time as a JSON dictionary.
67-
for line in api.GetStreamFilter(track=USERS):
71+
for line in api.GetStreamFilter(track=USERS, languages=LANGUAGES):
6872
f.write(json.dumps(line))
6973
f.write('\n')
7074

twitter/api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4524,6 +4524,7 @@ def GetStreamFilter(self,
45244524
follow=None,
45254525
track=None,
45264526
locations=None,
4527+
languages=None,
45274528
delimited=None,
45284529
stall_warnings=None):
45294530
"""Returns a filtered view of public statuses.
@@ -4540,6 +4541,10 @@ def GetStreamFilter(self,
45404541
Specifies a message length. [Optional]
45414542
stall_warnings:
45424543
Set to True to have Twitter deliver stall warnings. [Optional]
4544+
languages:
4545+
A list of Languages.
4546+
Will only return Tweets that have been detected as being
4547+
written in the specified languages. [Optional]
45434548
45444549
Returns:
45454550
A twitter stream
@@ -4558,6 +4563,8 @@ def GetStreamFilter(self,
45584563
data['delimited'] = str(delimited)
45594564
if stall_warnings is not None:
45604565
data['stall_warnings'] = str(stall_warnings)
4566+
if languages is not None:
4567+
data['language'] = ','.join(languages)
45614568

45624569
resp = self._RequestStream(url, 'POST', data=data)
45634570
for line in resp.iter_lines():

0 commit comments

Comments
 (0)
X Tutup