ó <¿CVc @sßdZddlmZddlZddlmZddlZddlmZddl m Z m Z m Z m Z mZmZdZd„Zd „Zd „Zed „ƒZed d „ƒZeddd„ƒZedd„ƒZeddd„ƒZed„ƒZedd„ƒZed d„ƒZedd„ƒZed„ƒZed„ƒZeeeeeeeeeeeg ZeZ e!dkrÛeƒxe D] Z"e"ƒq¥We#deƒe#dƒe#eƒndS(sQ 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 `_. * 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 `_ and `The REST APIs Overview `_. For error codes see Twitter's `Error Codes and Responses ` iÿÿÿÿ(tprint_functionN(twraps(tStringIO(tQuerytStreamertTwittert TweetViewert TweetWritert credsfromfiles####################################cstˆƒ‡fd†ƒ}|S(sDecorator for demo functionscs9tƒttƒtdˆjƒttƒˆ||ŽS(NsUsing %s(tprinttSPACERt__name__(targstkwargs(tfunc(sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytwith_formatting1s   (R(RR((Rsk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytverbose/scCs9tjjƒ}|tjddƒ8}|jƒd }|S(s0 Get yesterday's datetime as a 5-tuple. tdaysii(tdatetimetnowt timedeltat timetuple(tdatet date_tuple((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pyt yesterday:scCs"dddddgadgadS(s4 Initialize global variables for the demos. t759251t612473t15108702t6017542t 2673523800tid_strN(tUSERIDStFIELDS(((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytsetupCscCs©tƒ}tdƒ|jddddƒttƒtdƒtƒ}|jdddtddƒttƒtdd ƒtƒ}|jd d d gdtdd ƒdS(sS Use the simplified :class:`Twitter` class to write some tweets to a file. sTrack from the public stream tkeywordss love, hatetlimiti sSearch past Tweets tstreams(Follow two accounts in the public streams& -- be prepared to wait a few minutes tfollowRRiN(RR ttweetsR tFalsetTrue(ttw((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pyttwitterclass_demoOs       icCs9tƒ}t|}|jtd|ƒƒ|jƒdS(sD Sample from the Streaming API and send output to terminal. R#N(RRtregisterRtsample(R#toauthtclient((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytsampletoscreen_demobs  s taylor swifti cCs?tƒ}t|}|jtd|ƒƒ|jd|ƒdS(sS Track keywords from the public Streaming API and send output to terminal. R#ttrackN(RRR+Rtfilter(R0R#R-R.((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pyttracktoscreen_demoms  tnltkcCsJtƒ}t|}x.|jd|ddƒD]}t|dƒq.WdS(sP Use the REST API to search for past tweets containing a given keyword. R"R#i ttextN(RRt search_tweetsR (R"R-R.ttweet((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pyt search_demoxs  tNLTK_orgiÈcCs9tƒ}t|}|jtƒƒ|j||ƒdS(sE Use the REST API to search for past tweets by a given user. N(RRR+Rt user_tweets(tusertcountR-R.((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pyttweets_by_user_demoƒs  cCsptƒ}t|}|jtƒ}xE|D]=}|d}|d}|d}tdj|||ƒƒq+WdS(s@ Use the REST API to convert a userID to a screen name. t screen_nametfollowers_countt friends_counts#{0}, followers: {1}, following: {2}N(RRtuser_info_from_idRR tformat(R-R.t user_infotinfotnamet followerst following((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytlookup_by_userid_demoŽs      cCsBtƒ}t|}|jtd|ƒƒ|jjdtƒdS(s÷ 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. R#R%N(RRR+RtstatusesR1R(R#R-R.((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytfollowtoscreen_demos  cCsBtƒ}t|}|jtd|dtƒƒ|jjƒdS(sJ Write 20 tweets sampled from the public Streaming API to a file. R#trepeatN(RRR+RR'RHR,(R#R-R.((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytstreamtofile_demo¬s  cCsªtƒ}tj|Œ}tƒ}t|}|jtddd|ƒƒtdj|ƒƒxG|jd|ƒD]3}tdj|dƒdd ƒ|j j |ƒqoWd S( sì 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. R#idtlower_date_limitsCutoff date: {} R"s{} t created_attendtN( RRRRR+RR RAR5thandlerthandle(R"Rtdt_dateR-R.R6((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytlimit_by_time_demo·s   cCsòddlm}tƒtdƒttƒx:|jdƒd D]%}ttj|dddtƒƒq?Wtƒtdƒttƒx%|jdƒd D]}t|ƒq—Wtƒtd ƒttƒx%|j dƒd D]}t|ƒqÚWd S( sþ Use :module:`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. iÿÿÿÿ(ttwitter_samplessComplete tweet documentsstweets.20150430-223406.jsonitindentt sort_keyssRaw tweet strings:isTokenized tweet strings:N( t nltk.corpusRTR R tdocstjsontdumpsR(tstringst tokenized(R&R6R4ttoks((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytcorpusreader_demoÍs   #    cCs’tdƒ}tƒ}t|}|j|ƒ}x[|D]S}|d}tdj|ƒƒ|d}|jdƒr|d}nt|dƒq7WdS( su Given a file object containing a list of Tweet IDs, fetch the corresponding full Tweets, if available. s  588665495492124672 588665495487909888 588665495508766721 588665495513006080 588665495517200384 588665495487811584 588665495525588992 588665495487844352 588665495492014081 588665495512948737Rsid: {}R4s@nulls[Tweet not available]s N(RRRtexpand_tweetidsR RAt startswith(tids_fR-R.thydratedR6RR4((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pytexpand_tweetids_demoìs       t__main__s sAll demos completed($t__doc__t __future__RRt functoolsRRYt nltk.compatRt nltk.twitterRRRRRRR RRR!R*R/R2R7R<RGRIRKRSR^RctALLtDEMOSR tdemoR (((sk/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/twitter/twitter_demo.pyt sN  .      !