-
Notifications
You must be signed in to change notification settings - Fork 323
/
twitter-stream-search.py
executable file
·70 lines (55 loc) · 2.49 KB
/
twitter-stream-search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
#-----------------------------------------------------------------------
# twitter-stream-format:
# - ultra-real-time stream of twitter's public timeline.
# does some fancy output formatting.
#-----------------------------------------------------------------------
from twitter import *
import re
search_term = "bieber"
#-----------------------------------------------------------------------
# import a load of external features, for text display and date handling
# you will need the termcolor module:
#
# pip install termcolor
#-----------------------------------------------------------------------
from time import strftime
from textwrap import fill
from termcolor import colored
from email.utils import parsedate
#-----------------------------------------------------------------------
# load our API credentials
#-----------------------------------------------------------------------
import sys
sys.path.append(".")
import config
#-----------------------------------------------------------------------
# create twitter streaming API object
#-----------------------------------------------------------------------
auth = OAuth(config.access_key,
config.access_secret,
config.consumer_key,
config.consumer_secret)
stream = TwitterStream(auth=auth, secure=True)
#-----------------------------------------------------------------------
# iterate over tweets matching this filter text
#-----------------------------------------------------------------------
tweet_iter = stream.statuses.filter(track=search_term)
pattern = re.compile("%s" % search_term, re.IGNORECASE)
for tweet in tweet_iter:
# turn the date string into a date object that python can handle
timestamp = parsedate(tweet["created_at"])
# now format this nicely into HH:MM:SS format
timetext = strftime("%H:%M:%S", timestamp)
# colour our tweet's time, user and text
time_colored = colored(timetext, color="white", attrs=["bold"])
user_colored = colored(tweet["user"]["screen_name"], "green")
text_colored = tweet["text"]
# replace each instance of our search terms with a highlighted version
text_colored = pattern.sub(colored(search_term.upper(), "yellow"), text_colored)
# add some indenting to each line and wrap the text nicely
indent = " " * 11
text_colored = fill(text_colored, 80, initial_indent=indent, subsequent_indent=indent)
# now output our tweet
print("(%s) @%s" % (time_colored, user_colored))
print("%s" % (text_colored))