check = 2
            counterimgs += 1
        except:
            pass
        # if check == 2:
        #     sys.exit()
    def on_error(self, status_code):
        print("algo ha pasao ", status_code)


#auth shinannigans
keys = json.loads(open("credenciales.json").read())

auth = tweepy.OAuthHandler(keys["consumer_key"], keys["consumer_secret"])
auth.set_access_token(keys["access_token"], keys["access_token_secret"])

tapi = tweepy.API(auth,
                  wait_on_rate_limit=True,
                  wait_on_rate_limit_notify=True)

# logica prebarrido
print(
    "word based image scrapper, creara una carpeta llamada imgs en donde este programa este alojado"
)
print("Dame la palabra sobre la que quieres monitorizar:")
targetWord = input()

stream = TweetListener()
streamingApi = tweepy.Stream(auth=tapi.auth, listener=stream)

streamingApi.filter(track=[targetWord], )
Exemple #2
0
def stream_listener():
    stream_listener = StreamListener()
    print("Stream Starting...\n")
    stream = tweepy.Stream(auth=myAPI.auth, listener=stream_listener)
    stream.filter(track=["@paul2greasy"])
Exemple #3
0
                'ignore'), decoded['coordinates'], decoded['user']['location'])
        print ''

        coords = decoded['coordinates']
        if coords:
            self.store_data(coords)

        return True

    def store_data(self, coords):
        coords = coords['coordinates']
        firebase_ref.post('/processed_coords', coords)

    def on_error(self, status):
        print status


if __name__ == "__main__":
    l = StdOutListener()

    url = 'https://stream.twitter.com/1.1/statuses/filter.json?delimited=length&track=twitterapi&'

    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)

    stream = tweepy.Stream(auth, l)
    stream.filter(track=[
        'happybirthday', 'happy birthday', 'feliz cumpleanos',
        'felizcumpleanos', 'joyeux anniversaire', 'joyeuxanniversaire'
    ])
import tweepy

import Myauth


class twitterStream(tweepy.StreamListener):
    def on_status(self, status):
        reply_status = "@%s %s" % (status.author.screen_name, "follow back")
        # api.update_status(status=reply_status, in_reply_to_status_id=status.id)
        api.create_favorite(status)
        api.create_friendship(status.author.screen_name)
        print(status.author.screen_name)

    def on_error(self, status_code):
        if status_code == 420:
            # returning False in on_data disconnects the stream
            return False


api = tweepy.API(Myauth.auth)
myStreamListener = twitterStream()
myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
myStream.filter(track=['python'])
Exemple #5
0
        # entities provide structured data from Tweets including resolved URLs, media, hashtags and mentions without having to parse the text to extract that information
        tweet_text = status.text

        print(status.user.screen_name + ' tweeted you ' + status.text)

        if 'separation' in tweet_text or 'departure' in tweet_text:
            api.update_status('@' + username +
                              random.choice(departure.values()))
            print(username + ' sent ' + tweet_text +
                  ' and we  responded with a departure quote')
        elif 'initiation' in status.text:
            api.update_status('@' + username +
                              random.choice(initiation.values()))
            print(username + ' sent ' + tweet_text +
                  ' responded with initiation quote')
        elif 'return' in status.text:
            api.update_status('@' + username + random.choice(ret.values()))
            print(username + ' sent ' + tweet_text +
                  'responded with return quote')
        elif 'whats up' in tweet_text:
            api.update_status('@' + username + ' not much wasuup w u?')


myStreamListener = BotStreamer()
# Construct the Stream instance
stream = tweepy.Stream(auth, myStreamListener)
stream.filter(track=[
    'nickiboi_c'
])  #filter the stream for tweets with "nickiboi_c" in them
Exemple #6
0
#     subject = 'News Tweets'

