Example #1
0
    def testbasicauth(self):
        auth = BasicAuthHandler(username, password)

        # test accessing twitter API
        api = API(auth)
        s = api.update_status('test %i' % random.randint(1, 1000))
        api.destroy_status(s.id)
Example #2
0
def tweet(title, url, location=None, parsed_location=None):
    auth = OAuthHandler(app.config['TWITTER_CONSUMER_KEY'], app.config['TWITTER_CONSUMER_SECRET'])
    auth.set_access_token(app.config['TWITTER_ACCESS_KEY'], app.config['TWITTER_ACCESS_SECRET'])
    api = API(auth)
    urllength = 23  # Current Twitter standard for HTTPS (as of Oct 2014)
    maxlength = 140 - urllength - 1 # == 116
    locationtag = u''
    if parsed_location:
        locationtags = []
        for token in parsed_location.get('tokens', []):
            if 'geoname' in token and 'token' in token:
                locname = token['token'].strip()
                if locname:
                    locationtags.append(u'#' + locname.title().replace(u' ', ''))
        locationtag = u' '.join(locationtags)
        if locationtag:
            maxlength -= len(locationtag) + 1
    if not locationtag and location:
        # Make a hashtag from the first word in the location. This catches
        # locations like 'Anywhere' which have no geonameid but are still valid
        locationtag = u'#' + re.split('\W+', location)[0]
        maxlength -= len(locationtag) + 1

    if len(title) > maxlength:
        text = title[:maxlength-1] + u'…'
    else:
        text = title[:maxlength]
    text = text + ' ' + url  # Don't shorten URLs, now that there's t.co
    if locationtag:
        text = text + ' ' + locationtag
    api.update_status(text)
Example #3
0
def FeedTwitter():
    auth = OAuthHandler(CONSUMER_KEY_TWITTER, CONSUMER_SECRET_TWITTER)
    auth.set_access_token(ACCESS_TOKEN_TWITTER, ACCESS_TOKEN_SECRET_TWITTER)
    api = API(auth)
    feed = api.user_timeline(screen_name=CONST_USER_TWITTER, count=COUNT_NUMBER_POST, page=1, include_rts=True)
    tweets = []
    for t in feed:
        if t.in_reply_to_status_id == None:
            try:
                tweet = {"text": t.text, "text_encoded": t.text, "type": "tweet", "created_time": t.created_at,
                         "link":"https://twitter.com/"+CONST_USER_LINK_TWITTER+"/status/" + str(t.id),
                         "link_encoded":urllib.quote_plus("https://twitter.com/"+CONST_USER_LINK_TWITTER+"/status/" + str(t.id)),
                         "user": {"name": t.user.name, "screen_name": "@" + t.user.screen_name,"profile_image":t.user.profile_image_url}}
                if "media" in t.entities:
                    if t.entities['media'][0]['type'] == 'photo':
                        tweet["image"] = t.entities['media'][0]['media_url']
                    else:
                        pass
                        #print t.entities['media'][0]['type']
                else:
                    pass
                    #tweet["image"] = get_randon_image()
                tweets.append(tweet)
            except Exception, e:
                #print(str(e))
                pass
Example #4
0
    def authenticate(self, access_token_key='', access_token_secret='', consumer_key='', consumer_secret=''):
        handler = OAuthHandler(consumer_key, consumer_secret)
        handler.set_access_token(access_token_key, access_token_secret)

        api = API(handler)
        twitter_user = api.verify_credentials()

        try:
            twitter_id = twitter_user.id
        except AttributeError:
            user = None
        else:
            try:
                user = User.objects.get(username=twitter_id)
            except User.DoesNotExist:
                user = User.objects.create_user(twitter_id)

            profile = user.get_profile()

            profile.access_token_key = handler.access_token.key
            profile.access_token_secret = handler.access_token.secret

            profile.save()

        return user
Example #5
0
def get_tweets(count=5):

    try:
        consumer_key = environ['TWITTER_CONSUMER_KEY']
        access_token = environ['TWITTER_ACCESS_TOKEN']
        consumer_secret = environ['TWITTER_CONSUMER_SECRET']
        access_secret = environ['TWITTER_ACCESS_SECRET']
    except KeyError:
        error("No Twitter credentials were found.")
        # We don't have login stuff, bail.
        return []

    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    api = API(auth_handler=auth)

    try:
        statuses = api.user_timeline('pythonglasgow', count=count)
    except TweepError:
        error("Failed to read timelime.")
        return []

    tweets = [(status, twitterfy(status.text)) for status in statuses]

    return tweets
Example #6
0
    def unwrapped_callback(self, resp):
        if resp is None:
            raise LoginCallbackError(_("You denied the request to login"))

        # Try to read more from the user's Twitter profile
        auth = TwitterOAuthHandler(self.consumer_key, self.consumer_secret)
        if self.access_key is not None and self.access_secret is not None:
            auth.set_access_token(self.access_key, self.access_secret)
        else:
            auth.set_access_token(resp['oauth_token'], resp['oauth_token_secret'])
        api = TwitterAPI(auth)
        try:
            twinfo = api.lookup_users(user_ids=[resp['user_id']])[0]
            fullname = twinfo.name
            avatar_url = twinfo.profile_image_url_https.replace('_normal.', '_bigger.')
        except TweepError:
            fullname = None
            avatar_url = None

        return {'userid': resp['user_id'],
                'username': resp['screen_name'],
                'fullname': fullname,
                'avatar_url': avatar_url,
                'oauth_token': resp['oauth_token'],
                'oauth_token_secret': resp['oauth_token_secret'],
                'oauth_token_type': None,  # Twitter doesn't have token types
                }
Example #7
0
class FaveRetweeter(object):

    def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret):
        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret

        self.access_token = access_token
        self.access_token_secret = access_token_secret

        self._authorize()

    def _authorize(self):
        oauth_handler = OAuthHandler(self.consumer_key, self.consumer_secret)
        oauth_handler.set_access_token(self.access_token, self.access_token_secret)

        self.tweepy = API(oauth_handler)

    def retweet_fave(self):
        me = self.tweepy.me()

        favorite_count = me.favourites_count
        page = randrange(favorite_count)

        favorites = self.tweepy.favorites(count=1, page=page)
        favorite = favorites[0]

        _retweet(favorite, me)
    def __init__(self, term='', oauthfile=''):
        threading.Thread.__init__(self)
        self.searchterm = term
        self.name = term
        self.consume = True
        
        listener = MongoDBListener()
        
        try:
            oauth = json.loads(open(oauthfile, 'r').read())
            
            if 'consumer_key' in oauth:
                auth = OAuthHandler(oauth['consumer_key'], oauth['consumer_secret'])
                auth.set_access_token(oauth['access_token'], oauth['access_token_secret'])
                api = API(auth)

                if not api.verify_credentials():
                    raise Exception("Invalid credentials")
            else:
                auth = BasicAuthHandler(oauth['username'], oauth['password'])

        except:
            print "Error logging to Twitter"
            raise
    
        self.stream = Stream(auth, listener, timeout=60)  
Example #9
0
    def unwrapped_callback(self, resp):
        if resp is None:
            raise LoginCallbackError(_("You denied the request to login"))

        # Try to read more from the user's Twitter profile
        auth = TwitterOAuthHandler(self.consumer_key, self.consumer_secret)
        auth.set_access_token(resp['oauth_token'], resp['oauth_token_secret'])
        api = TwitterAPI(auth)
        try:
            twinfo = api.verify_credentials(include_entities='false', skip_status='true', include_email='true')
            fullname = twinfo.name
            avatar_url = twinfo.profile_image_url_https.replace('_normal.', '_bigger.')
            email = getattr(twinfo, 'email', None)
        except TweepError:
            fullname = None
            avatar_url = None
            email = None

        return {'email': email,
                'userid': resp['user_id'],
                'username': resp['screen_name'],
                'fullname': fullname,
                'avatar_url': avatar_url,
                'oauth_token': resp['oauth_token'],
                'oauth_token_secret': resp['oauth_token_secret'],
                'oauth_token_type': None,  # Twitter doesn't have token types
                }
Example #10
0
class TwitterPlayer(player.Player):
    def __init__(self, model, code, access_token, access_token_secret, opponent):
        player.Player.__init__(self, model, code)
        self._opponent = opponent
        self._last_id = None

        self._auth = OAuthHandler(auth.consumer_key, auth.consumer_secret)
        self._auth.set_access_token(access_token, access_token_secret)
        self._api = API(self._auth)
        self._listener = TwitterListener(self, self._api)
        self._stream = Stream(self._auth, self._listener)

    @property
    def username(self):
        return self._auth.get_username()

    def allow(self):
        print 'This is the opponent\'s turn...'
        self._stream.userstream()

    def update(self, event):
        if event.player == self.code:
            return
        message = '@%s %s' % (self._opponent, self._model.events[-1][1])
        self.tweet(message)

    def tweet(self, message):
        if self._last_id is None:
            self._api.update_status(message)
        else:
            self._api.update_status(message, self._last_id)
