Esempio n. 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)
Esempio n. 2
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)
Esempio n. 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)
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)