#     # body is a variable that is a formatted version for the tweets that are sent to the email.
#     # Each tweet will occupy a row that has the username of the person who sent the tweet, the text of the tweet, and the link of the tweet.
#     body = f"""{(tweets[0].user.name)}: {(tweets[0].text)} (https://twitter.com/i/web/status/{tweets[0].id_str})\n{(tweets[1].user.name)}: {(tweets[1].text)}
#     (https://twitter.com/i/web/status/{tweets[1].id_str})\n{(tweets[2].user.name)}: {(tweets[2].text)} (https://twitter.com/i/web/status/{tweets[2].id_str})\n{(tweets[3].user.name)}:
#     {(tweets[3].text)} (https://twitter.com/i/web/status/{tweets[3].id_str})\n{(tweets[4].user.name)}: {(tweets[4].text)} (https://twitter.com/i/web/status/{tweets[4].id_str})"""

#     message = 'Subject: {}\n\n{}'.format(subject, body) # Message sent for the email.
#     server.sendmail('*****@*****.**', input('Enter your email to get the news tweets!: \n'), message.encode('ascii', 'ignore')) # The email with the tweets will be sent from my personal email to my school email.
#     print('Email has been sent!')
#     playsound('youGotmail.mp3') # plays sound effect when email has been sent.
#     server.quit() # The SMTP server stops once the email has been sent.

streamListener = TwitterStreamListener()  # Instance of TwitterStreamListener.
stream = tweepy.Stream(
    auth=api.auth, listener=streamListener
)  # Stream of tweets which the streamListener object will record.

stream.filter(
    follow=['428333', '807095', '2467791'],
    track=['President Trump', 'News'],
    languages=['en']
)  # This will filter the stream of tweets. I will only get tweet that contain the words 'President Trump' as he popular on the news currently.
body2 = f"yo"
for tweet in arr_of_tweets:
    body2 + f"hello"
    print(body2)
# send_mail(arr_of_tweets) # Sends the array of tweets to my email.
Exemple #7
0
            writer = csv.writer(f)
            writer.writerow([
                str(uuid1()),
                self.sanitize_text(status.text), status.author.name,
                status.created_at
            ])
            time.sleep(1)

    def sanitize_text(self, text: str) -> str:
        return re.sub(r'[\n,]', ' ', text)


if __name__ == '__main__':

    log.basicConfig(level=log.INFO)

    api_key = config('API_KEY')
    api_secret_key = config('API_KEY_SECRET')
    access_token = config('ACCESS_TOKEN')
    access_secret_token = config('ACCESS_TOKEN_SECRET')

    auth = tweepy.OAuthHandler(api_key, api_secret_key)
    auth.set_access_token(access_token, access_secret_token)

    stdout_stream = tweepy.Stream(auth, StdOutStream())
    datapipe_csv_stream = tweepy.Stream(auth, DataPipeStream())

    # stream-load tweets that match condition
    print('Starting stream...')
    datapipe_csv_stream.filter(track=['ps5', '楽天', '買えた'], languages=['ja'])
        processed_doc["text"] = x["text"]

    return processed_doc


# Custom listener class
class StdOutListener(StreamListener):  # Don't touch this
    """ A listener handles tweets that are received from the stream.
    This is a basic listener that just pushes tweets to pubsub
    """
    def __init__(self):
        super(StdOutListener, self).__init__()
        self._counter = 0

    def on_data(self, data):
        data = json.loads(data)
        write_to_pubsub(reformat_tweet(data))
        self._counter += 1
        return True

    def on_error(self, status):
        if status == 420:
            print("rate limit active")
            return False


# Start listening
trump_api = StdOutListener()
stream_trump = tweepy.Stream(auth, trump_api, tweet_mode='extended')
stream_trump.filter(track=trump_hashtags)
Exemple #9
0
def main(keywords):
    api = create_api()
    tweets_listener = RetweetListener(api)
    stream = tweepy.Stream(auth=api.auth, listener=tweets_listener)
    stream.filter(track=keywords, languages=["en"])
    time.sleep(60)
 def streamingSearch(cls, auth, instance, query):
     StreamingApiForSearch = instance
     start_time = time.time()
     mystreaming = tweepy.Stream(auth=auth, listener=StreamingApiForSearch)
     
     mystreaming.filter(track=query, languages=["en"])