Example #11
0
def tweet(title, url, location=None, parsed_location=None, username=None):
    auth = OAuthHandler(app.config["TWITTER_CONSUMER_KEY"], app.config["TWITTER_CONSUMER_SECRET"])
    auth.set_access_token(app.config["TWITTER_ACCESS_KEY"], app.config["TWITTER_ACCESS_SECRET"])
    api = API(auth)
    urllength = 23  # Current Twitter standard for HTTPS (as of Oct 2014)
    maxlength = 140 - urllength - 1  # == 116
    if username:
        maxlength -= len(username) + 2
    locationtag = u""
    if parsed_location:
        locationtags = []
        for token in parsed_location.get("tokens", []):
            if "geoname" in token and "token" in token:
                locname = token["token"].strip()
                if locname:
                    locationtags.append(u"#" + locname.title().replace(u" ", ""))
        locationtag = u" ".join(locationtags)
        if locationtag:
            maxlength -= len(locationtag) + 1
    if not locationtag and location:
        # Make a hashtag from the first word in the location. This catches
        # locations like 'Anywhere' which have no geonameid but are still valid
        locationtag = u"#" + re.split("\W+", location)[0]
        maxlength -= len(locationtag) + 1

    if len(title) > maxlength:
        text = title[: maxlength - 1] + u"…"
    else:
        text = title[:maxlength]
    text = text + " " + url  # Don't shorten URLs, now that there's t.co
    if locationtag:
        text = text + " " + locationtag
    if username:
        text = text + " @" + username
    api.update_status(text)
def get_tweets(tweeter_id, from_id = None):
    #Setup
    #l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = API(auth)

    #Get tweets (status) from Twitter A
    if from_id == None:
        status = api.user_timeline(user_id = tweeter_id, count=amont_of_tweets)
    else:
        status = api.user_timeline(user_id = tweeter_id, count=amont_of_tweets, max_id = from_id)
        status.pop(0) #Remove duplicate first tweet, max_id not included.

    tweetlist = []
    last_id = 0

    #print("Cleaning tweets from :", status.user.screen_name, "id: ", tweeter_id)
    for items in status:
        tweet = {}
        tweet["id"] = items.id
        tweet["user_id"] = tweeter_id
        tweet["nickname"] = items.user.screen_name
        tweet["realname"] = items.user.name
        tweet["date"] = datetime.strptime(str(items.created_at), "%Y-%m-%d %H:%M:%S")
        tweet["text"] = items.text

        tweetlist.append(tweet)
        last_id = items.id

    print("Last ID: ", last_id, "\n")
    return tweetlist, last_id
Example #13
0
    def get(self, request, *args, **kwargs):
        """
        The GET method controller for the API endpoint and saves the results
        to a CSV file.
        Accepts query strings:
            - keyword: the keyword that searched for
            - count: number of results retrieved (default = 100)
        """
        keyword = request.GET.get('keyword')
        count = request.GET.get('count', 100)
        if not keyword:
            return Response(HTTP_400_BAD_REQUEST)

        auth = twitter_auth()
        api = API(auth, parser=JSONParser())
        data = []
        results = api.search(q=keyword, count=count)
        filename = 'search_{}_{}.csv'.format(
            keyword, datetime.now().isoformat()
        )
        for result in results['statuses']:
            data.append({
                'user': parse_tweet(result['user']['name']),
                'tweet': parse_tweet(result['text'])
            })
        save_to_csv(filename, data)
        response_text = 'Saved {} objects.'.format(
            results['search_metadata']['count']
        )
        return Response(response_text)
Example #14
0
    def unwrapped_callback(self, resp):
        if resp is None:
            raise LoginCallbackError(_("You denied the request to login"))

        # Try to read more from the user's Twitter profile
        auth = TwitterOAuthHandler(self.consumer_key, self.consumer_secret)
        if self.access_key is not None and self.access_secret is not None:
            auth.set_access_token(self.access_key, self.access_secret)
        else:
            auth.set_access_token(resp["oauth_token"], resp["oauth_token_secret"])
        api = TwitterAPI(auth)
        try:
            twinfo = api.lookup_users(user_ids=[resp["user_id"]])[0]
            fullname = twinfo.name
            avatar_url = twinfo.profile_image_url_https.replace("_normal.", "_bigger.")
        except TweepError:
            fullname = None
            avatar_url = None

        return {
            "userid": resp["user_id"],
            "username": resp["screen_name"],
            "fullname": fullname,
            "avatar_url": avatar_url,
            "oauth_token": resp["oauth_token"],
            "oauth_token_secret": resp["oauth_token_secret"],
            "oauth_token_type": None,  # Twitter doesn't have token types
        }
class StreamConsumerThreadClass(threading.Thread):
    def __init__(self, term="", oauthfile="", follow=False, user=False, track=False):
        threading.Thread.__init__(self)
        self.searchterm = term
        self.name = term
        self.consume = True
        self.follow = follow
        self.user = user
        self.track = track
        listener = MongoDBListener()

        try:
            oauth = json.loads(open(oauthfile, "r").read())

            if "consumer_key" in oauth:
                auth = OAuthHandler(oauth["consumer_key"], oauth["consumer_secret"])
                auth.set_access_token(oauth["access_token"], oauth["access_token_secret"])
                self.api = API(auth)

                if not self.api.verify_credentials():
                    raise Exception("Invalid credentials")

                self.stream = Stream(auth, listener, timeout=60)

        except:
            print "Error logging to Twitter"
            raise

    def stop_consume(self):
        self.stream.disconnect()

    def run(self):
        now = datetime.datetime.now()
        if self.user:
            print "Twitter Stream of the OAuth user: started at: %s" % (now)
        else:
            print "Twitter Stream with terms: %s started at: %s" % (self.getName(), now)

        connected = False
        while True:
            try:
                if not connected:
                    connected = True
                    if self.user:
                        self.stream.userstream(_with="followings")
                    elif self.follow:
                        user_ids = []
                        for user in self.api.lookup_users([], self.searchterm.split(","), False):
                            user_ids.append(user.id)
                        self.stream.filter(follow=[",".join("{0}".format(n) for n in user_ids)])
                    elif self.track:
                        self.stream.filter(track=[self.searchterm])

            except SSLError, sse:
                print sse
                connected = False
            except Exception, e:
                print "Stream error"
                raise e
Example #16
0
class Twitter():
  def __init__(self, config = None):
    auth = OAuthHandler(unicode(config.consumerKey), unicode(config.consumerSecret))
    auth.set_access_token(unicode(config.accessToken), unicode(config.accessTokenSecret))
    self._api = API(auth)

  def tweet(self, message):
    self._api.update_status(message.encode('utf-8'))
def send_reply(tweetID,screen_name):
	try:
		auth = OAuthHandler(consumer_key, consumer_secret)
		auth.set_access_token(access_token, access_token_secret)
		api = API(auth)
		### at this point I've grabbed the tweet and loaded it to JSON...
		status_message = "@%s You are not supposed to use such words. @stophatebot " % (screen_name)
		api.update_status(status_message, tweetID)
	except Exception, e:
		print e
Example #18
0
def get_location_trends(locations, auth):

	api = API(auth)

	trends = api.trends_place(locations)
	data = trends[0]
	trends_data = data['trends']
	names = [trend['name'] for trend in trends_data]

	print names
Example #19
0
def main():
   	try:
		auth = OAuthHandler(consumer_key, consumer_secret)
		auth.secure = True
		auth.set_access_token(access_token, access_token_secret)
		api = API(auth)
		print(api.me().name)
		stream = Stream(auth, StdOutListener())
		stream.userstream()

    	except BaseException as e:
        	print("Error in main()", e)
Example #20
0
class TweetReply():
    def __init__(self):
        auth = OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)
        self.api = API(auth)

    def tweet_in_response(self, screen_name, tweet_id, url):
        # self.api.update_status("@" + screen_name + " Hi! Could you please click the link to stream your surroundings? " + url, tweet_id)
        self.api.update_status("@" + screen_name + " We support you!", tweet_id)

    def tweet_to_person(self, screen_name):
        # self.api.update_status("@" + screen_name + " Hi! Could you please click the link to stream your surroundings? " + url, tweet_id)
        self.api.update_status("@" + screen_name + " Somebody requested a live stream from you: http://streamy.co/streamer/" + screen_name)
