Ejemplo n.º 1
0
def twitter_checkin(creds, sysinfo):
    consumer_key, consumer_secret, access_token, access_token_secret = creds
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    api = API(auth, wait_on_rate_limit=True)
    api.update_status(sysinfo)
    return ["twitter_checkin"]
Ejemplo n.º 2
0
class Tweet():
	
	def __init__(self, auth):
		self.auth = auth
		self.api = API(auth)

	def tweet_with_media(self, fn, status):
		self.api.update_with_media(fn, status=status)

	def update_with_media(self, fn, status, tweet_id):
		# self.api.update_with_media(filename=fn, status=status, in_reply_to_status_id=tweet_id)
		media = self.api.media_upload(fn)
		self.api.update_status(status=status, reply_to_status_id=tweet_id, media_ids=[media.media_id])

	def update(self, status, tweet_id):
		self.api.update_status(status=status, reply_to_status_id=tweet_id)
Ejemplo n.º 3
0
def tweet(answer):
    CONSUMER_KEY = config.get("auth", "CONSUMER_KEY")
    CONSUMER_SECRET = config.get("auth", "CONSUMER_SECRET")
    ACCESS_TOKEN = config.get("auth", "ACCESS_TOKEN")
    ACCESS_TOKEN_SECRET = config.get("auth", "ACCESS_TOKEN_SECRET")

    auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    api = API(auth)
    result = api.update_status(status=answer)
Ejemplo n.º 4
0
    def _testoauth(self):
        auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)

        # test getting access token
        auth_url = auth.get_authorization_url()
        print 'Please authorize: ' + auth_url
        verifier = raw_input('PIN: ').strip()
        self.assert_(len(verifier) > 0)
        access_token = auth.get_access_token(verifier)
        self.assert_(access_token is not None)

        # build api object test using oauth
        api = API(auth)
        s = api.update_status('test %i' % random.randint(0, 1000))
        api.destroy_status(s.id)
Ejemplo n.º 5
0
def update_reply(text, reply_id, screen_name):
    auth = get_oauth()
    api = API(auth)
    st = "@" + str(screen_name) + " " + str(text)
    api.update_status(status=st, in_reply_to_status_id=reply_id)
Ejemplo n.º 6
0
def update_tweet(text):
    auth = get_oauth()
    api = API(auth)
    api.update_status(status=text)