limit = 500


class MyStreamListener(StreamListener):

    def __init__(self):
        super(MyStreamListener, self).__init__()
        self.num_tweets = 0

    def on_data(self, data):
        if self.num_tweets < limit:
            self.num_tweets += 1
            try:
                with open('tweet_mining.json', 'a') as tweet_file:
                    tweet_file.write(data)
                    return True
            except BaseException as e:
                print("Failed on data: %s" % str(e))
            return True
        else:
            return True

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


twitter_stream = tweepy.Stream(auth, MyStreamListener())
twitter_stream.filter(track=keyword_list)
Exemple #12
0
def start_listener(api, username):
    l = MyStreamListener()
    streamer = tweepy.Stream(auth=api.auth, listener=l)
    streamer.filter(follow=[username], async=True)
Exemple #13
0
def filter_tweets(keyword, api):
    myStreamListener = MyStreamListener()
    myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
    myStream.filter(track=[keyword])
Exemple #14
0
 def initialize_twitter_stream(self):
     if self.twitter_stream is None:
         self.twitter_stream = tweepy.Stream(self.twitter_auth,
                                             self.listener,
                                             retry_420=3 * 60)
                
            #print out a message to the screen that we have collected a tweet
            print("Tweet de  " + format(name.encode('utf-8'),'>25') + "\tposté le   " + str(created_at) + "\ten " + str(lang.upper()) + "\tenregistré")
            # print(text + "\n")
            #insert the data into the mongoDB into a collection called twitter_search
            #if twitter_search doesn't exist, it will be created.
            datajson['tweet_corrige'] = " "
            datajson['utilisateurs'] = {}
            datajson['signalements'] = 0
            datajson['evaluations'] = 0
            datajson['valide'] = False

            if datajson['truncated'] == False:
                datajson['tweet_complet'] = datajson['text']
            else:
                datajson["tweet_complet"] = datajson['extended_tweet']['full_text']
            db.twitter_search.insert(datajson)

        except Exception as e:
           print(e)




auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
#Set up the listener. The 'wait_on_rate_limit=True' is needed to help with Twitter API rate limiting.
listener = StreamListener(api=tweepy.API(wait_on_rate_limit=True)) 
streamer = tweepy.Stream(auth=auth, listener=listener)
print("Tracking" + str(dico))
streamer.filter(track=dico)
Exemple #16
0
                #append ai-answer to the memory
                memory.append(replyText.replace('notgewure', ''))

                print(memory)

                # check if repsonse is over 140 char
                if len(replyText) > 140:
                    replyText = replyText[0:138] + '..'

                print('Tweet ID: ' + tweetId)
                print('From: ' + screenName)
                print('Tweet Text: ' + tweet.get('text'))
                print('Reply Text: ' + replyText)

                # If rate limited, the status posts should be queued up and sent on an interval TODO
                tapi.update_status(replyText, tweetId)
        except:
            print(traceback.format_exc())
            print("an exception occured")

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


if __name__ == '__main__':
    streamListener = ReplyToTweet()
    twitterStream = tweepy.Stream(auth, listener=streamListener)
    twitterStream.filter(track=['@notgewure'])
Exemple #17
0
def streamBasic():
    print('StartBasic')
    myStreamListener = MyStreamListener()
    myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener)
    myStream.sample(languages=['en'], async=True)
Exemple #18
0
def tw_stream(api):
    myStreamListener = MyStreamListener()
    myStream = tweepy.Stream(auth=api.auth, listener=myStreamListener())
    myStream.filter(track=qw, async=True)
    def on_error(self, status):
        print("Error detected")
        print(status)

