Ejemplo n.º 1
0
def connect_to_twitter():
    '''
    Connect to Twitter Stream API using the API wrapper in stream.py in the following steps:

    Authenticate
    ============
    Authenticate app using OAuth

    Connect
    =======
    Connect to the streaming API using oauth object received from previous step

    Returns authenticated and stream connected Twitter object
    '''
    twitter = Twitter()
    twitter.authenticate()
    twitter.connect()
    return twitter
Ejemplo n.º 2
0
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from wordcloud import WordCloud, STOPWORDS

from api import Twitter

account_name = "Yang55109999"

twitter = Twitter(account_name)
twitter.connect()

tweets = twitter.get_tweets()

sorted_tweets = twitter.sort_by_popularity()

fmt = '{likes:<5} | {rts: <5} | {text}'
print(fmt.format(likes='♥', rts='♺', text='✍'))
print('-' * 100)
for tw in sorted_tweets[:10]:
    print(
        fmt.format(likes=tw.likes, rts=tw.rts, text=tw.text.replace('\n',
                                                                    '⏎')))

all_tweets_excl_rts_mentions = ' '.join([
    tw.text.lower() for tw in tweets
    if not tw.text.startswith('RT') and not tw.text.startswith('@')
])

pb_mask = np.array(Image.open("pybites.png"))
stopwords = set(STOPWORDS)