Ejemplo n.º 7
0
class TweepyAPITests(unittest.TestCase):

    def setUp(self):
        auth = OAuthHandler(oauth_consumer_key, oauth_consumer_secret)
        auth.set_access_token(oauth_token, oauth_token_secret)
        self.api = API(auth)
        self.api.retry_count = 2
        self.api.retry_delay = 5

    def testhometimeline(self):
        self.api.home_timeline()

    def testfriendstimeline(self):
        self.api.friends_timeline()

    def testusertimeline(self):
        self.api.user_timeline()
        self.api.user_timeline('twitter')

    def testmentions(self):
        self.api.mentions()

    def testretweetedbyme(self):
        self.api.retweeted_by_me()

    def testretweetedbyuser(self):
        self.api.retweeted_by_user('twitter')

    def testretweetedtome(self):
        self.api.retweeted_to_me()

    def testretweetsofme(self):
        self.api.retweets_of_me()

    def testretweet(self):
        s = self.api.retweet(123)
        s.destroy()

    def testretweets(self):
        self.api.retweets(123)

    def testgetstatus(self):
        self.api.get_status(id=123)

    def testupdateanddestroystatus(self):
        # test update
        text = 'testing %i' % random.randint(0, 1000)
        update = self.api.update_status(status=text)
        self.assertEqual(update.text, text)

        # test destroy
        deleted = self.api.destroy_status(id=update.id)
        self.assertEqual(deleted.id, update.id)

    def testgetuser(self):
        u = self.api.get_user('twitter')
        self.assertEqual(u.screen_name, 'twitter')

        u = self.api.get_user(783214)
        self.assertEqual(u.screen_name, 'twitter')

    def testsearchusers(self):
        self.api.search_users('twitter')

    def testme(self):
        me = self.api.me()
        self.assertEqual(me.screen_name, username)

    def testfriends(self):
        self.api.friends()

    def testfollowers(self):
        self.api.followers()

    def testdirectmessages(self):
        self.api.direct_messages()

    def testsentdirectmessages(self):
        self.api.sent_direct_messages()

    def testsendanddestroydirectmessage(self):
        # send
        sent_dm = self.api.send_direct_message(username, text='test message')
        self.assertEqual(sent_dm.text, 'test message')
        self.assertEqual(sent_dm.sender.screen_name, username)
        self.assertEqual(sent_dm.recipient.screen_name, username)

        # destroy
        destroyed_dm = self.api.destroy_direct_message(sent_dm.id)
        self.assertEqual(destroyed_dm.text, sent_dm.text)
        self.assertEqual(destroyed_dm.id, sent_dm.id)
        self.assertEqual(destroyed_dm.sender.screen_name, username)
        self.assertEqual(destroyed_dm.recipient.screen_name, username)

    def testcreatedestroyfriendship(self):
        enemy = self.api.destroy_friendship('twitter')
        self.assertEqual(enemy.screen_name, 'twitter')
        self.assertFalse(self.api.exists_friendship(username, 'twitter'))

        friend = self.api.create_friendship('twitter')
        self.assertEqual(friend.screen_name, 'twitter')
        self.assertTrue(self.api.exists_friendship(username, 'twitter'))

    def testshowfriendship(self):
        source, target = self.api.show_friendship(target_screen_name='twtiter')
        self.assert_(isinstance(source, Friendship))
        self.assert_(isinstance(target, Friendship))

    def testfriendsids(self):
        self.api.friends_ids(username)

    def testfollowersids(self):
        self.api.followers_ids(username)

    def testverifycredentials(self):
        self.assertNotEqual(self.api.verify_credentials(), False)

        # make sure that `me.status.entities` is not an empty dict
        me = self.api.verify_credentials(include_entities=True)
        self.assertTrue(me.status.entities)

        # `status` shouldn't be included
        me = self.api.verify_credentials(skip_status=True)
        self.assertFalse(hasattr(me, 'status'))

    def testratelimitstatus(self):
        self.api.rate_limit_status()

    def testupdateprofilecolors(self):
        original = self.api.me()
        updated = self.api.update_profile_colors(
            '000', '000', '000', '000', '000')

        # restore colors
        self.api.update_profile_colors(
            original.profile_background_color,
            original.profile_text_color,
            original.profile_link_color,
            original.profile_sidebar_fill_color,
            original.profile_sidebar_border_color
        )

        self.assertEqual(updated.profile_background_color, '000')
        self.assertEqual(updated.profile_text_color, '000')
        self.assertEqual(updated.profile_link_color, '000')
        self.assertEqual(updated.profile_sidebar_fill_color, '000')
        self.assertEqual(updated.profile_sidebar_border_color, '000')

    """
    def testupateprofileimage(self):
        self.api.update_profile_image('examples/profile.png')

    def testupdateprofilebg(self):
        self.api.update_profile_background_image('examples/bg.png')
    """

    def testupdateprofile(self):
        original = self.api.me()
        profile = {
            'name': 'Tweepy test 123',
            'url': 'http://www.example.com',
            'location': 'pytopia',
            'description': 'just testing things out'
        }
        updated = self.api.update_profile(**profile)
        self.api.update_profile(
            name=original.name, url=original.url,
            location=original.location, description=original.description
        )

        for k, v in profile.items():
            if k == 'email':
                continue
            self.assertEqual(getattr(updated, k), v)

    def testfavorites(self):
        self.api.favorites()

    def testcreatedestroyfavorite(self):
        self.api.create_favorite(4901062372)
        self.api.destroy_favorite(4901062372)

    def testenabledisablenotifications(self):
        self.api.enable_notifications('twitter')
        self.api.disable_notifications('twitter')

    def testcreatedestroyblock(self):
        self.api.create_block('twitter')
        self.assertEqual(self.api.exists_block('twitter'), True)
        self.api.destroy_block('twitter')
        self.assertEqual(self.api.exists_block('twitter'), False)
        self.api.create_friendship('twitter')  # restore

    def testblocks(self):
        self.api.blocks()

    def testblocksids(self):
        self.api.blocks_ids()

    def testcreateupdatedestroylist(self):
        self.api.create_list('tweeps')
        # XXX: right now twitter throws a 500 here,
        # issue is being looked into by twitter.
        # self.api.update_list('tweeps', mode='private')
        self.api.destroy_list('tweeps')

    def testlists(self):
        self.api.lists()

    def testlistsmemberships(self):
        self.api.lists_memberships()

    def testlistssubscriptions(self):
        self.api.lists_subscriptions()

    def testlisttimeline(self):
        self.api.list_timeline('applepie', 'stars')

    def testgetlist(self):
        self.api.get_list('applepie', 'stars')

    def testlistmembers(self):
        self.api.list_members('applepie', 'stars')

    def testislistmember(self):
        uid = self.api.get_user('applepie').id
        self.api.is_list_member('applepie', 'stars', uid)

    def testsubscribeunsubscribelist(self):
        self.api.subscribe_list('applepie', 'stars')
        self.api.unsubscribe_list('applepie', 'stars')

    def testlistsubscribers(self):
        self.api.list_subscribers('applepie', 'stars')

    def testissubscribedlist(self):
        uid = self.api.get_user('applepie').id
        self.api.is_subscribed_list('applepie', 'stars', uid)

    def testsavedsearches(self):
        s = self.api.create_saved_search('test')
        self.api.saved_searches()
        self.assertEqual(self.api.get_saved_search(s.id).query, 'test')
        self.api.destroy_saved_search(s.id)

    def testsearch(self):
        self.api.search('tweepy')

    def testtrends(self):
        self.api.trends_daily()
        self.api.trends_weekly()

    def testgeoapis(self):
        self.api.geo_id(id='c3f37afa9efcf94b')  # Austin, TX, USA
        self.api.nearby_places(lat=30.267370168467806,
                               long=-97.74261474609375)  # Austin, TX, USA
        self.api.reverse_geocode(lat=30.267370168467806,
                                 long=-97.74261474609375)  # Austin, TX, USA