# Authenticate to Twitter
auth = tweepy.OAuthHandler("CQTGkAGL1TSNX5MVShJvXjk7H",
    "nvJopSrix84GSCHShhr4fd0lf4HvZuDvMy0E0b3TQPIlO6LF1L")
auth.set_access_token("794777197470830593-0fQdci4AGivrgv2IQy5CAKW7e1VDf12",
    "kkUxcZC2YGQHuDZpHcvXOiniDW4mXwaTdvQtSjXi7nuax")

# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True,
    wait_on_rate_limit_notify=True)

tweets_listener = MyStreamListener(api)
stream = tweepy.Stream(api.auth, tweets_listener)
# print("start stream")
#
# (stream.filter(track=["#emoj_flowers"], languages=["en","ar" ]))
#
while True:
        print("start stream")
        try:

            #stream.filter(track=keyword_list, stall_warnings=True)

            stream = tweepy.Stream(api.auth, tweets_listener)
            stream.filter(track=["#emoj_flowers"], languages=["en", "ar"],stall_warnings=True)

        except IncompleteRead as e:
            # Oh well, sleep sometime & reconnect and keep trying again
                            id_str = data['id_str']

                    except KeyError as e:
                        logging.error("Error on_data %s" % str(e))

                    # Determine the sentiment of the tweet
                    vs = analyzer.polarity_scores(tweet)
                    sentiment = vs['compound']
                    if not tweet.startswith('RT'):
                        message = json.dumps(
                            {
                                "time_ms": time_ms,
                                "id_str": id_str,
                                "tweet": tweet,
                                "sentiment": sentiment
                            },
                            sort_keys=True)
                        logging.debug(message)
                        producer.send(topic, message.encode("utf-8"))
                    return True

            except BaseException as e:
                logging.error("Error on_data %s" % str(e))
                return true

    myStreamListener = MyStreamListener()
    myStream = tw.Stream(auth=api.auth,
                         listener=myStreamListener,
                         tweet_mode='extended')
    myStream.filter(track=[topic], is_async=True)
            coords = json.dumps(coords)

        table = db["tweets"]
        
        try:
            table.insert(dict(
                user_description=description,
                user_location=loc,
                coordinates=coords,
                text=text,
                geo=geo,
                user_name=name,
                user_created=user_created,
                user_followers=followers,
                id_str=id_str,
                created=created,
                retweet_count=retweets,
                polarity=sentiment.polarity,
                subjectivity=sentiment.subjectivity,
            ))
        except ProgrammingError as err:
            print(err)

    def on_error(self, status_code):
        if status_code == 420:
            return False
        
twitterstream = tp.Stream(auth, listener())
twitterstream.filter(track=['Trump'])

                'profile_image_url':
                status.user.profile_image_url,
                'receiver_at':
                datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            }
            storage.push(tweet_item)
            print(f"Pushed to Redis: {tweet_item}")

    def on_error(self, status_code):
        # Shut down the stream if we get rate limit error
        if status_code == 420:
            print("Rate Limit error")
            return False


# Get API Auth tokens
config_path = 'config/api.json'
with open(config_path) as config_file:
    twitter_api = json.loads(config_file.read())
consumer_key = twitter_api["consumer_key"]
consumer_secret = twitter_api["consumer_secret"]
access_token = twitter_api["access_token"]
access_token_secret = twitter_api["access_token_secret"]

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

api = tweepy.API(auth)
stream = tweepy.Stream(auth=api.auth, listener=StreamListener())
stream.filter(track=TRACKED_BRANDS_LIST)
Exemple #23
0
    def collect_by_streaming():

        connexion = tcs.twitter_setup()
        listener = StdOutListener()
        stream=tweepy.Stream(auth = connexion.auth, listener=listener)
        stream.filter(track=[nom_candidate])
