Exemple #1
0
def find_matching_tweets(num_tweets=100,
                         fname="matching_tweets.csv",
                         shownum=50):
    """Given the number of tweets to retrieve, queries that number of tweets with
    the keyword "Trump" and saves the tweet id and text as a csv file "fname". Prints
    out the shownum amount of tweets using panda. Does not remove retweets."""
    oauth = credsfromfile()
    # create and register a streamer
    client = Streamer(**oauth)
    writer = TweetWriter(limit=num_tweets)
    client.register(writer)
    # get the name of the newly-created json file
    input_file = writer.timestamped_file()
    client.filter(track="trump")  # case-insensitive
    client.sample()

    with open(input_file) as fp:
        # these two fields for now
        json2csv(fp, fname, [
            'id',
            'text',
        ])

    # pretty print using pandas
    tweets = pd.read_csv(fname, encoding="utf8")
    return tweets.head(shownum)
Exemple #2
0
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()
Exemple #3
0
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)
Exemple #4
0
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()
Exemple #5
0
def limit_by_time_demo(limit=20):
    """
    Sample from the Streaming API and send output to terminal.
    """
    oauth = credsfromfile()
    client = Streamer(**oauth)
    client.register(TweetWriter(limit=limit, date_limit=DATE))
    client.sample()
Exemple #6
0
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()
Exemple #7
0
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)
Exemple #8
0
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()
Exemple #9
0
def limit_by_time_demo(limit=20):
    """
    Sample from the Streaming API and send output to terminal.
    """
    oauth = credsfromfile()
    client = Streamer(**oauth)
    client.register(TweetWriter(limit=limit, date_limit=DATE))
    client.sample()
Exemple #10
0
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)
Exemple #11
0
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)
print("Importing")
from DangerTweetWriter import DangerTweetWriter
from nltk.twitter import Streamer, credsfromfile

from nltk.corpus import stopwords

print("Building auth")

oauth = credsfromfile()

twitterPass = ""
alreadyAdded = []
stopWords = set(stopwords.words('english'))

for phrase in DangerTweetWriter.dangerPhrases:
    split = phrase.split(" ")
    for word in split:
        if (word not in alreadyAdded and word not in stopWords):
            if (len(alreadyAdded) > 0):
                twitterPass = twitterPass + ", "
            twitterPass = twitterPass + word
            alreadyAdded.append(word)

print("Words to pass to Twitter's filter: " + twitterPass)

client = Streamer(**oauth)
client.register(DangerTweetWriter(10))
print("Attempting to stream")
client.filter(twitterPass)
        self.check_date_limit(data)
        if self.do_stop:
            return

        self.startingup = False


print("Building auth")

oauth = credsfromfile()

twitterPass = ""
alreadyAdded = []
stopWords = set(stopwords.words('english'))

for phrase in DangerTweetWriter.dangerPhrases:
    split = phrase.split(" ")
    for word in split:
        if (word not in alreadyAdded and word not in stopWords):
            if (len(alreadyAdded) > 0):
                twitterPass = twitterPass + ", "
            twitterPass = twitterPass + word
            alreadyAdded.append(word)

print("Words to pass to Twitter's filter: " + twitterPass)

client = Streamer(**oauth)
client.register(LiveTweetClassifierAndWriter(10000))
print("Attempting to stream")
client.filter(twitterPass)
Exemple #14
0
 def save_tweetes_file(self):
     client = Streamer(**oauth)
     client.register(TweetWriter(limit=100, subdir='twitter_samples_files'))
     client.statuses.sample()