<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import datetime <EOL> from functools import wraps <EOL> import json <EOL> from nltk . compat import StringIO <EOL> from nltk . twitter import Query , Streamer , Twitter , TweetViewer , TweetWriter , credsfromfile <EOL> SPACER = '<STR_LIT>' <EOL> def verbose ( func ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( func ) <EOL> def with_formatting ( * args , ** kwargs ) : <EOL> print ( ) <EOL> print ( SPACER ) <EOL> print ( "<STR_LIT>" % ( func . __name__ ) ) <EOL> print ( SPACER ) <EOL> return func ( * args , ** kwargs ) <EOL> return with_formatting <EOL> def yesterday ( ) : <EOL> """<STR_LIT>""" <EOL> date = datetime . datetime . now ( ) <EOL> date -= datetime . timedelta ( days = <NUM_LIT:1> ) <EOL> date_tuple = date . timetuple ( ) [ : <NUM_LIT:6> ] <EOL> return date_tuple <EOL> def setup ( ) : <EOL> """<STR_LIT>""" <EOL> global USERIDS , FIELDS <EOL> USERIDS = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> FIELDS = [ '<STR_LIT>' ] <EOL> @ verbose <EOL> def twitterclass_demo ( ) : <EOL> """<STR_LIT>""" <EOL> tw = Twitter ( ) <EOL> print ( "<STR_LIT>" ) <EOL> tw . tweets ( keywords = '<STR_LIT>' , limit = <NUM_LIT:10> ) <EOL> print ( SPACER ) <EOL> print ( "<STR_LIT>" ) <EOL> tw = Twitter ( ) <EOL> tw . tweets ( keywords = '<STR_LIT>' , stream = False , limit = <NUM_LIT:10> ) <EOL> print ( SPACER ) <EOL> print ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> tw = Twitter ( ) <EOL> tw . tweets ( follow = [ '<STR_LIT>' , '<STR_LIT>' ] , stream = True , limit = <NUM_LIT:5> ) <EOL> @ verbose <EOL> def sampletoscreen_demo ( limit = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> oauth = credsfromfile ( ) <EOL> client = Streamer ( ** oauth ) <EOL> client . register ( TweetViewer ( limit = limit ) ) <EOL> client . sample ( ) <EOL> @ verbose <EOL> def tracktoscreen_demo ( track = "<STR_LIT>" , limit = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> oauth = credsfromfile ( ) <EOL> client = Streamer ( ** oauth ) <EOL> client . register ( TweetViewer ( limit = limit ) ) <EOL> client . filter ( track = track ) <EOL> @ verbose <EOL> def search_demo ( keywords = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> oauth = credsfromfile ( ) <EOL> client = Query ( ** oauth ) <EOL> for tweet in client . search_tweets ( keywords = keywords , limit = <NUM_LIT:10> ) : <EOL> print ( tweet [ '<STR_LIT:text>' ] ) <EOL> @ verbose <EOL> def tweets_by_user_demo ( user = '<STR_LIT>' , count = <NUM_LIT:200> ) : <EOL> """<STR_LIT>""" <EOL> oauth = credsfromfile ( ) <EOL> client = Query ( ** oauth ) <EOL> client . register ( TweetWriter ( ) ) <EOL> client . user_tweets ( user , count ) <EOL> @ verbose <EOL> def lookup_by_userid_demo ( ) : <EOL> """<STR_LIT>""" <EOL> oauth = credsfromfile ( ) <EOL> client = Query ( ** oauth ) <EOL> user_info = client . user_info_from_id ( USERIDS ) <EOL> for info in user_info : <EOL> name = info [ '<STR_LIT>' ] <EOL> followers = info [ '<STR_LIT>' ] <EOL> following = info [ '<STR_LIT>' ] <EOL> print ( "<STR_LIT>" . format ( name , followers , following ) ) <EOL> @ verbose <EOL> def followtoscreen_demo ( limit = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> oauth = credsfromfile ( ) <EOL> client = Streamer ( ** oauth ) <EOL> client . register ( TweetViewer ( limit = limit ) ) <EOL> client . statuses . filter ( follow = USERIDS ) <EOL> @ verbose <EOL> def streamtofile_demo ( limit = <NUM_LIT:20> ) : <EOL> """<STR_LIT>""" <EOL> oauth = credsfromfile ( ) <EOL> client = Streamer ( ** oauth ) <EOL> client . register ( TweetWriter ( limit = limit , repeat = False ) ) <EOL> client . statuses . sample ( ) <EOL> @ verbose <EOL> def limit_by_time_demo ( keywords = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> date = yesterday ( ) <EOL> dt_date = datetime . datetime ( * date ) <EOL> oauth = credsfromfile ( ) <EOL> client = Query ( ** oauth ) <EOL> client . register ( TweetViewer ( limit = <NUM_LIT:100> , lower_date_limit = date ) ) <EOL> print ( "<STR_LIT>" . format ( dt_date ) ) <EOL> for tweet in client . search_tweets ( keywords = keywords ) : <EOL> print ( "<STR_LIT>" . format ( tweet [ '<STR_LIT>' ] ) , end = '<STR_LIT>' ) <EOL> client . handler . handle ( tweet ) <EOL> @ verbose <EOL> def corpusreader_demo ( ) : <EOL> """<STR_LIT>""" <EOL> from nltk . corpus import twitter_samples as tweets <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( SPACER ) <EOL> for tweet in tweets . docs ( "<STR_LIT>" ) [ : <NUM_LIT:1> ] : <EOL> print ( json . dumps ( tweet , indent = <NUM_LIT:1> , sort_keys = True ) ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( SPACER ) <EOL> for text in tweets . strings ( "<STR_LIT>" ) [ : <NUM_LIT:15> ] : <EOL> print ( text ) <EOL> print ( ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( SPACER ) <EOL> for toks in tweets . tokenized ( "<STR_LIT>" ) [ : <NUM_LIT:15> ] : <EOL> print ( toks ) <EOL> @ verbose <EOL> def expand_tweetids_demo ( ) : <EOL> """<STR_LIT>""" <EOL> ids_f = StringIO ( """<STR_LIT>""" ) <EOL> oauth = credsfromfile ( ) <EOL> client = Query ( ** oauth ) <EOL> hydrated = client . expand_tweetids ( ids_f ) <EOL> for tweet in hydrated : <EOL> id_str = tweet [ '<STR_LIT>' ] <EOL> print ( '<STR_LIT>' . format ( id_str ) ) <EOL> text = tweet [ '<STR_LIT:text>' ] <EOL> if text . startswith ( '<STR_LIT>' ) : <EOL> text = "<STR_LIT>" <EOL> print ( text + '<STR_LIT:\n>' ) <EOL> ALL = [ twitterclass_demo , sampletoscreen_demo , tracktoscreen_demo , <EOL> search_demo , tweets_by_user_demo , lookup_by_userid_demo , followtoscreen_demo , <EOL> streamtofile_demo , limit_by_time_demo , corpusreader_demo , expand_tweetids_demo ] <EOL> """<STR_LIT>""" <EOL> DEMOS = ALL [ : ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> setup ( ) <EOL> for demo in DEMOS : <EOL> demo ( ) <EOL> print ( "<STR_LIT:\n>" + SPACER ) <EOL> print ( "<STR_LIT>" ) <EOL> print ( SPACER ) </s>