consumer_secret = ''
access_token = ''
access_secret =  ''

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
file = codecs.open("an_hotel.txt", "w", "utf-8")


#Classes

class MyStreamListener(tweepy.StreamListener):
    """ StreamListener to find 'an hotel' and 'an hospital' """
    
    def on_status(self, status):
        print(status.text.encode('utf-8'))
    
    def on_data(self, data):
        json_data = json.loads(data)
        file.write(str(json_data))
    
    def on_error(self, status_code):
        if status_code == 420:
            #returning False in on_data disconnects the stream
            return False

myStream = tweepy.Stream(auth, MyStreamListener())
myStream.filter(track=["an hotel", "an hospital"], async=True)

Exemple #25
0
            TweetListener.add_log(
                self, consts.LOG_GAME_STOPPED_FAIL + status.user.screen_name)
            api.update_status(
                "@" + status.user.screen_name + "\n" +
                consts.TWEET_GAME_STOPPED_FAIL, status.id)

    #Searches for a command inside a tweet
    #If a command is found executes it and returns ok
    def search_command(self, status):
        tab = status.text.lower().split()
        for ta in tab:
            if ta == consts.COMMAND_START:
                TweetListener.create_game(self, status)
                return consts.RETURN_NO_ERROR
            if ta == consts.COMMAND_QUIT:
                TweetListener.quit_game(self, status)
                return consts.RETURN_NO_ERROR

    #If we receive an error what do we do
    def on_error(self, status):
        if status == 420:
            print "Slowing down..."
            return False
        print(status)


#launching the bot
tmp_player = pendu.player()
bot = TweetListener()
bot = tweepy.Stream(auth=api.auth, listener=bot)
bot.filter(track=['YOURTRACK'])
Exemple #26
0
def get_streaming_tweets(search_word):
    global myStream
    print 'the search word is : ' + search_word
    myStream = tweepy.Stream(auth=api.auth, listener=MyStreamListener())
    print myStream
    myStream.filter(track=[search_word])
Exemple #27
0
            clear_output(wait=True)
            status_array.append(status._json)

            if self.tweet_number >= self.max_tweets:
                print('PROCESSING COMPLETE : ' + str(self.max_tweets) +
                      ' tweets processed.')
                return False

                #sys.exit('PROCESSING COMPLETE : '+str(self.max_tweets)+' tweets processed.')

        def on_error(self, status_code):
            if status_code == 420:
                return False

    stream_listener = StreamListener()
    stream = tweepy.Stream(auth=api.auth, listener=stream_listener)

    #print('Pulling down data.....')
    stream.filter(track=[TOPIC], languages=["en"])

elif scantype == 2:
    # HISTORICAL DATA SWITCHED OFF (IF YOU SWITCH ON, THEN SWITCH STREAM OFF ABOVE)

    status_array = []
    LOOP_COUNT = 0

    # ------ENTER YOUR SERACH TERM HER E--------
    tweets = tweepy.Cursor(api.search,
                           q=TOPIC,
                           lang='en',
                           count=int(max_count))