Ejemplo n.º 8
0
def forecast(city):
    config = ConfigParser.RawConfigParser()
    config.read('settings.cfg')
    WOEID = config.get(city, 'WOEID')
    baseurl = "https://query.yahooapis.com/v1/public/yql?"
    yql_query = ("select * from weather.forecast where woeid=" + WOEID)
    yql_url = baseurl + urllib.urlencode({'q': yql_query}) + "&format=xml"
    forecastfile = urllib.urlopen(yql_url)
    tree = ET.parse(forecastfile)
    query = tree.getroot()
    root = query[0]
    channel = root[0]
    item = channel[12]
    description = item[5]
    forecast = item[7]
    high = forecast.attrib['high']
    low = forecast.attrib['low']
    forecastText = forecast.attrib['text']
    currentTemp = description.attrib['temp']
    currentText = description.attrib['text']
    currentC = description.attrib['code']
    currentCondition = int(currentC)
    timeStamp = description.attrib['date']
    forecastfile.close()

    rainCodes = [
        1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 18, 35, 37, 38, 39, 40, 47, 45, 46
    ]
    fairCodes = [31, 32, 33, 34]
    overcastCodes = [26, 27, 28]
    snowCodes = [13, 14, 15, 16, 41, 42, 43]
    uniqueCodes = [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 17, 18, 19, 20, 21, 22, 23, 24, 35, 37,
        38, 39, 40, 44, 45, 46, 47
    ]
    blankCodes = [9, 11, 12, 25, 29, 30, 36, 3200]

    if currentCondition in rainCodes:
        yes_choices = [
            'Yes', 'Yes', 'Yea', 'Yep', 'Ya', 'Grab an umbrella',
            "It's raining"
        ]
        a = random.choice(yes_choices)

    else:
        no_choices = ['No', 'Nah', 'Nope', 'Not raining', 'Not raining']
        a = random.choice(no_choices)

    if currentCondition in fairCodes:
        fair_choices = [
            ", beautiful day", ", clear day", ", nice day", ", fair weather",
            ""
        ]
        comment = random.choice(fair_choices)

    if currentCondition in overcastCodes:
        overcast_choices = [
            ", gloomy", ", cloudy", ", overcast", ", grey skies", ""
        ]
        comment = random.choice(overcast_choices)

    if currentCondition in snowCodes:
        snow_choices = [
            ", snowing", ", snow", ", snowfall", ", snow coming down"
        ]
        comment = random.choice(snow_choices)

    if currentCondition in uniqueCodes:
        uniqueChoice = str(", " + currentText)
        unique_choices = [uniqueChoice, ""]
        comment = random.choice(unique_choices)

    if currentCondition in blankCodes:
        comment = str('')

    if 'PM' in timeStamp:
        timeStamp = "w/ low tonight of "
        tempHL = low
    else:
        timeStamp = "w/ high today of "
        tempHL = high
        q = taskqueue.Queue(city)
        q.purge()

    a = a.rstrip("\r\n")
    comment = comment.rstrip("\r\n")
    comment = comment.lower()
    forecastText = forecastText.lower()

    answer = (a + comment + '.\n' + currentTemp + '° now ' + timeStamp +
              tempHL + '°\n' + "Forecast: " + forecastText + '.')
    logging.info(answer)
    CONSUMER_KEY = config.get(city, 'CONSUMER_KEY')
    CONSUMER_SECRET = config.get(city, 'CONSUMER_SECRET')
    ACCESS_TOKEN = config.get(city, 'ACCESS_TOKEN')
    ACCESS_TOKEN_SECRET = config.get(city, 'ACCESS_TOKEN_SECRET')
    auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
    api = API(auth)
    result = api.update_status(status=answer)