Example #21
0
def post_gettweets():
    page = 1
    keyword = bottle.request.forms.get("tweetKeyword")
    maxPage = bottle.request.forms.get("pages")
    print keyword
    print maxPage
    results =[]
    twitter_api = API()
    for page in range(int(maxPage)):
        tweets = twitter_api.search(keyword, page=page)
        for tweet in tweets:
            result.append(tweet)
    print len(result)
Example #22
0
    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'))

        api = API(BasicAuthHandler('bad', 'password'))
        self.assertEqual(api.verify_credentials(), False)
def start():


	# setup Twitter API connection details
	twitter_auth = OAuthHandler( TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET )
	twitter_auth.set_access_token( TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET )

	tweet_api = API(twitter_auth)

	public_tweets = tweet_api.home_timeline(count = 1) #count=1 to read only one tweet

	for tweet in public_tweets:

		print ('{}'.format(tweet))
Example #24
0
    def authenticate(self, access_token):
        """
        Twitterから取得したアクセストークンをもとに認証を行う。

        :param access_token: アクセストークン
        :type access_token: tuple
        :return: ユーザー情報
        :rtype: django.contrib.auth.models.User
        """
        # APIオブジェクトを構築する
        oauth_handler = OAuthHandler(settings.CONSUMER_KEY, settings.CONSUMER_SECRET)
        oauth_handler.set_access_token(access_token[0], access_token[1])
        api = API(oauth_handler)

        # ログインユーザーのTwitter情報を取得する
        try:
            twitter_user = api.me()
        except TweepError:
            return None

        # Profile/Userを取得/作成する
        try:
            profile = Profile.objects.get(twitter_id=twitter_user.id)
            user = profile.user
        except Profile.DoesNotExist:
            # Userを作成する
            user = User()
            user.username = twitter_user.id
            user.first_name = twitter_user.screen_name
            user.last_name = twitter_user.name
            user.set_unusable_password()
            user.save()

            # Profileを作成する
            profile = Profile()
            profile.twitter_id = twitter_user.id
            profile.screen_name = twitter_user.screen_name
            profile.name = twitter_user.name
            profile.description = twitter_user.description
            profile.url = twitter_user.url
            profile.profile_image_url = twitter_user.profile_image_url
            profile.user = user
            profile.save()

        # 有効なユーザーであるかチェックする
        if user.is_active:
            return user
        else:
            return None
class TwitterHandler( StreamListener ):


    def __init__( self ):
        self.logger = logging.getLogger(__name__)

        try:
            self.auth = OAuthHandler(twitter_secrets.APP_KEY, twitter_secrets.APP_SECRET)
            self.auth.secure = True
            self.auth.set_access_token(twitter_secrets.OAUTH_TOKEN, twitter_secrets.OAUTH_TOKEN_SECRET)

            self.api = API(self.auth)

            # If the authentication was successful, you should
            # see the name of the account self.logger.info out
            self.logger.info(self.api.me().name)

            self.stream = Stream(self.auth, self)
            self.stream.userstream(async = True)

        except BaseException as e:
            self.logger.info("Error in __init__", e)

    def on_connect( self ):
        self.logger.info("Connection established!!")

    def on_disconnect( self, notice ):
        self.logger.info("Connection lost!! : ", notice)

    def on_data( self, status ):
        self.logger.info("Entered on_data()")
        data = json.loads(status)
        if 'direct_message' in data:
            name = data['direct_message']['sender_screen_name']
            text = data['direct_message']['text']
            m = models.Message(source = "twitter", name = name, message = text, rec_by = "", response = "")
            m.save()
            self.logger.info("Name: " + name + " Text: " + text)
        return True

    def on_error( self, status ):
        self.logger.info(status)

    def sendMessage( self, name, message):
        if(self.api.me().screen_name != name):
            self.api.send_direct_message(screen_name = name, text = message)
            self.logger.info("successfully sent " + message + " to " + name)
        else:
            self.logger.info("Cannot send message to yourself")
Example #26
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)
Example #27
0
def update_status(text):

    try:
        consumer_key = environ['TWITTER_CONSUMER_KEY']
        access_token = environ['TWITTER_ACCESS_TOKEN']
        consumer_secret = environ['TWITTER_CONSUMER_SECRET']
        access_secret = environ['TWITTER_ACCESS_SECRET']
    except KeyError:
        error("No Twitter credentials were found.")
        # We don't have login stuff, bail.
        return

    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    api = API(auth_handler=auth)
    api.update_status(status=text)
Example #28
0
def send_dm(username, text):

    try:
        consumer_key = environ['TWITTER_CONSUMER_KEY']
        access_token = environ['TWITTER_ACCESS_TOKEN']
        consumer_secret = environ['TWITTER_CONSUMER_SECRET']
        access_secret = environ['TWITTER_ACCESS_SECRET']
    except KeyError:
        error("No Twitter credentials were found.")
        # We don't have login stuff, bail.
        return

    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_secret)
    api = API(auth_handler=auth)
    api.send_direct_message(screen_name=username, text=text)
Example #29
0
 def init_api(self):
     oauth_handler = TweepyOAuthHandler(self._consumer_key,
                                        self._consumer_secret,
                                        secure=configuration.twitter['use_https'])
     oauth_handler.set_access_token(self._access_token_key,
                                    self._access_token_secret)
     self._api = BaseTweepyApi(oauth_handler, secure=configuration.twitter['use_https'])
Example #30
0
def start():
    global redis_conn
    redis_conn = redis.StrictRedis(host="localhost", port=6379, db=0)
    consumer_key = config.get("twitter", "client_id")
    consumer_secret = config.get("twitter", "client_secret")
    access_token = config.get("twitter", "access_token")
    access_token_secret = config.get("twitter", "access_token_secret")

    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = API(auth)
    friend_ids = api.friends_ids()
    stream = Stream(auth, l)
    #        ,track=['startup','entrepreneurship','marketing','SEO']
    stream.filter(follow=friend_ids, track=["clojure"])  # ,track=[