def collect_data(tweetsPath,
                 consumer_key,
                 consumer_secret,
                 access_token,
                 access_token_secret,
                 tweets_count_to_collect,
                 terms=['#earthquake', '#tsunami', 'earthquake', 'tsunami']):

    new_tweets = []
    rate_error = False

    # consumer_key = "JflKYuYqaJvGNkPaZnImasKyS"
    # consumer_secret = "4NnZJjqyuQHolgoJRrBHdmEjE68ZI2Hl5aJmFFtQEh7IsYaaH9"
    # access_token = "159404488-0kY7styL2iWoF8f7FWk1Sk2Q2yAluNzOQrZCL4ID"
    # access_token_secret = "hz1CmckrUmEnB1Jx0rBhJun6P8F5C23mJpiCi3uYj5PCh"

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

    api = tweepy.API(auth)

    storePath = tweetsPath + "/"
    # terms = ['cyberattack', 'cybersecurity', 'cyberwar', 'DDOS', 'cyberhack', 'cyberespionage', 'cyberrisk']
    # terms = ['@INCIndiaLive', '@narendramodi', '#RafaleDeal', '#RahulGandhi', '#NamoAgain', '#NaMoAgain', '#RafaleScam', '#MeraParivarBhajapaParivar', '#Modi4NewIndia', '#PriyankaGandhi', '#PhirEkBaarModiSarkar', '#2019Election', '#MeraParivarBhajpaParivar', 'indian election', 'election in india', 'india election', '@RahulGandhi','@INCIndia']
    GEOBOX_WORLD = [-180, -90, 180, 90]

    #     data_count = 0
    #     tweets_count_to_collect = 10

    class StdOutListener(tweepy.StreamListener):
        def __init__(self, api=None):
            super(StdOutListener, self).__init__()
            self.num_tweets = 0
            self.start = datetime.utcnow()
            self.write = codecs.open(
                storePath + 'tweets_' + str(self.start.month) + '_' +
                str(self.start.day) + ".json", 'a', 'utf-8')
            time_now = datetime.now()
            self.write.write('time:{}\n'.format(str(time_now)))

        def on_data(self, data):
            #             global write
            #             global start

            #             data_count += 1
            try:
                #                 print('data_count: ', data_count)
                self.write.write(str(data))
            except Exception as e:
                print(e)
                print(str(data))

            end = datetime.utcnow()
            #             global start
            if (end.day != self.start.day):
                self.start = end
                write.close()
                write = codecs.open(
                    storePath + 'tweets_' + str(self.start.month) + '_' +
                    str(self.start.day) + ".json", 'a', 'utf-8')

            self.num_tweets += 1
            if self.num_tweets < tweets_count_to_collect:
                #                 print(self.num_tweets)
                #             collection.insert(record)
                new_tweets.append(str(data))
                return True
            else:
                return False

        def on_limit(self, status):
            print("Rate Limit Exceeded, Sleep for 1 Mins")
            time.sleep(60)
            self.num_tweets = self.num_tweets * 2  # get more data in a batch
            rate_error = True
            return True

        def on_error(self, status):
            if (status == 420):
                rate_error = True
                self.num_tweets = self.num_tweets * 2
                print(
                    'Because of error, updating the number of tweets to download: ',
                    self.num_tweets)
            print('error status', status)

    #     if __name__ == '__main__':
    l = StdOutListener()
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = tweepy.Stream(auth, l)
    #     while(True):
    try:
        stream.filter(track=terms, encoding='utf-8')  #locations=GEOBOX_WORLD,
    except KeyboardInterrupt:
        print('keyboard interruptions detected')
        stream.disconnect()
#         break
    except Exception as e:
        print(time.gmtime())
        print("severe problem", e)


#         continue
    print('rate_error: ', rate_error)

    return new_tweets, rate_error
Exemple #29
0
def main(keywords):
    api = create_api()
    tweets_listener = FavRetweetListener(api)
    stream = tweepy.Stream(api.auth, tweets_listener)
    stream.filter(track=keywords, languages=["en"])
    def on_error(self, status):
        """Called when a non-200 status code is returned"""
        if status == 420:
            self.on_timeout()

    def on_timeout(self):
        """ Limit Timeout """
        logger.warning("API Reach its limit, sleep for 10 minutes")
        time.sleep(600)
        return


if __name__ == '__main__':
    listen = TwitterStream()
    stream = tweepy.Stream(auth, listen)
    move = True
    while move:
        try:
            logger.info('Start Loop')
            stream.filter(
                locations=[141.157913, -38.022041, 146.255569, -36.412349])
            move = False
            logger.info('Finish Loop')
        except Exception as e:
            logger.error('Error found')
            logger.error(e)
            loop = True
            stream.disconnect()
            time.sleep(600)
            continue