File size: 8,309 Bytes
de33670 |
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# Natural Language Toolkit: Twitter client
#
# Copyright (C) 2001-2023 NLTK Project
# Author: Ewan Klein <[email protected]>
# Lorenzo Rubio <[email protected]>
# URL: <https://www.nltk.org/>
# For license information, see LICENSE.TXT
"""
Examples to demo the :py:mod:`twitterclient` code.
These demo functions should all run, with the following caveats:
* You must have obtained API keys from Twitter, and installed them according to
the instructions in the `twitter HOWTO <https://www.nltk.org/howto/twitter.html>`_.
* If you are on a slow network, some of the calls to the Twitter API may
timeout.
* If you are being rate limited while searching, you will receive a 420
error response.
* Your terminal window / console must be able to display UTF-8 encoded characters.
For documentation about the Twitter APIs, see `The Streaming APIs Overview
<https://dev.twitter.com/streaming/overview>`_ and `The REST APIs Overview
<https://dev.twitter.com/rest/public>`_.
For error codes see Twitter's
`Error Codes and Responses <https://dev.twitter.com/overview/api/response-codes>`
"""
import datetime
import json
from functools import wraps
from io import StringIO
from nltk.twitter import (
Query,
Streamer,
TweetViewer,
TweetWriter,
Twitter,
credsfromfile,
)
SPACER = "###################################"
def verbose(func):
"""Decorator for demo functions"""
@wraps(func)
def with_formatting(*args, **kwargs):
print()
print(SPACER)
print("Using %s" % (func.__name__))
print(SPACER)
return func(*args, **kwargs)
return with_formatting
def yesterday():
"""
Get yesterday's datetime as a 5-tuple.
"""
date = datetime.datetime.now()
date -= datetime.timedelta(days=1)
date_tuple = date.timetuple()[:6]
return date_tuple
def setup():
"""
Initialize global variables for the demos.
"""
global USERIDS, FIELDS
USERIDS = ["759251", "612473", "15108702", "6017542", "2673523800"]
# UserIDs corresponding to\
# @CNN, @BBCNews, @ReutersLive, @BreakingNews, @AJELive
FIELDS = ["id_str"]
@verbose
def twitterclass_demo():
"""
Use the simplified :class:`Twitter` class to write some tweets to a file.
"""
tw = Twitter()
print("Track from the public stream\n")
tw.tweets(keywords="love, hate", limit=10) # public stream
print(SPACER)
print("Search past Tweets\n")
tw = Twitter()
tw.tweets(keywords="love, hate", stream=False, limit=10) # search past tweets
print(SPACER)
print(
"Follow two accounts in the public stream"
+ " -- be prepared to wait a few minutes\n"
)
tw = Twitter()
tw.tweets(follow=["759251", "6017542"], stream=True, limit=5) # public stream
@verbose
def sampletoscreen_demo(limit=20):
"""
Sample from the Streaming API and send output to terminal.
"""
oauth = credsfromfile()
client = Streamer(**oauth)
client.register(TweetViewer(limit=limit))
client.sample()
@verbose
def tracktoscreen_demo(track="taylor swift", limit=10):
"""
Track keywords from the public Streaming API and send output to terminal.
"""
oauth = credsfromfile()
client = Streamer(**oauth)
client.register(TweetViewer(limit=limit))
client.filter(track=track)
@verbose
def search_demo(keywords="nltk"):
"""
Use the REST API to search for past tweets containing a given keyword.
"""
oauth = credsfromfile()
client = Query(**oauth)
for tweet in client.search_tweets(keywords=keywords, limit=10):
print(tweet["text"])
@verbose
def tweets_by_user_demo(user="NLTK_org", count=200):
"""
Use the REST API to search for past tweets by a given user.
"""
oauth = credsfromfile()
client = Query(**oauth)
client.register(TweetWriter())
client.user_tweets(user, count)
@verbose
def lookup_by_userid_demo():
"""
Use the REST API to convert a userID to a screen name.
"""
oauth = credsfromfile()
client = Query(**oauth)
user_info = client.user_info_from_id(USERIDS)
for info in user_info:
name = info["screen_name"]
followers = info["followers_count"]
following = info["friends_count"]
print(f"{name}, followers: {followers}, following: {following}")
@verbose
def followtoscreen_demo(limit=10):
"""
Using the Streaming API, select just the tweets from a specified list of
userIDs.
This is will only give results in a reasonable time if the users in
question produce a high volume of tweets, and may even so show some delay.
"""
oauth = credsfromfile()
client = Streamer(**oauth)
client.register(TweetViewer(limit=limit))
client.statuses.filter(follow=USERIDS)
@verbose
def streamtofile_demo(limit=20):
"""
Write 20 tweets sampled from the public Streaming API to a file.
"""
oauth = credsfromfile()
client = Streamer(**oauth)
client.register(TweetWriter(limit=limit, repeat=False))
client.statuses.sample()
@verbose
def limit_by_time_demo(keywords="nltk"):
"""
Query the REST API for Tweets about NLTK since yesterday and send
the output to terminal.
This example makes the assumption that there are sufficient Tweets since
yesterday for the date to be an effective cut-off.
"""
date = yesterday()
dt_date = datetime.datetime(*date)
oauth = credsfromfile()
client = Query(**oauth)
client.register(TweetViewer(limit=100, lower_date_limit=date))
print(f"Cutoff date: {dt_date}\n")
for tweet in client.search_tweets(keywords=keywords):
print("{} ".format(tweet["created_at"]), end="")
client.handler.handle(tweet)
@verbose
def corpusreader_demo():
"""
Use `TwitterCorpusReader` tp read a file of tweets, and print out
* some full tweets in JSON format;
* some raw strings from the tweets (i.e., the value of the `text` field); and
* the result of tokenising the raw strings.
"""
from nltk.corpus import twitter_samples as tweets
print()
print("Complete tweet documents")
print(SPACER)
for tweet in tweets.docs("tweets.20150430-223406.json")[:1]:
print(json.dumps(tweet, indent=1, sort_keys=True))
print()
print("Raw tweet strings:")
print(SPACER)
for text in tweets.strings("tweets.20150430-223406.json")[:15]:
print(text)
print()
print("Tokenized tweet strings:")
print(SPACER)
for toks in tweets.tokenized("tweets.20150430-223406.json")[:15]:
print(toks)
@verbose
def expand_tweetids_demo():
"""
Given a file object containing a list of Tweet IDs, fetch the
corresponding full Tweets, if available.
"""
ids_f = StringIO(
"""\
588665495492124672
588665495487909888
588665495508766721
588665495513006080
588665495517200384
588665495487811584
588665495525588992
588665495487844352
588665495492014081
588665495512948737"""
)
oauth = credsfromfile()
client = Query(**oauth)
hydrated = client.expand_tweetids(ids_f)
for tweet in hydrated:
id_str = tweet["id_str"]
print(f"id: {id_str}")
text = tweet["text"]
if text.startswith("@null"):
text = "[Tweet not available]"
print(text + "\n")
ALL = [
twitterclass_demo,
sampletoscreen_demo,
tracktoscreen_demo,
search_demo,
tweets_by_user_demo,
lookup_by_userid_demo,
followtoscreen_demo,
streamtofile_demo,
limit_by_time_demo,
corpusreader_demo,
expand_tweetids_demo,
]
"""
Select demo functions to run. E.g. replace the following line with "DEMOS =
ALL[8:]" to execute only the final three demos.
"""
DEMOS = ALL[:]
if __name__ == "__main__":
setup()
for demo in DEMOS:
demo()
print("\n" + SPACER)
print("All demos completed")
print(SPACER)
|