
    Qdu                      "   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ dZd Zd Zd	 Zed
             Zedd            Zedd            Zed d            Zed!d            Zed             Zed"d            Zedd            Zed d            Zed             Zed             ZeeeeeeeeeeegZ	 edd         Zedk    r? e             eD ]Z  e               e!dez               e!d            e!e           dS dS )#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 <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>`
    Nwraps)StringIO)QueryStreamerTweetViewerTweetWriterTwittercredsfromfilez####################################c                 <     t                      fd            }|S )zDecorator for demo functionsc                      t                       t          t                     t          dj        z             t          t                      | i |S )NzUsing %s)printSPACER__name__)argskwargsfuncs     9lib/python3.11/site-packages/nltk/twitter/twitter_demo.pywith_formattingz verbose.<locals>.with_formatting5   sL    fjDM*+++ftT$V$$$    r   )r   r   s   ` r   verboser   2   s6     4[[% % % % [% r   c                      t           j                                         } | t          j        d          z  } |                                 dd         }|S )z0
    Get yesterday's datetime as a 5-tuple.
       )daysN   )datetimenow	timedelta	timetuple)date
date_tuples     r   	yesterdayr"   @   sN       ""DHA&&&&D!!"1"%Jr   c                      g da dgadS )z4
    Initialize global variables for the demos.
    )759251612473151087026017542
2673523800id_strN)USERIDSFIELDS r   r   setupr-   J   s     HGGG ZFFFr   c                     t                      } t          d           |                     dd           t          t                     t          d           t                      } |                     ddd           t          t                     t          d           t                      } |                     d	d
gdd           dS )zS
    Use the simplified :class:`Twitter` class to write some tweets to a file.
    zTrack from the public stream
z
love, hate
   keywordslimitzSearch past Tweets
F)r1   streamr2   zNFollow two accounts in the public stream -- be prepared to wait a few minutes
r$   r'   T   )followr3   r2   N)r
   r   tweetsr   )tws    r   twitterclass_demor8   V   s    
 
B	
*+++II|2I...	&MMM	
 !!!	BII|EI<<<	&MMM		4   
BIIh	*4qIAAAAAr      c                     t                      }t          di |}|                    t          |                      |                                 dS )zD
    Sample from the Streaming API and send output to terminal.
    r2   Nr,   )r   r   registerr   sampler2   oauthclients      r   sampletoscreen_demorA   k   sN    
 OOEF
OOKe,,,---
MMOOOOOr   taylor swiftr/   c                     t                      }t          di |}|                    t          |                     |                    |            dS )zS
    Track keywords from the public Streaming API and send output to terminal.
    r;   )trackNr,   )r   r   r<   r   filter)rD   r2   r?   r@   s       r   tracktoscreen_demorF   v   sW    
 OOEF
OOKe,,,---
MMMr   nltkc                     t                      }t          di |}|                    | d          D ]}t          |d                    dS )zP
    Use the REST API to search for past tweets containing a given keyword.
    r/   r0   textNr,   )r   r   search_tweetsr   )r1   r?   r@   tweets       r   search_demorL      s]    
 OOE^^U^^F%%xr%BB  eFm r   NLTK_org   c                     t                      }t          di |}|                    t                                 |                    | |           dS )zE
    Use the REST API to search for past tweets by a given user.
    Nr,   )r   r   r<   r	   user_tweets)usercountr?   r@   s       r   tweets_by_user_demorS      sN    
 OOE^^U^^F
OOKMM"""
tU#####r   c                      t                      } t          di | }|                    t                    }|D ]1}|d         }|d         }|d         }t	          | d| d|            2dS )z@
    Use the REST API to convert a userID to a screen name.
    screen_namefollowers_countfriends_countz, followers: z, following: Nr,   )r   r   user_info_from_idr*   r   )r?   r@   	user_infoinfoname	followers	followings          r   lookup_by_userid_demor^      s    
 OOE^^U^^F((11I I IM"*+	)	GGIGGIGGHHHH	I Ir   c                     t                      }t          di |}|                    t          |                      |j                            t                     dS )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;   )r5   Nr,   )r   r   r<   r   statusesrE   r*   r>   s      r   followtoscreen_demora      s\     OOEF
OOKe,,,---
O'*****r   c                     t                      }t          di |}|                    t          | d                     |j                                         dS )zJ
    Write 20 tweets sampled from the public Streaming API to a file.
    F)r2   repeatNr,   )r   r   r<   r	   r`   r=   r>   s      r   streamtofile_demord      sY    
 OOEF
OOKeE:::;;;
Or   c                    t                      }t          j        | }t                      }t          di |}|                    t          d|                     t          d| d           |                    |           D ]F}t          d                    |d                   d	           |j	        
                    |           Gd
S )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   )r2   lower_date_limitzCutoff date: 
)r1   z{} 
created_at )endNr,   )r"   r   r   r   r<   r   r   rJ   formathandlerhandle)r1   r    dt_dater?   r@   rK   s         r   limit_by_time_demorp      s     ;;D&GOOE^^U^^F
OOKcDAAABBB	
%'
%
%
%&&&%%x%88 % %ell5.//R8888e$$$$% %r   c                  |   ddl m}  t                       t          d           t          t                     |                     d          dd         D ]&}t          t          j        |dd                     't                       t          d	           t          t                     |                     d          dd
         D ]}t          |           t                       t          d           t          t                     |                     d          dd
         D ]}t          |           dS )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_sampleszComplete tweet documentsztweets.20150430-223406.jsonNr   T)indent	sort_keyszRaw tweet strings:   zTokenized tweet strings:)	nltk.corpusrr   r   r   docsjsondumpsstrings	tokenized)r6   rK   rI   tokss       r   corpusreader_demor}      s6    655555	GGG	
$%%%	&MMM:;;BQB? ; ;djqD999::::	GGG	
	&MMM<==crcB  d	GGG	
$%%%	&MMM  !>??D  d r   c                  "   t          d          } t                      }t          d	i |}|                    |           }|D ]M}|d         }t	          d|            |d         }|                    d          rd}t	          |dz              NdS )
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)   zid: rI   z@nullz[Tweet not available]rh   Nr,   )r   r   r   expand_tweetidsr   
startswith)ids_fr?   r@   hydratedrK   r)   rI   s          r   expand_tweetids_demor      s     
	 E OOE^^U^^F%%e,,H  xoVooV}??7## 	+*DdTk r   __main__rh   zAll demos completed)r9   )rB   r/   )rG   )rM   rN   )r/   )"__doc__r   rx   	functoolsr   ior   nltk.twitterr   r   r   r	   r
   r   r   r   r"   r-   r8   rA   rF   rL   rS   r^   ra   rd   rp   r}   r   ALLDEMOSr   demor   r,   r   r   <module>r      s   0                              
/    	 	 	 	B B 	B( 	   	 	   	 	   	 	$ $ $ 	$ 	I I 	I 	+ + + 	+ 	   	 	% % % 	%* 	  	< 	  	B  	AAAz	EGGG  	E$-	E
   	E&MMMMM r   