Example #31
0
class Twitter:
    '''
    :param credential: class that contains objects like administrator_data -> object
    '''

    def __init__(self, credential):
        '''
        initialize twitter with tweepy
        Attributes:
            - credential
            - api
            - AdminCmd
            - UserCmd
            - me
            - follower
            - followed
            - random_time
            - db_sent
            - day
            - db_received
            - indicator_start
            - db_intervalTime
        
        :param credential: class that contains objects like administrator_data -> object
        '''
        self.credential = credential

        print("Initializing twitter...")
        auth = OAuthHandler(
            credential.CONSUMER_KEY, credential.CONSUMER_SECRET)
        auth.set_access_token(
            credential.ACCESS_KEY, credential.ACCESS_SECRET)
        self.api = API(
            auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
        
        self.AdminCmd = AdminCommand(self.api, credential)
        self.UserCmd = UserCommand(self.api, credential)

        self.me = self.api.me()
        self.follower = list() # list of integer
        self.followed = list() # list of integer
        self.random_time = credential.Delay_time
        self.db_sent = dict() # dict of sender and his postid, update every midnight with self.day
        self.day = (datetime.now(timezone.utc) + timedelta(hours=credential.Timezone)).day
        self.db_received = list() # list of 55 received menfess's message id
        self.indicator_start = False
        self.db_intervalTime = dict()
    

    def get_all_followers(self, user_id, first_delay=True):
        '''Return all followers ids
        Twitter API limiting to get 5000 followers/minute
        :param user_id: User id -> int or str
        :param first_delay: False: delete delay for first operation -> bool
        :returns: list of followers ids integer
        '''
        try:
            print("Getting all followers ids...")
            ids = list()
            for page in Cursor(self.api.followers_ids, user_id=user_id).pages():
                ids.extend(page)
                if first_delay is False:
                    first_delay = True
                    continue
                sleep(60)
            return ids

        except Exception as ex:
            pass
            print(ex)
            sleep(60)
            return list()

    
    def get_all_followed(self, user_id, first_delay=True):
        '''Get all account ids that followed by screen_name
        Twitter api limiting to get 5000 followed/minute
        :param user_id: user id -> str or int
        :param first_delay: False: delete delay for first operation -> bool
        :returns: list of followers ids integer
        '''
        try:
            print("Getting all friends ids...")
            ids = list()
            for page in Cursor(self.api.friends_ids, user_id=user_id).pages():
                ids.extend(page)
                if first_delay is False:
                    first_delay = True
                    continue
                sleep(60)
            return ids

        except Exception as ex:
            pass
            print(ex)
            sleep(60)
            return list()
    

    def db_sent_updater(self, action, sender_id=None, postid=None):
        '''Update self.db_sent
        :param action: 'update','add' or 'delete' -> str
        :param sender_id: sender id who has sent the menfess -> str
        :param postid: tweet id or (sender_id, tweet id) -> str or tuple
        '''
        try:
            if action == 'update':
                day = (datetime.now(timezone.utc) + timedelta(hours=self.credential.Timezone)).day
                if day != self.day:
                    self.day = day
                    self.db_sent.clear()
            
            elif action == 'add':
                if sender_id not in self.db_sent:
                    self.db_sent[sender_id] = [postid]
                else: self.db_sent[sender_id] += [postid]
            
            elif action == 'delete':
                self.db_sent[sender_id].remove(postid)
                if len(self.db_sent[sender_id]) == 0:
                    del self.db_sent[sender_id]

        except Exception as ex:
            pass
            print(ex)


    def read_dm(self):
        '''Read and filter DMs
        This method contains AdminCmd and UserCmd that can do exec and
        self.db_sent updater.
        Filters:
            - received dm
            - admin & user command
            - interval per sender
            - account status
            - blacklist words
            - only followed
            - sender requirements
            - menfess trigger
                - attachment_url
                - photo
                - video
                - animated_gif
        :returns: list of dict filtered DMs
        '''
        # Update db_sent
        self.db_sent_updater('update')

        print("Getting direct messages...")
        dms = list()
        try:
            api = self.api
            dm = api.list_direct_messages(count=50)

            # FILL DB_RECEIVED WHEN BOT WAS JUST STARTED (Keep_DM)
            if self.indicator_start is False:
                self.indicator_start = True

                if self.credential.Keep_DM is True:
                    for x in dm:
                        self.db_received.append(x.id)
                    # Ignore messages that received before bot started
                    return dms
            
            # Check Account_status to make process (after Turned off) faster
            if self.credential.Account_status is True:
                dm.reverse()
            else:
                for x in range(len(dm)):
                    message = dm[x].message_create['message_data']['text'].lower()
                    if "#switch on" in message:
                        self.credential.Account_status = True
                        self.delete_dm(dm[x].id)
                        self.send_dm(dm[x].message_create['sender_id'],"processed: #switch on")
                        del dm[x]
                        dm.reverse()
                        break

                    elif any(i in message for i in self.credential.Admin_cmd):
                        dm.reverse()
                        break

                else:
                    print("Account status: False; AdminCmd not found")
                    return dms

            for x in range(len(dm)):
                sender_id = dm[x].message_create['sender_id'] # str
                message_data = dm[x].message_create['message_data']
                message = message_data['text']
                id = dm[x].id

                # Message id is already stored on db_received, the message will be skipped (Keep_DM)
                if id in self.db_received:
                    continue

                # Avoid keyword error by skipping bot messages
                if sender_id == str(self.me.id):
                    self.delete_dm(id)
                    continue

                # ADMIN & USER COMMAND
                list_command = list(self.credential.Admin_cmd) + list(self.credential.User_cmd)
                command = message.split()[0].lower()
                if any(i == command for i in list_command):
                    # delete DM to avoid repeated command
                    self.delete_dm(id)

                    AdminCmd = self.AdminCmd #pylint: disable=unused-variable
                    UserCmd = self.UserCmd #pylint: disable=unused-variable
                    command, *contents = message.split()
                    notif = str()
                    
                    if command.lower() in self.credential.Admin_cmd:
                        # Manage admin access
                        if sender_id not in self.credential.Admin_id:
                            notif = self.credential.Notify_wrongTrigger
                            self.send_dm(recipient_id=sender_id, text=notif)
                            continue
                        else:
                            pass
                    print(f"command {command} {str(contents)} in progress...")

                    dict_command = self.credential.Admin_cmd.copy()
                    dict_command.update(self.credential.User_cmd)

                    if len(contents) != 0:
                        urls = message_data["entities"]["urls"]
                        for arg in contents:
                            try:
                                notif += f"\nprocessed: {command} {arg}"
                                fix_command = dict_command[command.lower()]
                                exec(fix_command)
                                if "urls" in fix_command:
                                    break

                            except Exception as ex:
                                pass
                                print(ex)
                                notif += f"\nException: {ex}"
                    else:
                        try:
                            notif += f"\nprocessed: {command}"
                            exec(dict_command[command.lower()])
                        except Exception as ex:
                            pass
                            print(ex)
                            notif += f"\nException: {ex}"
                    
                    # Skip notif if '#no_notif' in command's comment
                    if "#no_notif" in dict_command[command.lower()]:
                        if "Exception" not in notif:
                            continue
                    
                    # Manage notification for user
                    if sender_id not in self.credential.Admin_id:
                        if "Exception" not in notif:
                            notif = self.credential.Notify_userCmdDelete
                        else:
                            notif = self.credential.Notify_userCmdDeleteFail
                    
                    self.send_dm(sender_id, notif)
                    continue
                
                # ACCOUNT STATUS
                if self.credential.Account_status is False:
                    continue

                # Interval time per sender
                if self.credential.Interval_perSender is True and sender_id not in self.credential.Admin_id:
                    date_now = datetime.now(timezone.utc) + timedelta(hours=self.credential.Timezone)
                    for i in list(self.db_intervalTime):
                        # cleaning self.db_intervalTime
                        if self.db_intervalTime[i] < date_now:
                            del self.db_intervalTime[i]

                    if sender_id in self.db_intervalTime:
                        continue
                    else:
                        self.db_intervalTime[sender_id] = date_now + timedelta(seconds=self.credential.Interval_time)

                # Delete or store message id to avoid repeated menfess (Keep_DM)
                if self.credential.Keep_DM is True:     
                    if len(self.db_received) > 55:
                        self.db_received.pop(0)
                    self.db_received.append(id)

                else:
                    self.delete_dm(id)

                # ONLY FOLLOWED
                if self.credential.Only_followed is True and sender_id not in self.credential.Admin_id:
                    if int(sender_id) not in self.followed:
                        self.send_dm(sender_id, self.credential.Notify_notFollowed)
                        continue

                # Minimum lenMenfess
                if len(message) < self.credential.Minimum_lenMenfess and sender_id not in self.credential.Admin_id:
                    self.send_dm(sender_id, self.credential.Notify_senderRequirements)
                    continue

                # SENDER REQUIREMENTS
                if self.credential.Sender_requirements is True and sender_id not in self.credential.Admin_id:
                    indicator = 0
                    user = (api.get_user(sender_id))._json
                    # minimum followers
                    if user['followers_count'] < self.credential.Minimum_followers:
                        indicator = 1
                    # minimum age
                    created_at = datetime.strptime(user['created_at'], '%a %b %d %H:%M:%S +0000 %Y')
                    now = (datetime.now(timezone.utc) + timedelta(hours=self.credential.Timezone)).replace(tzinfo=None)

                    if (now-created_at).days < self.credential.Minimum_day:
                        indicator = 1
                    
                    if indicator == 1:
                        self.send_dm(sender_id, self.credential.Notify_senderRequirements)
                        continue

                # BLACKLIST WORDS
                list_blacklist = [i.lower() for i in self.credential.Blacklist_words]
                if any(i in message.lower() for i in list_blacklist) and sender_id not in self.credential.Admin_id:
                    try:
                        print("Skipping blacklist menfess")
                        notif = self.credential.Notify_blacklistWords
                        self.send_dm(recipient_id=sender_id, text=notif)
                    except Exception as ex:
                        sleep(60)
                        print(ex)
                        pass

                    continue

                # MENFESS TRIGGER
                if any(j.lower() in message.lower() for j in self.credential.Trigger_word):

                    print("Getting message -> sender_id: " + str(sender_id))
                    dict_dms = dict(message=message, sender_id=sender_id,
                        media_url=None, attachment_urls={'tweet':(None, None),
                                                         'media':list()})

                    # attachment url
                    urls = message_data['entities']['urls']
                    for i in urls:
                        if "twitter.com/" in i['expanded_url'] and "/status/" in i['expanded_url']:
                            # i['url]: url in text message                          
                            # Media
                            if any(j in i['expanded_url'] for j in ['/video/', '/photo/', '/media/']):
                                dict_dms['attachment_urls']['media'].append((i['url'], i['expanded_url']))
                                #i['expanded_url'] e.g https://twitter.com/username/status/123/photo/1
                            
                            # Tweet
                            else:
                                dict_dms['attachment_urls']['tweet'] = (i['url'], i['expanded_url'])
                                #i['expanded_url'] e.g https://twitter.com/username/status/123?s=19

                    # attachment media
                    if 'attachment' in message_data:
                        media = message_data['attachment']['media']
                        media_type = media['type']

                        if media_type == 'photo':
                            media_url = media['media_url']

                        elif media_type == 'video':
                            media_urls = media['video_info']['variants']
                            temp_bitrate = list()
                            for varian in media_urls:
                                if varian['content_type'] == "video/mp4":
                                    temp_bitrate.append((varian['bitrate'], varian['url']))
                            # sort to choose the highest bitrate
                            temp_bitrate.sort()
                            media_url = temp_bitrate[-1][1]

                        elif media_type == 'animated_gif':
                            media_url = media['video_info']['variants'][0]['url']
                        
                        dict_dms['media_url'] = media_url

                    dms.append(dict_dms)

                # WRONG TRIGGER
                else:
                    notif = self.credential.Notify_wrongTrigger
                    self.send_dm(recipient_id=sender_id, text=notif)

                    # Send wrong menfess to admin
                    username = self.get_user_screen_name(sender_id)
                    notif = message + f"\nstatus: wrong trigger\nfrom: @{username}\nid: {sender_id}"

                    for admin in self.credential.Admin_id:
                        self.send_dm(recipient_id=admin, text=notif)
            
            print(str(len(dms)) + " messages collected")
            return dms

        except Exception as ex:
            pass
            print(ex)
            sleep(60)
            return dms


    def notify_queue(self, dms):
        """Notify the menfess queue to sender
        :param dms: dms that returned from self.read_dm -> list of dict
        """
        try:
            print("Notifying the queue to sender")
            x, y, z = -1, 0, 0
            # x is primary time (30 sec); y is queue; z is addition time for media
            time = datetime.now(timezone.utc) + timedelta(hours=self.credential.Timezone)
            for i in dms:
                y += 1
                x += (len(i['message']) // 272) + 1
                if i['media_url'] != None:
                    z += 3
                
                if self.credential.Private_mediaTweet is True:
                    z += len(i['attachment_urls']['media']) * 4

                # Delay for the first sender is very quick, so, it won't be notified
                if x == 0:
                    continue

                sent_time = time + timedelta(seconds= x*(32+self.random_time) + z)
                sent_time = datetime.strftime(sent_time, '%H:%M')
                notif = self.credential.Notify_queueMessage.format(str(y), sent_time)
                self.send_dm(recipient_id=i['sender_id'], text=notif)

        except Exception as ex:
            pass
            print(ex)
            sleep(60)


    def delete_dm(self, id):
        '''Delete a DM
        :param id: message id -> int or str
        '''
        try:
            self.api.destroy_direct_message(id)
        except Exception as ex:
            print(ex)
            sleep(60)
            pass
    
    
    def send_dm(self, recipient_id, text):
        '''Send DM and automatically delete the sent DM
        :param recipient_id: -> str or int
        :param text: -> str
        '''
        try:
            sent = self.api.send_direct_message(recipient_id=recipient_id, text=text)
            self.delete_dm(sent.id)
        except Exception as ex:
            pass
            print(ex)
            sleep(60)


    def get_user_screen_name(self, id):
        '''Get username
        :param id: account id -> int
        :returns: username -> str
        '''
        try:
            user = self.api.get_user(id)
            return user.screen_name

        except Exception as ex:
            pass
            print(ex)
            sleep(60)
            return "Exception"


    def download_media(self, media_url, filename=None):
        '''Download media from url
        :param media_url: url -> string
        :param filename: None (default) or filename --> str
        :returns: file name (when filename=None) -> str
        '''
        print("Downloading media...")
        oauth = OAuth1(client_key=self.credential.CONSUMER_KEY,
                       client_secret=self.credential.CONSUMER_SECRET,
                       resource_owner_key=self.credential.ACCESS_KEY,
                       resource_owner_secret=self.credential.ACCESS_SECRET)

        r = get(media_url, auth=oauth)

        if filename == None:
            for i in sub("[/?=]", " ", media_url).split():
                if search(r"\.mp4$|\.gif$|\.jpg$|\.jpeg$|\.png$|\.webp$", i):
                    filename = i
                    break
            if filename == None:
                raise Exception("filename is not supported, please check the link")

        with open(filename, 'wb') as f:
            f.write(r.content)
            f.close()

        print("Download media successfully")
        return filename
    

    def add_watermark(self, filename, output=None):
        '''Add watermark to photo, then save as output
        Only support photo, if other, nothing will happen
        :param filename: file name -> str
        :param output: output name -> str
        :returns: output name -> str
        '''
        try:
            if output == None:
                output = filename

            file_type = filename.split('.')[-1]
            if file_type in "jpg jpeg png webp":
                print("Adding watermark...")
                adm = self.credential
                wm.watermark_text_image(filename, text=adm.Watermark_text,
                ratio=adm.Watermark_ratio, pos=adm.Watermark_position,
                output=output, color=adm.Watermark_textColor,
                stroke_color=adm.Watermark_textStroke, watermark=adm.Watermark_image)
            
            return output

        except Exception as ex:
            pass
            print(ex)
            return filename


    def upload_media_tweet(self, media_tweet_url):
        '''Upload media with (from) media tweet url
        Usually when sender want to post more than one media, he will attachs media tweet url.
        But the sender's username is mentioned on the bottom of the media.
        This method intended to make sender anonym. This return list of media_ids, then
        you can add media_ids to other method. Contains watermark module
        :param media_tweet_url: media tweet url e.g https://twitter.com/username/status/123/photo/1 -> str
        :returns: [(media_id, media_type),] a.k.a media_idsAndTypes -> list
        '''
        try:
            postid = sub(r"[/\.:]", " ", media_tweet_url).split()[-3]
            status = self.api.get_status(postid)
            media_idsAndTypes = list()

            if 'extended_entities' not in status._json:
                return list()
            print("Uploading media tweet...")
            
            for media in status._json['extended_entities']['media']:
                media_type = media['type']

                if media_type == 'photo':
                    media_url = media['media_url']

                elif media_type == 'video':
                    media_urls = media['video_info']['variants']
                    temp_bitrate = list()
                    for varian in media_urls:
                        if varian['content_type'] == "video/mp4":
                            temp_bitrate.append((varian['bitrate'], varian['url']))
                    # sort to choose the highest bitrate
                    temp_bitrate.sort()
                    media_url = temp_bitrate[-1][1]

                elif media_type == 'animated_gif':
                    media_url = media['video_info']['variants'][0]['url']

                filename = self.download_media(media_url)

                # Add watermark
                if self.credential.Watermark is True:
                    self.add_watermark(filename)

                media_id, media_type = self.upload_media(filename)
                remove(filename)
                media_idsAndTypes.append((media_id, media_type))
        
            return media_idsAndTypes # e.g [(media_id, media_type), (media_id, media_type), ]

        except Exception as ex:
            pass
            print(ex)
            sleep(60)
            return list()


    def upload_media(self, filename, media_category='tweet'):
        '''Upload media with chunk
        This method are needed when you want to use media to do something on
        twitter. This returns list of media_id, you can attach it to other method
        that require media id.
        :param filename: -> str
        :param media_category: 'tweet' or 'dm'. default to 'tweet'
        :returns: media id, media_type -> tuple
        '''
        mediaupload = MediaUpload(self.credential, filename, media_category)
        media_id, media_type = mediaupload.upload_init()
        mediaupload.upload_append()
        mediaupload.upload_finalize()
        return media_id, media_type



    def post_tweet(self, tweet, sender_id, media_url=None, attachment_url=None,
                media_idsAndTypes=list(), possibly_sensitive=False):
        '''Post a tweet, contains watermark module and self.db_sent updater
        Per tweet delay is 30s + self.random_time, but the last delay is deleted
        :param tweet: -> str
        :param sender_id: -> str or int
        :param media_url: media url that will be posted -> str
        :param attachment_url: url -> str
        :param media_idsAndTypes: [(media_ids, media_type),] -> list
        :param possibly_sensitive: True when menfess contains sensitive contents -> bool
        :returns: tweet id -> str
        '''
        try:
            #### ADD MEDIA_ID AND MEDIA_TYPE TO LIST_MEDIA_IDS ####
            # mediaIdsAndTypes e.g. [(media_id, media_type), (media_id, media_type), ]
            if media_url != None:
                tweet = tweet.split()
                tweet = " ".join(tweet[:-1])
                filename = self.download_media(media_url)

                # Add watermark
                if self.credential.Watermark is True:
                    self.add_watermark(filename)

                media_id, media_type = self.upload_media(filename)
                # Add attachment media from DM to the first order
                media_idsAndTypes.insert(0, (media_id, media_type))
                remove(filename)

            list_media_ids = [[]] # e.g. [[media_ids],[media_ids],[media_ids]]
            temp = 0
            while len(media_idsAndTypes) != 0:
                if temp == 0:
                    temp = 1
                    list_media_ids = list()
                media_ids = list()
                added = 0
                for media_id, media_type in media_idsAndTypes[:4]:
                    if media_type == 'video' or media_type == 'animated_gif':
                        if added == 0:
                            media_ids.append(media_id)
                            added += 1
                        break
                    media_ids.append(media_id)
                    added += 1

                list_media_ids.append(media_ids)
                # media_idsAndTypes are dynamic here
                media_idsAndTypes = media_idsAndTypes[added:]
            
            #### POST TWEET ####
            postid = 0
            # postid is the first tweet of the tweets thread
            while len(tweet) > 280:
            # Making a Thread.
                limit = 272
                check = tweet[:limit].split()
                separator = len(check[-1])
                if tweet[limit-1] == " ":
                    separator += 1
                tweet1 = unescape(tweet[:limit-separator]) + '-cont-'
                
                if postid == 0:
                    print("Making a thread...")
                    # postid is static after first update.
                    postid = self.api.update_status(
                        tweet1, attachment_url=attachment_url, media_ids=list_media_ids[:1][0],
                        possibly_sensitive=possibly_sensitive).id
                    postid1 = postid
                else:
                    postid1 = self.api.update_status(
                        tweet1, in_reply_to_status_id=postid1, auto_populate_reply_metadata=True,
                        media_ids=list_media_ids[:1][0], possibly_sensitive=possibly_sensitive).id
                
                list_media_ids = list_media_ids[1:] + [[]]
                sleep(30+self.random_time)
                # tweet are dynamic here
                tweet = tweet[limit-separator:]
            
            # Above and below operation differences are on tweet1 and unescape(tweet), also tweet[limit-separator:]
            # It's possible to change it to be one function
            if postid == 0:
                # postid is static after first update.
                postid = self.api.update_status(
                        unescape(tweet), attachment_url=attachment_url, media_ids=list_media_ids[:1][0],
                        possibly_sensitive=possibly_sensitive).id
                postid1 = postid        
            else:
                postid1 = self.api.update_status(
                    unescape(tweet), in_reply_to_status_id=postid1, auto_populate_reply_metadata=True,
                    media_ids=list_media_ids[:1][0], possibly_sensitive=possibly_sensitive).id
            
            list_media_ids = list_media_ids[1:] + [[]]

            # When media_ids still exists, It will be attached to the subsequent tweets
            while len(list_media_ids[0]) != 0: # Pay attention to the list format, [[]]
                sleep(30+self.random_time)

                print("Posting the rest of media...")
                postid1 = self.api.update_status(
                    in_reply_to_status_id=postid1,
                    auto_populate_reply_metadata=True, media_ids=list_media_ids[:1][0],
                    possibly_sensitive=possibly_sensitive).id

                list_media_ids = list_media_ids[1:] + [[]]

            print('Menfess is posted -> postid:', str(postid))

            # ADD TO DB SENT
            self.db_sent_updater('add', sender_id, str(postid))
            
            return postid

        except Exception as ex:
            pass
            print(ex)
            sleep(60)
            return None
from datetime import datetime, date, time, timedelta
from collections import Counter
import sys

# In[96]:

# Step 1 - Authenticate
consumer_key = 'consumer_key'
consumer_secret = 'consumer_secret'

access_token = 'access_token'
access_token_secret = 'access_token_secret'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
auth_api = API(auth)

# In[117]:

account_list = ['@realDonaldTrump']

# In[170]:

op = {}

# In[171]:

if len(account_list) > 0:
    for target in account_list:
        item = auth_api.get_user(target)
        op["name "] = item.name
Example #33
0
 def __init__(self):
     self.auth = authenticate_twitter_app()
     self.twitter_client = API(self.auth)
Example #34
0
class Twitter:
    """A helper for talking to Twitter APIs."""
    def __init__(self, logs_to_cloud):
        self.logs_to_cloud = logs_to_cloud
        self.logs = Logs(name="twitter", to_cloud=self.logs_to_cloud)
        self.twitter_auth = OAuthHandler(TWITTER_CONSUMER_KEY,
                                         TWITTER_CONSUMER_SECRET)
        self.twitter_auth.set_access_token(TWITTER_ACCESS_TOKEN,
                                           TWITTER_ACCESS_TOKEN_SECRET)
        self.twitter_api = API(self.twitter_auth)

    def start_streaming(self, callback):
        """Starts streaming tweets and returning data to the callback."""

        self.twitter_listener = TwitterListener(
            callback=callback, logs_to_cloud=self.logs_to_cloud)
        twitter_stream = Stream(self.twitter_auth, self.twitter_listener)

        self.logs.debug("Starting stream.")
        twitter_stream.filter(follow=[TRUMP_USER_ID])

        # If we got here because of an API error, raise it.
        if self.twitter_listener.get_error_status():
            raise Exception(self.twitter_listener.get_error_status())

    def stop_streaming(self):
        """Stops the current stream."""

        if not self.twitter_listener:
            self.logs.warn("No stream to stop.")
            return

        self.logs.debug("Stopping stream.")
        self.twitter_listener.stop_queue()

    def tweet(self, companies, link):
        """Posts a tweet listing the companies, their ticker symbols, and a
        quote of the original tweet.
        """

        text = self.make_tweet_text(companies, link)
        self.logs.info("Tweeting: %s" % text)
        self.twitter_api.update_status(text)

    def make_tweet_text(self, companies, link):
        """Generates the text for a tweet."""

        text = ""

        for company in companies:
            line = company["name"]

            if "root" in company and company["root"]:
                line += " (%s)" % company["root"]

            ticker = company["ticker"]
            line += " $%s" % ticker

            if "sentiment" in company:
                if company["sentiment"] == 0:
                    sentiment = EMOJI_SHRUG
                else:
                    if company["sentiment"] > 0:
                        sentiment = EMOJI_THUMBS_UP
                    else:
                        sentiment = EMOJI_THUMBS_DOWN
                line += " %s" % sentiment

            text += "%s\n" % line

        text += link

        return text

    def get_tweets(self, ids):
        """Looks up metadata for a list of tweets."""

        statuses = self.twitter_api.statuses_lookup(ids)
        self.logs.debug("Got statuses response: %s" % statuses)

        return statuses
Example #35
0
def find_list(api: tweepy.API) -> int:
    for l in api.lists_all():
        if l.name == list_name:
            return l.id
    print("リスト作成")
    return create_list(api)
except ImportError:

    print("Some Error Occured in Importing the Modules and Library")

if __name__ == '__main__':
    auth = get_twitter_client()
'''
NOTE:	user-authentication	: 180 queries per access token every 15 minutes
	application- authentication : 450 queries every 15 minutes

Here we would be using application- authentication for higher rate limits.
For that we would have to use Tweepy's AppAuthHandler, we would need consumer_key and consumer_secret for that only

'''
auth = AppAuthHandler(consumer_key, consumer_secret)
api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
'''
NOTE: 
		1.The search API returns a maximum of 100 TWEETS per Query.
		2.We'll have to wait once our wait limit is reached.
		3.If not sure about the limits and want to check the status of your Rate Limit we can use the Tweepy's following method:
				print( api.rate_limit_status()['resource']['search'] )
'''
# pprint.pprint(api.rate_limit_status())  # IF YOU WANT TO LOOK ALL THE ATTRIBUTES
print(api.rate_limit_status()['resources']['search'])
'''
We are all set for making the query we'll seprate the mutiple HASHTAGS by using OR.
Note: Try to keep the query less than 140 characters
'''
query_delhi = ' #DelhiSmog OR #Smog OR #myrighttobreathe OR #smog OR #crop Burning OR\
		 #delhi OR #delhichokes OR #AirPollution OR \
Example #37
0
def get_twitter_client():
    auth = get_twitter_auth()
    client = API(auth)
    return client
Example #38
0
        try:
            self.producer.send('twitterstream', data)
        except Exception as e:
            print(e)
            return False
        return True

    def on_error(self, status_code):
        print("Error received in kafka producer")
        return True  #don't kill the stream

    def on_timeout(self):
        return True


if __name__ == '__main__':

    consumer_key = 'clcZktfmstM96nvSUjFHAa3fX'
    consumer_secret = 'c6cQyak6noAb7lFc3j2dXbNHU9IGzzFen0v1EFo6p0cbfz2BDy'
    access_token = '1273089309738926086-EGo5KbNZc37wrnjLpxClo56CVMh2n6'
    access_token_secret = 'f4lFuCCAB9ApFD0pjELNUYPYdUmOFIAOl85cqW0qn7Awk'

    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = API(auth)

    stream = Stream(auth, listener=TstreamListener(api))

    tracked = ['Vaccine']
    stream.filter(track=tracked, languages=['en'])
    def __init__(self, twitter_user=None):
        self.auth = TwitterAuthenticator().authenticate_twitter_app()
        self.twitter_client = API(self.auth)

        self.twitter_user = twitter_user
Example #40
0
def pull_down_tweets(screen_name):
    api = API(auth)
    tweets = api.user_timeline(screen_name=screen_name, count=200)
    for tweet in tweets:
        #        print(json.dumps(tweet._json,indent=4))
        print(tweet.text)
Example #41
0
from time import sleep
from tweepy import OAuthHandler, API
from pprint import pprint
import requests
import re

consumer_key = ''  # Input consumer key
consumer_secret = ''  # Input secret consumer key
access_token = ''  # Input access token
access_token_secret = ''  # Input secret access token

auth = OAuthHandler(consumer_key,
                    consumer_secret)  # Input Twitter app credentials
auth.set_access_token(
    access_token, access_token_secret)  # Initiate connection to Twitter bot
twitterBot = API(auth)


def main():
    while True:
        todayPop = worldPopulation()  # Define the current world population
        percent, popChange = populationChange(
            todayPop
        )  # Find the percent change and population change from yesterday to today
        twitterBot.update_status(
            'Currently there are {0} people on earth. That is a {1} increase since yesterday!'
            .format(todayPop, popChange))
        sleep(86400)  # Pause for 24 hours


def worldPopulation(
Example #42
0
class SListener(StreamListener):
    def__init__(self, api = None):
        self.output = open('tweets_%s.json %
            time.strftime('%Y%m%d-%H%M%S'), 'w'')
                           self.api or API()
Example #43
0
def get_trendy(woeid):
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = API(auth)
    return api.trends_place(woeid)
Example #44
0
def auth():
    acct_name, consumer_key, consumer_secret, access_token, access_token_secret = get_account_sequential()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    auth_api = API(auth)
    return auth_api
#getting jokes from website
for item in jocking:
    jokes.append(item.find_all("p"))

#now code for tweeting
# Credentials to access Twitter API
ACCESS_TOKEN = 'paste your access token here'
ACCESS_SECRET = 'paste your access secret here'
CONSUMER_KEY = 'paste your consumer key here'
CONSUMER_SECRET = 'paste your consumer secret here'

#authenticating twitter API
Auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
Auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
TwitterBot = API(Auth)

num = 0
while (num < len(jokes)):

    try:

        if len(jokes[num]) == 1:
            TwitterBot.update_status(jokes[num][0].text)

        elif len(jokes[num]) == 2:
            TwitterBot.update_status(
                str(jokes[num][0].text + ' \n' + jokes[num][1].text))

        elif len(jokes[num]) == 3:
            TwitterBot.update_status(
Example #46
0
def tweepy_direct_message():
    test_direct_message = json.loads(test_direct_message_json)
    test_direct_message['text'] = 'https://twitter.com/fooman/status/12345'
    return TweepyDirectMessage.parse(API(), test_direct_message)
Example #47
0
#-*- coding:utf8
from secret import *
from tweepy import API, OAuthHandler, TweepError
from time import *

import pip

# If there is no tweepy, it will install the package auto
pip.main(["install","tweepy"])

print("[+] Prepare to start")

auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

api = API(auth)

me = api.me()
last_tweet = {}

print(f"[+] Start Bot - {me.name}")

followers = me.followers_ids()

while True:
    print(f"[+] Check {len(followers)} followers")

    for follower in followers:
        # to response to "Rate Limit exceeded"
        sleep(2)
Example #48
0
            print (d['text'])
        ##print(data[100])
        print('\n\n')
     """    

    def on_error(self, status):
        ##print(stasus)
        print("ERROR")

 
if __name__ == '__main__':
    

    
    listener = StdOutListener()
    auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_KEY_SECRET)
    auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
    
    api = API(auth)
    trends1 = api.trends_place(23424934)
    trends12 = choice(trends1[0]['trends'])
    
    print(trends1[0]['trends'][])
    

    ##stream = Stream(auth, listener)
    ##stream.filter(track = trends12['name'])
    ##stream.filter(track = ['#KMKapalit'], languages = ['tl'])
    ##stream.filter(track = ['Duterte'], languages = ['tl'])
    
        
def get_twitter_client():
    #setup twitter API client
    #return tweepy.API object
    auth = get_twitter_auth()
    client = API(auth)
    return client
Example #50
0
import time  # time library to get current time at the start and end of the program - to show users how long it takes to run
import pydoc  # documentation in this program
from datetime import datetime  #to manipulate datetime
import praw  # this is Reddit API crawler

# ---------------- Authentications for the APIs ----------------
#Variables that contains the user credentials to access Twitter API
access_token = '421663967-Hz4HafUN5RYo82UWqLGmTd6KxPprP90qOsld6aKv'  #"mytoken"
access_token_secret = '2Enlm0YjbMvqqfUXqz53rymo0U5hjVKrcMnRMJikG48nb'  #"mytokenscret"
consumer_key = 'R79OBgR64Bip8SPPy32A6FlO8'  #"consumerkey'
consumer_secret = 'keoB1Qs4NMKpkf1VjhQYaQOqGqsKAJy3HJXug5GRBpo4WSkxzV'  #"consumersecret

# Authorization and Authentication - call the API
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = API(auth, wait_on_rate_limit=True)

# Authorization and Authentication - to call the praw API
reddit = praw.Reddit(client_id='H75A7tYoO4NU7w',\
                     client_secret='bzYDpEB6hXe3ZAruahZILgSFReMCxQ',\
                     user_agent='1009_crawler_asg',\
                     username='******',\
                     password='******')


def main():
    """ Main Function to call API and the scrap functions for the individual keywords  - this will run for 500 rounds with a 500seconds interval between each run.
        This would help to update the webpage with more accurate data from the latest tweets.
        - Users are allowed to enter their desired number of rounds and tweets that is to be crawled
        :exception AssertionError: When user enter number out of range
        :exception ValueError: When user entered something not numeric
Example #51
0
def create_list(api: tweepy.API) -> int:
    return api.create_list(name=list_name, mode="private").id
Example #52
0
 def __init__(self, user=None, hashtag=''):
     self.auth = TwitterAuthenticator().authenticate_twitter_app()
     self.api = API(self.auth)
     self.user = user
     self.hashtag = hashtag + ' -filter:retweets'
Example #53
0
class TweepyApi(BaseTweepyApi, ApiAdapter):
    """
    A :class:`turses.api.ApiAdapter` implementation using `tweepy` library.

        http://github.com/tweepy/tweepy/
    """

    def __init__(self, *args, **kwargs):
        ApiAdapter.__init__(self, *args, **kwargs)

    # from `turses.api.base.ApiAdapter`

    def init_api(self):
        oauth_handler = TweepyOAuthHandler(self._consumer_key,
                                           self._consumer_secret,
                                           secure=configuration.twitter['use_https'])
        oauth_handler.set_access_token(self._access_token_key,
                                       self._access_token_secret)
        self._api = BaseTweepyApi(oauth_handler, secure=configuration.twitter['use_https'])

    @to_user
    def verify_credentials(self):
        return self._api.me()

    @to_user
    @include_entities
    def get_user(self, screen_name, **kwargs):
        return self._api.get_user(screen_name=screen_name, **kwargs)

    # timelines

    @to_status
    @include_entities
    def get_status(self, status_id, **kwargs):
        return self._api.get_status(status_id, **kwargs)

    @to_status
    @include_entities
    def get_home_timeline(self, **kwargs):
        tweets = self._api.home_timeline(**kwargs)
        return tweets

    @to_status
    @include_entities
    def get_user_timeline(self, screen_name, **kwargs):
        return self._api.user_timeline(screen_name, **kwargs)

    @to_status
    @include_entities
    def get_own_timeline(self, **kwargs):
        me = self.verify_credentials()
        return self._api.user_timeline(screen_name=me.screen_name, **kwargs)

    @to_status
    @include_entities
    def get_mentions(self, **kwargs):
        return self._api.mentions_timeline(**kwargs)

    @to_status
    @include_entities
    def get_favorites(self, **kwargs):
        return self._api.favorites(**kwargs)

    @to_direct_message
    @include_entities
    def get_direct_messages(self, **kwargs):
        dms = self._api.direct_messages(**kwargs)
        sent = self._api.sent_direct_messages(**kwargs)
        dms.extend(sent)
        return dms

    @include_entities
    def get_thread(self, status, **kwargs):
        """
        Get the conversation to which `status` belongs.
        """
        users_in_conversation = [status.authors_username]

        # Save the users that are mentioned
        for user in status.mentioned_usernames:
            if user not in users_in_conversation:
                users_in_conversation.append(user)

        # Fetch the tweets from participants before and after `status`
        # was published
        tweets_from_participants = []
        for user in users_in_conversation:
            user_tweets = self._get_older_and_newer_tweets(user, status.id)
            tweets_from_participants.extend(user_tweets)

        def belongs_to_conversation(tweet):
            for user in users_in_conversation:
                if user in tweet.text:
                    return True

        return filter(belongs_to_conversation, tweets_from_participants)

    def _get_older_and_newer_tweets(self, screen_name, tweet_id, count=20):
        """
        Get tweets from the user with `screen_name` username that are older
        and newer than `tweet_id`.

        By default, 20 tweets are fetched. If provided, `count` controls how
        many tweets are requested.
        """
        older = self.get_user_timeline(screen_name,
                                       max_id=tweet_id,
                                       count=count/2)
        newer = self.get_user_timeline(screen_name,
                                       since_id=tweet_id,
                                       count=count/2)
        return older + newer

    def get_message_thread(self, dm, **kwargs):
        messages = self.get_direct_messages(**kwargs)

        me = self.verify_credentials()
        if dm.sender_screen_name == me.screen_name:
            with_user = dm.recipient_screen_name
        else:
            with_user = dm.sender_screen_name

        def belongs_to_conversation(message):
            return (message.sender_screen_name == with_user or
                    message.recipient_screen_name == with_user)

        return filter(belongs_to_conversation, messages)

    @to_status
    @include_entities
    def search(self, text, **kwargs):
        return self._api.search(text, **kwargs)

    @to_status
    @include_entities
    def get_retweets_of_me(self, **kwargs):
        return self._api.retweets_of_me(**kwargs)

    def update(self, text):
        self._api.update_status(text)

    def reply(self, status, text):
        self._api.update_status(text, in_reply_to_status_id=status.id)

    def destroy_status(self, status):
        self._api.destroy_status(status.id)

    def retweet(self, status):
        self._api.retweet(status.id)

    def direct_message(self, username, text):
        self._api.send_direct_message(user=username, text=text)

    def destroy_direct_message(self, dm):
        self._api.destroy_direct_message(dm.id)

    def create_friendship(self, screen_name):
        self._api.create_friendship(screen_name=screen_name)

    def destroy_friendship(self, screen_name):
        self._api.destroy_friendship(screen_name=screen_name)

    def create_favorite(self, status):
        self._api.create_favorite(status.id)

    def destroy_favorite(self, status):
        self._api.destroy_favorite(status.id)

    # list methods

    @to_list
    def get_lists(self, screen_name):
        return self._api.lists_all(screen_name)

    @to_list
    def get_own_lists(self):
        return self._api.lists_all()

    @to_list
    def get_list_memberships(self):
        return self._api.lists_memberships()

    @to_list
    def get_list_subscriptions(self):
        return self._api.lists_subscriptions()

    @to_status
    def get_list_timeline(self, a_list):
        owner = a_list.owner.screen_name
        return self._api.list_timeline(owner=owner, slug=a_list.slug)

    @to_user
    def get_list_members(self, a_list):
        owner = a_list.owner.screen_name
        return self._api.list_members(owner=owner, slug=a_list.slug)

    @to_list
    def subscribe_to_list(self, a_list):
        owner = a_list.owner
        return self._api.subscribe_list(owner=owner.screen_name,
                                        slug=a_list.slug)

    @to_user
    def get_list_subscribers(self, a_list):
        owner = a_list.owner
        return self._api.list_subscribers(owner=owner.screen_name,
                                          slug=a_list.slug,)
Example #54
0
def find_friends(api: tweepy.API) -> typing.Set[int]:
    return set(
        list(tweepy.Cursor(api.friends_ids, user_id=api.me().id).items()))
Example #55
0
from tweepy import API, OAuthHandler
import json, time

config = {}
with open("config/data.json") as f:
    config = json.loads(f.read())
with open("config/auth.json") as f:
    authdata = json.loads(f.read())

auth = OAuthHandler(authdata['consumer_key'], authdata['consumer_secret'])
auth.set_access_token(authdata['access_token'],
                      authdata['access_token_secret'])
authdata = {}
authdata = "authdata's been wiped. Don't try to gain access this way!"

api = API(auth)


def checkfake(u):
    html = "<h2><a href='https://twitter.com/" + u.screen_name + "' target='_blank'>" + u.screen_name + "</a></h2>"
    try:
        if u.screen_name != config['official']:
            retweets = 0
            safe = True
            print(u.screen_name)
            userposts = api.user_timeline(u.id)
            for j in userposts:
                for k in config['pointsmeanprizes']:
                    if k in j.text.lower():
                        if j.text[:4] == "RT @" and config['ignoreretweets']:
                            retweets += 1
            msg['user_place'] = tweet['user']['location']

        msg = json.dumps(msg)

        print(msg)

        producer.produce("test", msg.encode('utf-8'))

        return True

    def on_error(self, status):
        print(status)


env_path = Path('.') / 'local.env'

load_dotenv(dotenv_path=env_path)

consumer_key = os.getenv("CONSUMER_KEY")
consumer_secret = os.getenv("CONSUMER_SECRET")
access_token = os.getenv("ACCESS_TOKEN")
access_token_secret = os.getenv("ACCESS_TOKEN_SECRET")

producer = Producer({'bootstrap.servers': 'localhost:9092'})
listener = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
stream = Stream(auth, listener)
stream.filter(track=['#coronapocalypse'])
Example #57
0
        'bacon', 'pastel', 'tarta', 'galletas', 'bebida energética',
        'perrito caliente', 'helado', 'pizza', 'frito', 'cerveza',
        'hamburguesa', 'kebab', 'refresco', 'fastfood', 'vodka', 'whiskey',
        'ron', 'tequila', 'sandwich', 'ginebra', 'anís', 'brandy', 'nuggets'
    ]
    stream_args = marcas + food_keywords

    if not os.path.isdir('pid'):
        os.makedirs('pid')
    pid_file = 'pid/pidSpanishGenericTweets'
    file = open(pid_file, 'w')
    file.write(str(os.getpid()))
    file.close()

    #Creo el Listener:
    # - Con el directorio donde colocar los tweets
    # - Y con el tiempo de crear un fichero nuevo
    listener = FileListener(output_directory, 3600)
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    logger.warning("Connecting Process Spanish")
    api = API(auth)
    logger.warning(api.me().name)

    stream = Stream(auth=auth, listener=listener)
    try:
        stream.filter(locations=spain)  #, track=stream_args)
    except Exception as e:
        #SEND A EMAIL
        logger.error('Failed to upload to ftp: ' + str(e))
 def __init__(self,
              twitter_user=None
              ):  # if it is none it will go to default user_timeline
     self.auth = TwitterAuthenticator().authenticate_twitter_app()
     self.twitter_client = API(self.auth)
     self.twitter_user = twitter_user
Example #59
0
from tweepy import API, Cursor, OAuthHandler, TweepError

# Read in configs
configs = configparser.ConfigParser()
configs.read('./config.ini')
keys = configs['TWITTER']
consumer_key = keys['CONSUMER_KEY']
consumer_secret = keys['CONSUMER_SECRET']
access_token = keys['ACCESS_TOKEN']
access_secret = keys['ACCESS_SECRET']
screen_name = keys['SCREEN_NAME']

# Authenticate Tweepy connection with Twitter API
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

# Get Twitter ID for each follower
ids = []
for fid in Cursor(api.followers_ids, screen_name=screen_name,
                  count=5000).items():
    ids.append(fid)

# print(ids)

# Get more details for each follower
info = []
for i in range(0, len(ids), 100):
    try:
        chunk = ids[i:i + 100]
        info.extend(api.lookup_users(user_ids=chunk))
Example #60
0
 def __init__(self, twitter_user, fetched_tweets_filename):
     self.auth = TwitterAuthenticator().authenticate_twitter_app()
     self.twitter_client = API(self.auth, wait_on_rate_limit=True)
     self.twitter_user = twitter_user
     self.fetched_tweets_filename = fetched_tweets_filename