JL iC dZddlZddlZddlmZddlmZddlmZm Z m Z m Z m Z m Z dZdZdZd Zed Zedd Zedd Zedd ZeddZedZeddZeddZeddZedZedZeeeeeeeeeeeg Z eddZedk(r1eeD] Z e  e!deze!de!eyy)aR 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 ` Nwraps)StringIO)QueryStreamer TweetViewer TweetWriterTwitter credsfromfilez####################################c.tfd}|S)zDecorator for demo functionscttttdjztt|i|S)NzUsing %s)printSPACER__name__)argskwargsfuncs _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/twitter/twitter_demo.pywith_formattingz verbose..with_formatting5s7  f  jDMM*+ f T$V$$r)rrs` rverboser2s# 4[%% rctjj}|tjdz}|jdd}|S)z0 Get yesterday's datetime as a 5-tuple. )daysN)datetimenow timedelta timetuple)date date_tuples r yesterdayr"@sF    "DH  A &&D!"1%J rcgdadgay)z4 Initialize global variables for the demos. )759251612473151087026017542 2673523800id_strN)USERIDSFIELDSrrsetupr-Js HGZFrc8t}td|jddtttdt}|jdddtttdt}|jd d gd d y)zS Use the simplified :class:`Twitter` class to write some tweets to a file. zTrack from the public stream z love, hate keywordslimitzSearch past Tweets F)r1streamr2zNFollow two accounts in the public stream -- be prepared to wait a few minutes r$r'T)followr3r2N)r rtweetsr)tws rtwitterclass_demor8Vs B *+II|2I. &M ! BII|EI< &M  4 BIIh *4qIArct}tdi|}|jt||j y)zD Sample from the Streaming API and send output to terminal. r2Nr,)r rregisterrsampler2oauthclients rsampletoscreen_demor@ks2 OE   F OOKe,- MMOrct}tdi|}|jt||j |y)zS Track keywords from the public Streaming API and send output to terminal. r:)trackNr,)r rr;rfilter)rBr2r>r?s rtracktoscreen_demorDvs7 OE   F OOKe,- MMMrczt}tdi|}|j|dD]}t|dy)zP Use the REST API to search for past tweets containing a given keyword. r/r0textNr,)r r search_tweetsr)r1r>r?tweets r search_demorIsA OE ^U^F%%xr%B eFmrct}tdi|}|jt|j ||y)zE Use the REST API to search for past tweets by a given user. Nr,)r rr;r user_tweets)usercountr>r?s rtweets_by_user_demorNs4 OE ^U^F OOKM" tU#rct}tdi|}|jt}|D]$}|d}|d}|d}t |d|d|&y)z@ Use the REST API to convert a userID to a screen name. screen_namefollowers_count friends_countz , followers: z , following: Nr,)r ruser_info_from_idr*r)r>r? user_infoinfoname followers followings rlookup_by_userid_demorYso OE ^U^F((1IIM"*+ )  mI;mI;GH Irct}tdi|}|jt||jj t y)z 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:)r5Nr,)r rr;rstatusesrCr*r=s rfollowtoscreen_demor\s> OE   F OOKe,- OO'*rct}tdi|}|jt|d|jj y)zJ Write 20 tweets sampled from the public Streaming API to a file. F)r2repeatNr,)r rr;r r[r<r=s rstreamtofile_demor_s; OE   F OOKeE:; OOrc^t}tj|}t}td i|}|j t d|t d|d|j|D]<}t dj|dd |jj|>y ) z 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. d)r2lower_date_limitz Cutoff date:  )r1z{} created_at)endNr,) r"rr rr;rrrGformathandlerhandle)r1r dt_dater>r?rHs rlimit_by_time_demorks ;D&G OE ^U^F OOKcDAB M'" %&%%x%8% ell5./R8e$%rcddlm}ttdtt|j dddD]#}tt j |dd%ttd tt|jddd D] }t|ttd tt|jddd D] }t|y) z 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. r)twitter_sampleszComplete tweet documentsztweets.20150430-223406.jsonNrT)indent sort_keyszRaw tweet strings:zTokenized tweet strings:) nltk.corpusrmrrdocsjsondumpsstrings tokenized)r6rHrFtokss rcorpusreader_demorxs6 G $% &M:;BQ?; djjqD9:; G  &M<=crB d  G $% &M  !>?D d rctd}t}td i|}|j|}|D];}|d}t d||d}|j drd}t |dz=y) zu Given a file object containing a list of Tweet IDs, fetch the corresponding full Tweets, if available. a  588665495492124672 588665495487909888 588665495508766721 588665495513006080 588665495517200384 588665495487811584 588665495525588992 588665495487844352 588665495492014081 588665495512948737r)zid: rFz@nullz[Tweet not available]rcNr,)rr rexpand_tweetidsr startswith)ids_fr>r?hydratedrHr)rFs rexpand_tweetids_demor~s   E OE ^U^F%%e,Hx VHoV} ??7 #*D dTk r__main__rczAll demos completed))z taylor swiftr/)nltk)NLTK_org)r/)"__doc__rrs functoolsrior nltk.twitterrrrr r r rrr"r-r8r@rDrIrNrYr\r_rkrxr~ALLDEMOSrdemorr,rrrs0  /   B B(       $ $  I  I  +  +   % %*  <  B  A z G  $-   &Mr