def main(): (options, args) = get_parser() auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) api = API(auth_handler=auth) stream = Stream(auth, CustomStreamListener(api, options)) stream.timeout = None stream.userstream()
def monitor(self, parent, connection): ntpclient = ntplib.NTPClient() silentChannelsFile = open("./modules/static/NOTWEETCHANNELS", 'r') accountsToMonitorFile = open("./modules/static/TWITTERACCOUNTS", 'r') self.silentChannels = silentChannelsFile.read().splitlines() self.accounts = accountsToMonitorFile.read().splitlines() silentChannelsFile.close() accountsToMonitorFile.close() cm = ChannelMessager() cm.setup(self.silentChannels, self.accounts, connection, parent) keyFile = open("modules/pubmsg/TwitterKeys", 'r') keyList = keyFile.read().splitlines() keyFile.close() for entry in keyList: if entry.startswith('#'): keyList.remove(entry) consumerKey = keyList[0] consumerSecret = keyList[1] accessKey = keyList[2] accessSecret = keyList[3] auth = OAuthHandler(consumerKey, consumerSecret) auth.set_access_token(accessKey, accessSecret) while True: stream = Stream(auth, cm, timeout=60) try: stream.userstream(_with='followings') except: print "Error, restarting stream"
def stream_tweets(self, tweetsFile, keywords, users): #listener object listener = TwitterListener(tweetsFile) #variable created to pass credentials to OAuthHandler to handle authentication auth = OAuthHandler(api_access.consumer_key, api_access.consumer_secret) #finishes authentication auth.set_access_token(api_access.access_token, api_access.access_token_secret) #create a stream passing authentication and listener object we created stream = Stream(auth, listener) #depending on if keyword or user is given as parameter, filter stream by what is given if len(keywords) > 0: if len(users) > 0: stream.filter(track=keywords, follow=users) else: stream.filter(track=keywords) elif len(users) > 0: stream.filter(follow=users) else: stream.userstream(_with="following")
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)
def run(self,user,message): if utilities.getCommand() == "autotweet": streamListener = ReplyToTweet() streamListener.setAPI(self.twitterApi) streamListener.setUser(self.account_user_id) twitterStream = Stream(self.auth, streamListener) twitterStream.userstream(_with='user')
def __init__(self,slacker): # auth auth = OAuthHandler(settings.twitter_consumer_key, settings.twitter_consumer_secret) auth.set_access_token(settings.twitter_access_token, settings.twitter_access_token_secret) # out l = StdOutListener(slacker) # stream stream = Stream(auth, l) print("opening twitter stream") # if only a certain list if FILTER_LIST: api = API(auth) employees = api.list_members(LIST_USER,LIST) list = map(lambda val: str(val),employees.ids()) #print(list) print("only List: "+LIST) stream.filter(follow=list) elif FILTER_KEYWORDS: print("only Keywords: "+str(KEYWORDS)) stream.filter(track=KEYWORDS) else: print("your timeline") stream.userstream()
def main(): """ params: none return None: responsible for handling authentication with twittor api, and setting up stream """ global __API__ global __CONFIG__ try: # setting up authentication auth = OAuthHandler(__CONFIG__["consumer_token"], __CONFIG__["consumer_secret"] ) # passing in token and secret to handler auth.secure = True # making sure secure login is set maybe deprecated auth.set_access_token( __CONFIG__["access_token"], __CONFIG__["access_token_secret"] ) # set access token so we now have access to api # authenticate the twitter api __API__ = API(auth) # initiating tweepy (twitter) api # set up stream object, and start userstream by its method stream = Stream(auth, StdOutListener( )) # initiating listening stream, and passing in stream obj with auth stream.userstream( ) # starting user stream for user functionality listening stream # caution, this is a blocking call except Exception as e: print "Error in main()", e
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
def handle(self, *args, **options): l = StdOutListener() auth = tweepy.OAuthHandler('kFUnVwYAFmthrPg3P4MCDXRuf', 'LfY3zyzYEohCQLAH0f9ZuPE8Yok9ZXnyjAVvAJHNfYymgCwIsV') auth.set_access_token('2653803506-b9z9K01SaIChExXWlMnkwnpWAAzPPlsPzIEIE0k', 'PC6WIon73biuA87Wca2TuuincwFwzGyk78W4wWEFmIJNI') stream = Stream(auth, l) stream.userstream('HTR2014Live') return True
def main(): streamListener = ReplyToTweet() twitterStream = Stream(auth, streamListener) # twitterStream.userstream(_with='user') print "Ready to listen." twitterStream.userstream(replies=all)
def streamTwitter(self): Log.record('\tWill Read From Config') self.readFromConfig() Log.record('\tWill Log into Twitter') api = self.loginToTwitter() Log.record('\tWill Stream Tweets from Twitter') stream = Stream(self.api.auth, StdOutListener()) stream.userstream('survey11909')
def main_loop(): global can_send_after while 1: message = ircsock.recv(2048) # Receive data from the server. message = message.strip('\n\r') # Remove any unnecessary linebreaks. print message if "PING :" in message: pong(message) # Only respond to chat from the current chatroom (not private or administrative log in messages). if splitter not in message: continue # Get the content of the message. user = message.split("!")[0][1:] message = message.split(splitter)[1] # Convert to lowercase and split the message based on whitespace. split = message.lower().split() if split[0] == args.nick.lower() + ":": # Command addressed to the bot (e.g. learn or forget). if split[1] == "learn" and len(split) > 2: learn(split[2], message.split()[3:], user) elif split[1] == "forget" and len(split) == 3: forget(split[2]) elif split[1] == "help": send_help() elif split[1] == "quiet" and len(split) == 2: can_send_after = datetime.now() + timedelta(seconds=args.quiet) send("Whatever you say.", user, True) elif split[1] == "quiet" and len(split) == 3 and is_positive_number(split[2]): can_send_after = datetime.now() + timedelta(seconds=int(split[2])) send("Whatever you say.", user, True) elif split[1] == "speak" and len(split) == 2: can_send_after = datetime.now() elif split[1] == "list" and len(split) == 2: send("Builtin commands: [" + ", ".join(default_commands) + "]") send("User commands: [" + ", ".join(user_commands) + "]") elif split[1] == "blame" and len(split) == 3: if split[2] in default_commands: send(split[2] + " is a default command.", user) elif split[2] in user_commands: send(split[2] + " was created by " + user_commands[split[2]][1], user) else: send("That's not a valid keyword!", user) elif split[1] == "autotweet" and len(split) > 2: streamListener = ReplyToTweet() twitterStream = Stream(auth, streamListener) twitterStream.userstream(_with='user') send("AutoTweeting...", user, True) elif split[1] == "autofbook" and len(split) > 2: PostToWall() send("AutoFbooking...", user, True) else: send("How may I help you?", user) else: # Only handle messages that aren't sent directly to the bot. handle(message.lower(), default_commands) handle(message.lower(), user_commands)
def listen(): start_twitter() try: app_exfiltrate.log_message('info', "[twitter] Listening for DMs...") stream = Stream(auth, StdOutListener()) stream.userstream() except Exception, e: app_exfiltrate.log_message( 'warning', "[twitter] Couldn't listen for Twitter DMs".format(e))
class Twitter: def __init__(self, setup=['stream'], tweet_callback=lambda x, y, z: x, image_only=False): self.tweet_callback = tweet_callback self.image_only = image_only self.listener = TwitterListener(self.handle_tweet) self.auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) self.auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) if 'stream' in setup: print('Starting to listen to twitter stream...') self.stream = None self.run_stream() if 'cursor' in setup: print('Starting to scan twitter timeline...') self.cursor = CursorListener(self.auth, self.handle_tweet) self.cursor.run() def run_stream(self): while True: self.stream = Stream(self.auth, self.listener, timeout=60) self.stream.filter(follow=FOLLOW_IDS) try: self.stream.userstream() except Exception as e: print("Error. Restarting Stream.... Error: ") print(e.__doc__) print(e.message) def handle_tweet(self, tweet_json): screen_name = tweet_json["user"]["screen_name"] id = tweet_json["id_str"] text = tweet_json["text"].replace("\\", "") is_image = False # Get media if present try: urls = [x["media_url"].replace("\\", "") for x in tweet_json["entities"]["media"] if x["type"] == "photo"] for url in urls: response = requests.get(url) img = Image.open(io.BytesIO(response.content)) # Extract text from image # print(img) is_image = True img_text = pytesseract.image_to_string(img) text += f' . {img_text}' except KeyError: pass link = f'https://twitter.com/{screen_name}/status/{id}' try: if not self.image_only or (self.image_only and is_image): self.tweet_callback(text, screen_name, link) except: pass
def main(): try: print(api.me().name) stream = Stream(auth, MyStreamListener()) stream.userstream() except BaseException as e: print("Error in main()", e)
def main(): print('Welcome to Snap \'N\' Go!') auth = OAuthHandler(consumer_key, consumer_key_secret) auth.secure = True auth.set_access_token(access_token, access_token_secret) dmlistener = Stream(auth, DMListener()) dmlistener.userstream()
def run(self): log_info(str(timestamp()) + ": Starting "+ self.name) keyfile = open('twitter_keys.txt', 'r') cfg = json.loads(keyfile.read()) #todo: load from config file api = get_api(cfg) auth = OAuthHandler(cfg['consumer_key'], cfg['consumer_secret']) auth.set_access_token(cfg['access_token'], cfg['access_token_secret']) streamListener = ProcessTweet() twitterStream = Stream(auth, streamListener) twitterStream.userstream()
def run(self): try: self.listener = StdOutListener() stream = Stream(self.auth, self.listener) stream.userstream() print('Listening to Twitter finally stopped.') except BaseException as e: print("Error in main()", e)
def main(): try: stream = Stream(auth, CustomStreamListener()) stream.timeout = None #userstreamの開始 print("start") stream.userstream() except KeyboardInterrupt : print("\nexit: KeyboardInterrupt") return
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)
def openStream(): listener = TweetListener() stream = Stream(auth, listener, timeout=60) connected = False while True: try: if not connected: connected = True stream.userstream() except SSLError, e: print e connected = False
def run(self): def except_on_closed(*args): raise ClosedException("Twitter closed the stream!") while True: logging.info("Looping to start the tweeter; watching for %s and replying with %s", self.watchword, self.reply) try: tStream = Stream(auth, WatchwordListener(self.watchword, self.reply)) tStream.on_closed = except_on_closed tStream.userstream() except Exception as e: logging.exception("Encountered an error: %s", e) time.sleep(20)
def main(): global api try: auth = OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET) auth.secure = True auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) api = API(auth) stream = Stream(auth, StdOutListener()) stream.userstream() except BaseException as e: print("Error en main()", e)
def main(): global api try: auth = OAuthHandler(CONSUMER_TOKEN, CONSUMER_SECRET) auth.secure = True auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) api = API(auth) stream = Stream(auth, StdOutListener()) stream.userstream() except BaseException as e: print("Error in main()", e)
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")
def main(): auth = OAuthHandler(settings.consumer_key, settings.consumer_secret) auth.set_access_token(settings.access_token, settings.access_secret) server = couchdb.Server() db = server[settings.database] listener = CouchDBStreamListener(db) stream = Stream(auth, listener) try: stream.userstream() except KeyboardInterrupt: print("Total tweets received: %d" % listener.tweet_count)
def main(): SnapNGo.readFile("Wellesley_Outdoor_Map.txt") print('Welcome to Snap \'N\' Go!') auth = OAuthHandler(consumer_key, consumer_key_secret) auth.secure = True auth.set_access_token(access_token, access_token_secret) dmlistener = Stream(auth, DMListener()) # dmlistener.userstream() snap = SnapNGo() # snap.selectAction() # creating thread t1 = threading.Thread(target=snap.selectAction()) t2 = threading.Thread(target=dmlistener.userstream()) # starting thread 1 t1.start() # starting thread 2 t2.start() # wait until thread 1 is completely executed t1.join() # wait until thread 2 is completely executed t2.join()
def read_twitter(project_id): try: project = ContentType.objects.get(app_label="twiscrape", model="project").model_class() project = project.objects.get(pk=project_id) l = TweetDjListener(project) auth = OAuthHandler(project.consumer_key, project.consumer_secret) auth.set_access_token(project.access_token, project.access_token_secret) logger.info('starting stream') stream = Stream(auth, l, tweet_mode='extended') #stream.filter(follow=["20428671"]) stream.userstream(encoding='utf8') except SoftTimeLimitExceeded: logger.info('ended the task') return 'ended the task'
def fetch(): print('hello') while True: try: l = PersianListener() auth = OAuthHandler(consumer_key_data, consumer_secret_data) auth.set_access_token(access_token_data, access_token_secret_data) stream = Stream(auth, l) stream.userstream("with=following") except IncompleteRead: time.sleep(1) continue except Exception as e: time.sleep(1) print('i\'m error :((') print(e) continue
def main(): try: auth = OAuthHandler(consumer_key, consumer_secret) auth.secure = True auth.set_access_token(access_token_key, access_token_secret) api = API(auth) # If the authentication was successful, you should # see the name of the account print out print(api.me().name) stream = Stream(auth, Listener()) stream.userstream() except BaseException as e: print("Error in main()", e)
def main(): try: auth = OAuthHandler(keys.consumer_key, keys.consumer_secret) auth.secure = True auth.set_access_token(keys.access_token, keys.access_token_secret) api = API(auth) # If the authentication was successful, you should # see the name of the account print out print(api.me().name) stream = Stream(auth, StdOutListener()) stream.userstream() except BaseException as e: print("Error in main()", e)
class TwitterStream(TwitterBase): """ A TwitterStream class that will continuously capture data from a Twitter timeline and store in our backend model. """ stream = None listener = None def __init__(self, listener): TwitterBase.__init__(self) self.listener = listener def start(self): logging.info('start:') self.stream = Stream(self.auth, self.listener) self.stream.userstream(_with='user', stall_warnings=True, async=False) def stop(self): logging.info('stop:') self.stream.disconnect()
def run(parser_queue): """So this can be loaded as a module and run via multiprocessing""" global cmd_parser_queue cmd_parser_queue = parser_queue # Log into Twitter, get credentials. #try: # if (check_twitter_auth() == False): # sys.exit() # logging.debug("Authorized") #except: # logging.critical("FATAL: Authorization failed, exiting process.") # We need to figure out what to do to recover from this failure, if it happens. # sys.exit() tokens = twitter.oauth.read_token_file(fn) printme("We have authorization tokens") l = StdOutListener() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(tokens[0], tokens[1]) # # Setup an API thingy # try: # a_thingy = API(auth) # logging.debug("Got API of %s" % a_thingy) # except: # logging.critical("Failed to get the API for tweepy!") # Set up the stream listener stream = Stream(auth, l) # Suspect we need to make a different call here that just gets all mentions #stream.filter(track=[self.searchterm]) # Blocking call. We do not come back. stream.userstream() # Blocking call. We do not come back. We think this is right. Possibly. # If we've gotten here, the stream has died, in which case we need to restart this whole business. # I've noticed the stream does die periodically. All we can really do is restart. And hope. # logging.critical("Stream died, restarting...") import subprocess subprocess.call(["/home/mpesce/scripts/restart.sh"]) # This should kill us dead, and restart us.
def main(): logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) # create a file handler handler = logging.FileHandler('goddit.log') handler.setLevel(logging.INFO) # create a logging format formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # add the handlers to the logger logger.addHandler(handler) try: auth = OAuthHandler(consumer_key, consumer_secret) auth.secure = True auth.set_access_token(access_token, access_token_secret) api = API(auth) # If the authentication was successful, you should # see the name of the account print out #print(api.me().name) stream = Stream(auth, tweetchazzer(api=api, logger=logger)) s = stream.userstream() #while True: # s = stream.filter(track=['tweetchazzer']) #print(str(s)) except BaseException as e: print("Error in main()" +str(e))
def enclosed_func(): auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) stream = Stream(auth, listener) logging.info("Attempting to connect the stream.") stream.userstream(_with='user')
api = tweepy.API(auth) highest_dm_id = 0 print "Old messages:" for dm in api.direct_messages(): print "(" + str(dm.id) + ") " + dm.sender_screen_name +" : "+ dm.text if dm.id > highest_dm_id: highest_dm_id = dm.id print "Check for followers and automatically add them." for follower in tweepy.Cursor(api.followers).items(): print follower.screen_name follower.follow() tempfile.mkdtemp(prefix="welightimgs") print "Temp directory for images: " + tempfile.gettempdir() # l = StdOutListener() # stream = Stream(auth, l) # stream.userstream() while True: try: l = StdOutListener() stream = Stream(auth, l) stream.userstream() except: print "tweepy error, restarting" continue
os.path.join( directory, ('streamer.' + time.strftime('%Y%m%d-%H%M%S') + '.json')).replace("\\", "/"), 'ab') self.counter = 0 return else: pass else: pass def on_error(self, status): print(status) auth = OAuthHandler(consumer_key.rstrip(), consumer_secret.rstrip()) auth.set_access_token(api_key.rstrip(), api_secret.rstrip()) while True: twitterStream = Stream(auth, listener(), tweet_mode='extended', timeout=60) #twitterStream.filter(locations=[19,59,32,70], stall_warnings = True) twitterStream.filter(track=words) #twitterStream.filter(languages=["sv","fi","no","da"], track = words) try: twitterStream.userstream() except Exception as e: print("Error. Restarting Stream.... Error: ") print(e.__doc__) print(e.message)
# Convert the message to JSON json_data = json.loads(data) if 'id_str' not in json_data: # If this isn't a status, do nothing. print "no ID" else : print json_data['id_str'] # Write a file "123456789.json" containing the Tweet and all the metadata text_file = open(json_data['id_str'] + ".json", "w") text_file.write(data) text_file.close() if 'delete' in data: # Deleted Tweets look like this #{"delete":{"status":{"id":123456789,"user_id":99999999,"id_str":"123456789", print "DELETED!" # Write a file "123456789-DELETED.json" containing the metadata. Do NOT delete the original Tweet text_file = open(json_data['delete']['status']['id_str'] + "-DELETED.json", "w") text_file.write(data) text_file.close() return True def on_error(self, status): print status # Start consuming from the stream. This will get all the Tweets & Deletions from the users the user is following. twitterStream = Stream(auth, listener()) twitterStream.userstream("with=following")
TWITTER_ACCESS_SECRET = 'AwzXXO91l1oEWcP4hHrmRMML9F1o7jLYR0iXik2jXY95G' auth = OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET) auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_SECRET) api = tweepy.API(auth) class tweetListener(StreamListener): def on_connect(self): print "connect" def on_status(self, data): newTweet = Tweet(tweet_id=data.id, tweet_text=data.text, tweet_time=data.created_at, tweet_user=data.author.screen_name, tweet_location=data.author.location) newTweet.save() return def on_error(self, error): print error # def on_delete(self, status_id, user_id): # print status_id # print user_id return twitter_stream = Stream(api.auth, tweetListener()) twitter_stream.userstream() twitter_stream.filter(track=sys.argv[1:])
class StdOutListener(StreamListener): """ A listener handles tweets are the received from the stream. This is a basic listener that just prints received tweets to stdout. """ def on_data(self, data): tty.output(data) return True def on_error(self, status): print status if __name__ == '__main__': l = StdOutListener() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) tty = Teletype(hmhost=('localhost', 11123)) stream = Stream(auth, l) # with args, use args as search terms if len(sys.argv) > 1: tty.ttysock.sendall('streaming search terms.\n\n') stream.filter(track=sys.argv[1:]) else: # stream our own twitter timeline tty.ttysock.sendall('streaming timeline.\n\n') #stream.userstream(myscreenname) #stream.userstream(_with='followings') stream.userstream(encoding='utf8')
try: # jsonData=json.loads(data) # print(str(jsonData['text']).encode("utf-8",errors="ignore")) text = getTweets(data) if text != None: print(getTweets(data)) return True except BaseException as e: sys.stderr.write("Error on_data : {}\n".format(e)) time.sleep(5) return True def on_error(self, status): if status == 420: sys.stderr.write("Rate limit Exceeded\n") return False else: sys.stderr.write("Error{}\n".format(status)) return True def getTweets(data): jsonData = json.loads(data) return jsonData.get('text') if __name__ == '__main__': auth = getTwitterAuth() twitterStream = Stream(auth, CustomListener()) twitterStream.userstream(encoding='utf-8')
if 'id_str' not in json_data: # If this isn't a status, do nothing. print "no ID" else: print json_data['id_str'] # Write a file "123456789.json" containing the Tweet and all the metadata text_file = open(json_data['id_str'] + ".json", "w") text_file.write(data) text_file.close() if 'delete' in data: # Deleted Tweets look like this #{"delete":{"status":{"id":123456789,"user_id":99999999,"id_str":"123456789", print "DELETED!" # Write a file "123456789-DELETED.json" containing the metadata. Do NOT delete the original Tweet text_file = open( json_data['delete']['status']['id_str'] + "-DELETED.json", "w") text_file.write(data) text_file.close() return True def on_error(self, status): print status # Start consuming from the stream. This will get all the Tweets & Deletions from the users the user is following. twitterStream = Stream(auth, listener()) twitterStream.userstream("with=following")
tweetText = tweetText.replace("@nambot2016","").replace("@NAMbot2016","") tweetText = tweetText.replace("@NAMBOT2016","") chatResponse = conv.sub(tweetText) #chatbot.respond(tweetText) if tweetText.lower() == chatResponse.lower(): chatResponse = "@{} That looks pretty good already!".format(screenName) replyText = chatResponse else: replyText = chatResponse + ' @' + screenName #check if repsonse is over 140 char if len(replyText) > 140: replyText = replyText[0:137] + '...' print('Tweet ID: ' + tweetId) print('From: ' + screenName) print('Tweet Text: ' + tweetText) print('Reply Text: ' + replyText) # If rate limited, the status posts should be queued up and sent on an interval self.api.update_status(status=replyText, in_reply_to_status_id=tweetId) def on_error(self, status): print status if __name__ == '__main__': twitter = Twitter() conv = Converter() streamListener = ReplyToTweet(conv) twitterStream = Stream(streamListener.auth, streamListener) twitterStream.userstream(_with='user')
if (self.lastTweetedCount > 10): return #rate limiting - if attempts is greater than 10, give up try: if (self.wait > 10): return elif (self.wait > 0): time.sleep(self.wait) self.api.update_status(postMsg, status.id) self.wait = 0 except Exception, a: self.wait += 1 return except Exception, e: return def on_error(self, status): print(status) if __name__ == '__main__': l = StdOutListener() stream = Stream(l.auth, l) #start listening to userstream stream.userstream()
if status.user.screen_name == 'boooootttt_': pass else: """this is the general reply for anyone who tagged the bot""" bot_.reply(status) except tweepy.TweepError as e: bot_.log('Stream Failed') bot_.log(e) return True """on_error prints the error status and stops the stream. this should not happen. if it does something is broken and needs to be fixed""" def on_error(self, status): bot_.log(status) return False """new instance of StdOutListener and markov using database.txt as an infile. then a stream is set up using config from the bot_. _with = 'user' lets the stream know we are only looking for ourself being mentioned""" l = StdOutListener() while True: bot_.log('Stream Started: Listening') stream = Stream(bot_.auth, l) stream.userstream(_with='user')
raise if __name__ == '__main__': ctrlc = False try: while True: try: config = {} execfile(sys.argv[1], config) td = TwitterDigester(config) auth = OAuthHandler(td.config['consumer_key'], td.config['consumer_secret']) auth.set_access_token(td.config['access_token'], td.config['access_token_secret']) stream = Stream(auth, td) stream.userstream(async=True) stream_filter = Stream(auth, td) stream_filter.filter(track=td.config['track_terms'], async=True) scheduler = sched.scheduler(time.time, time.sleep) while True: scheduler.enter(td.config['periodicity'], 1, td.send_email, ([])) scheduler.run() except KeyboardInterrupt: print print "Ctrl+C detected, sending email..." td.send_email() ctrlc = True except Exception as e: print e traceback.print_stack()
createdAt = " " tweetId = " " userId = " " userName = "******" tweetText = " " with open('tweets.csv', 'a') as f: from csv import writer csv = writer(f) row = [createdAt, tweetId, userId, userName, tweetText] values = [ (value.encode('utf8') if hasattr(value, 'encode') else value) for value in row ] csv.writerow(values) return True def on_error(self, status): print(status) if __name__ == '__main__': l = StdOutListener() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) stream = Stream(auth, l) stream.userstream(track='@realDonalTrump')
from tweepy.streaming import StreamListener from tweepy import OAuthHandler from tweepy import Stream import re #initialising Twitter user #id and secret auth = tweepy.OAuthHandler('bCZwVzklnlD74MUvsFDz0oFo2', 'qAmYYrV3MVqadWBMbW73eZ8e4Z5infJ62dLytzV1fZMoLAkKb4') #access token id, access token secret auth.set_access_token('409511921-C0fLT44yBcyl4GpRqwzEuyXnn84LJaYafcYLBG7M', 'KJ0wEMeuUSNBxNlpNi5Nb7ZLYn0KTDp01KosKRi1M9Dtq') api = tweepy.API(auth) #steam_tweets = api.user_timeline('steam_games') # for tweet in steam_tweets: # print(tweet.text) class StdOutListener(StreamListener): """ A listener handles tweets that are received from the stream.""" def on_status(self, status): if re.search('deal',status.text,re.IGNORECASE): with open('twitter.txt', 'w') as f: f.write(status.text) return True def on_error(self, status): print(status) stream = Stream(auth, StdOutListener()) stream.userstream('steam_games') stream.filter(track=['#SteamDailyDeal','#MidweekMadness']) print('Twitter ready')
# determine if sentiment is positive, negative, or neutral if tweet.sentiment.polarity < 0: sentiment = "Negative" elif tweet.sentiment.polarity == 0: sentiment = "Neutral" else: sentiment = "Positive" # output sentiment print "Sentiment : ",sentiment print "\n" return True # on failure def on_error(self, status): print status if __name__ == '__main__': # tweet stream listener listener = TweetStreamListener() # set twitter keys/tokens auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # create instance of the tweepy stream stream = Stream(auth, listener) stream.userstream(_with='followings')