Skip to content

Commit 0186eb3

Browse files
committed
Added languages support to the GetStreamFilter() function in api.py. Also added the corresponding example for use.
1 parent d3eb170 commit 0186eb3

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2007-2016 The Python-Twitter Developers
4+
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# ----------------------------------------------------------------------
18+
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.
22+
23+
# To use this example, replace the W/X/Y/Zs with your keys obtained from
24+
# Twitter, or uncomment the lines for getting an environment variable. If you
25+
# are using a virtualenv on Linux, you can set environment variables in the
26+
# ~/VIRTUALENVDIR/bin/activate script.
27+
28+
# If you need assistance with obtaining keys from Twitter, see the instructions
29+
# in doc/getting_started.rst.
30+
31+
import os
32+
import json
33+
34+
from twitter import Api
35+
36+
# Either specify a set of keys here or use os.getenv('CONSUMER_KEY') style
37+
# assignment:
38+
39+
CONSUMER_KEY = 'WWWWWWWW'
40+
# CONSUMER_KEY = os.getenv("CONSUMER_KEY", None)
41+
CONSUMER_SECRET = 'XXXXXXXX'
42+
# CONSUMER_SECRET = os.getenv("CONSUMER_SECRET", None)
43+
ACCESS_TOKEN = 'YYYYYYYY'
44+
# ACCESS_TOKEN = os.getenv("ACCESS_TOKEN", None)
45+
ACCESS_TOKEN_SECRET = 'ZZZZZZZZ'
46+
# ACCESS_TOKEN_SECRET = os.getenv("ACCESS_TOKEN_SECRET", None)
47+
48+
# Users to watch for should be a list. This will be joined by Twitter and the
49+
# data returned will be for any tweet mentioning:
50+
# @twitter *OR* @twitterapi *OR* @support.
51+
USERS = ['@twitter',
52+
'@twitterapi',
53+
'@support']
54+
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+
59+
# Since we're going to be using a streaming endpoint, there is no need to worry
60+
# about rate limits.
61+
api = Api(CONSUMER_KEY,
62+
CONSUMER_SECRET,
63+
ACCESS_TOKEN,
64+
ACCESS_TOKEN_SECRET)
65+
66+
67+
def main():
68+
with open('output.txt', 'a') as f:
69+
# api.GetStreamFilter will return a generator that yields one status
70+
# message (i.e., Tweet) at a time as a JSON dictionary.
71+
for line in api.GetStreamFilter(track=USERS, languages=LANGUAGES):
72+
f.write(json.dumps(line))
73+
f.write('\n')
74+
75+
76+
if __name__ == '__main__':
77+
main()

0 commit comments

Comments
 (0)