Ejemplo n.º 9
0
class Twitter(ApiInterface):
    def __init__(self) -> None:
        """ Store easy access for keys """
        self.keys = TwitterKey()
        """ Store pointer for OAuth access """
        auth = OAuthHandler(self.keys.consumer_pub, self.keys.consumer_sec)
        auth.set_access_token(self.keys.access_pub, self.keys.access_sec)
        self.auth = auth
        self.api = API(auth)
        """ Store easy access for twitter info operations """
        self.info = TwitterInfo(self.api)
        self.bio = TwitterBio(self.api)
        """ Contains info for real-time graphing """
        self.streamfile = os.path.join('postr', 'twitter',
                                       'twitter_stream.txt')
        self.graphfile = os.path.join('postr', 'twitter',
                                      'twitter_graphing.csv')
        self.blobfile = os.path.join('postr', 'twitter', 'twitter_blob.csv')

    def post_text(self, text: str) -> bool:
        """ Posts a tweet containing text """
        try:
            self.api.update_status(status=text)
            return True
        except BaseException as e:
            print(e)
            return False

    # pylint: disable=no-self-use, unused-argument
    def post_video(self, url: str, text: str) -> bool:
        """ Not applicable """
        return False

    def post_photo(self, url: str, text: str) -> bool:
        """ Posts a tweet with text and a picture """
        try:
            self.api.update_with_media(filename=url, status=text)
            return True
        except BaseException as e:
            print(e)
            return False

    def get_user_followers(self, text: str) -> List[str]:
        """ Gets user followers, note: this is rate limited """
        my_followers = []
        i = 0

        # Use the cursor module for pagination
        for follower in Cursor(self.api.followers, screen_name=text).items():
            my_followers.append(follower.screen_name)
            i += 1

            # Simple rate limit for requests
            if i >= 100:
                i = 0
                time.sleep(1)

        return my_followers

    def remove_post(self, post_id: str) -> bool:
        """ Removes a tweet given its ID """
        try:
            self.api.destroy_status(post_id)
            return True
        except BaseException as e:
            print(e)
            return False

    def stream_tweets(self, hashtags: List[str], output_filename: str) -> None:
        """ Streams tweets from a hashtag and writes data into an output file """
        self.setup_csv()
        twitter_streamer = TwitterStreamer(self.keys, self.graphfile)
        twitter_streamer.stream_tweets(hashtags, output_filename, self.auth)
        print('done streaming')

    def setup_csv(self) -> None:
        """ Initializes a csv file for time series graphing """
        csvData = ['Tweet', 'Time']

        with open(self.graphfile, 'w') as csvFile:
            writer = csv.writer(csvFile)
            writer.writerow(csvData)
            csvFile.close()

    # pylint: disable=no-self-use, unused-argument
    def get_user_likes(self) -> int:
        """ Not applicable, see helper methods in TwitterInfo class"""
        return -1

    def read_csv_col(self, colNum: int, filename: str) -> List[str]:
        """ Reads a specific column by index in the graph csv"""
        col = []
        with open(filename, 'r') as rf:
            reader = csv.reader(rf, delimiter=',')
            for row in reader:
                col.append(str(row[colNum]))

        return col[1::]  # Ignore the csv header

    def analyzeSentiment(self) -> None:
        """ Converts a real-time tweet content into a positivity score"""
        with open(self.blobfile, 'w') as bf:
            writer = csv.writer(bf)
            graph_data = zip(
                self.read_csv_col(0, self.graphfile),
                self.read_csv_col(1, self.graphfile),
            )

            for pair in graph_data:
                text = str(re.sub(r'[^a-zA-Z ]+', '', pair[0]))
                score = Twitter.polarity(text)
                writer.writerow([pair[1], score])

            bf.close()

    @staticmethod
    def polarity(text: str) -> float:
        """ Returns the polarity of text. Made into a separate
            method to provide easy modification if needed in the future """
        return float(TextBlob(text).sentiment.polarity)

    def stream_and_graph(self, hashtags: List[str]) -> None:
        """ Streams tweets in real time, then graphs their sentiment """
        self.stream_tweets(hashtags, self.streamfile)
        self.analyzeSentiment()
        self.graph_blob()

    def graph_blob(self) -> None:
        """ Graphs a blob file for twitter sentiment """
        dates = self.read_csv_col(0, self.blobfile)
        # Truncate the datetime object to the minute precision
        dates = [d[:DATETIME_MILLISECOND_PRECISION] for d in dates]

        # Truncate off scores past a precision for easy viewing on the plot
        scores = list(
            map(lambda x: x[:SCORE_PRECISION],
                self.read_csv_col(1, self.blobfile)))

        plt.plot(
            dates,
            scores,
        )

        plt.ylabel('Positivity Score')
        plt.xlabel('Time')

        # beautify the x-labels
        plt.gcf().autofmt_xdate()

        plt.show()

    def update_bio(self, message: str) -> None:
        """ Sets an authenticated user's bio to a specified message """
        self.api.update_profile(description=message)
Ejemplo n.º 10
0
# -*- coding: utf-8 -*-
'''
Created on 2011-8-27

@author: redswallow
'''

from tweepy.auth import OAuthHandler
from tweepy.api import API

consumer_key="o2K22DnJqSG0STjRbLUA";consumer_secret ="SV7I5YxQ8ehDCEBnKmCHYMTJW0Z0MLt3kpEdW9KhaCo"
token="25798843-PyBwBx4AWqjUuSAm9yoKQuSvEtZQR78IEsuB7xGw";tokenSecret="nWhpP3g44eciBs0Db5SXQc8HJ0G53Rd2v4sAGJy3aTU"

#get_api
auth=OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(token, tokenSecret)
api=API(auth)

api.update_status(u"oauth登录成功~~")
Ejemplo n.º 11
0
def update_reply(text, reply_id, screen_name):
    auth = get_oauth()
    api = API(auth)
    st = "@" + str(screen_name) + " " + str(text)
    api.update_status(status=st, in_reply_to_status_id=reply_id)
Ejemplo n.º 12
0
def update_tweet(text):
    auth = get_oauth()
    api = API(auth)
    api.update_status(status=text)