Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
followers=status.user.followers_count,
id_str=status.id_str,
created=status.created_at,
retweet_count=status.retweet_count,
friends_count=status.user.friends_count,
source=status.source,
retweet=retweet,
# do not exist for every tweet
original_id=None if original_id is None else original_id,
original_name=None if original_name is None else original_name,
hashtags=None if hashtags is None else hashtags,
)
return tweet
class StreamListener(tweepy.StreamListener):
def __init__(self, api=None, connection_string=None, table="tweet", verbose=False):
super(StreamListener, self).__init__()
self.counter = 0
self.batch_size = 100
self.verbose = verbose
self.tweet_list = []
self.start = datetime.datetime.utcnow()
self.setup_backend(connection_string, table)
def setup_backend(self, db, table):
db_type = db.split(":")[0]
if db_type == "mongodb":
self.client = MongoClient(db)
self.db = self.client.twitter
self.table = self.db[table]
import tweepy
import json
from pymongo import MongoClient
class StreamListener(tweepy.StreamListener):
"""
tweepy.StreamListener is a class provided by tweepy used to access
the Twitter Streaming API. It allows us to retrieve tweets in real time.
"""
def on_connect(self):
"""Called when the connection is made"""
print("You're connected to the streaming server.")
def on_error(self, status_code):
"""This is called when an error occurs"""
print('Error: ' + repr(status_code))
return False
def on_data(self, data):
"""This will be called each time we receive stream data"""
#https://stackoverflow.com/questions/23601634/how-to-restart-tweepy-script-in-case-of-error
#from httplib import IncompleteRead
# user application credentials
consumer_key = "5uzIc3mu5bdqKfgPM2Mysnc0V"
consumer_secret = "cDrkl7CIZlzLfN6688xjJDmN5l2EDX8brTcfZCZUPjoDOdYfh8"
access_token = "4904583673-vMkTnb7l9pnR1X6wPhI8ceDR4BCwqexZzYvOWEl"
access_token_secret = "GiCJGDGW5m61Ew8NRl3Y6zbLuz8Ndlar7hD9CprwsSqxi"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
super(tweepy.StreamListener, self).__init__()
self.db = pymongo.MongoClient().disaster
def on_status(self, status):
#print status.text , "\n"
data ={}
data['text'] = status.text
data['created_at'] = status.created_at
data['geo'] = status.geo
data['source'] = status.source
self.db.Tweets.insert(data)
reply_id (int): optional, nests the tweet as reply to that tweet (use id_str element from a tweet)
media_ids (list of media_ids): optional, media to attach to the tweet
Returns:
bool: True if posted successfully, False otherwise
'''
tweet_text = trim_tweet_text(tweet_text)
try:
twitter_api.update_status(tweet_text, reply_id, media_ids=media_ids)
return True
except tweepy.error.TweepError as e:
cozmo.logger.error("post_tweet Error: " + str(e))
return False
class CozmoTweetStreamListener(tweepy.StreamListener):
'''Cozmo wrapper around tweepy.StreamListener
Handles all data received from twitter stream.
'''
def __init__(self, coz, twitter_api):
super().__init__(api=twitter_api)
self.cozmo = coz
self.twitter_api = twitter_api
def trim_tweet_text(self, tweet_text):
'''Trim a tweet to fit the Twitter max-tweet length'''
return trim_tweet_text(tweet_text)
def upload_images(self, images, image_format='jpeg', quality=90):
'''Upload Image(s) to twitter using the given settings
one_minute_in_milliseconds = 60000
def sell_after_pecentage_gain(bought_price, market, amount):
sold = False
while not sold:
cur_price = binance_utils.get_cur_price_from_large_enough_buy_order(binance, market, amount)
if utils.percent_change(bought_price, cur_price) > desired_gain_percent:
sold = binance_utils.market_sell_on_binance(binance, market)
if not sold:
time.sleep(seconds_before_checking_binance_price)
class MyStreamListener(tweepy.StreamListener):
# Called when there is a new status
def on_status(self, status):
tweet_time = int(status.timestamp_ms)
cur_time = int(round(time.time() * 1000))
# Tweets will queue wile we are waiting to sell
# and we don't want to buy on old data
if cur_time - tweet_time < one_minute_in_milliseconds:
if utils.contains_words_to_look_for(status.text, words_to_look_for):
coin_name = utils.get_coin_name_in_text(status.text, ignored_coins, binance_coins)
if coin_name:
utils.print_and_write_to_logfile(coin_name + " in tweet: " + status.text)
listener = StreamMonitor(epd, image, draw, name_font, message_font)
stream = tweepy.Stream(auth, listener)
setTerms = argv
# stream.sample() # low bandwidth public stream
stream.filter(track=setTerms)
def find_font(font_list):
"""find a font file from a list of possible paths"""
for f in font_list:
if os.path.exists(f):
return f
return ''
class StreamMonitor(tweepy.StreamListener):
"""class to receive twitter message"""
def __init__(self, epd, image, draw, name_font, message_font, *args, **kwargs):
super(StreamMonitor, self).__init__(*args, **kwargs)
self._epd = epd
self._image = image
self._draw = draw
self._name_font = name_font
self._message_font = message_font
def on_status(self, status):
screen_name = status.user.screen_name.encode('utf-8')
text = status.text.encode('utf-8')
print('@{u:s} Said: {m:s}'.format(u=screen_name, m=text))
w, h = self._image.size
import tweepy
import sys
# user application credentials
consumer_key = "5uzIc3mu5bdqKfgPM2Mysnc0V"
consumer_secret = "cDrkl7CIZlzLfN6688xjJDmN5l2EDX8brTcfZCZUPjoDOdYfh8"
access_token = "4904583673-vMkTnb7l9pnR1X6wPhI8ceDR4BCwqexZzYvOWEl"
access_token_secret = "GiCJGDGW5m61Ew8NRl3Y6zbLuz8Ndlar7hD9CprwsSqxi"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def __init__(self, api):
self.api = api
super(tweepy.StreamListener, self).__init__()
def on_status(self, status):
print status.text , "\n"
# handle errors without closing stream:
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True
import tweepy
'''STREAM'''
class StreamListener(tweepy.StreamListener):
# Decided I would keep all the overridable functions from the
# BaseClass so we know what we have to play with.
def __init__(self, twweeter_obj, time_limit=60):
self.twweeter_obj = twweeter_obj
super(StreamListener, self).__init__(self.twweeter_obj.api)
def on_status(self, status):
print('@{} => {}'.format(status.user.screen_name,
status.text.replace("\n", " ")))
def on_error(self, status_code):
print('AN ERROR: {}'.format(status_code))
# read the docs and handle different errors
def keep_alive(self):
"""Called when a keep-alive arrived"""
from kafka.client import KafkaClient
def initialize():
with open('data/config.json') as config_data:
config = json.load(config_data)
auth = tweepy.OAuthHandler(config['consumer_key'], config['consumer_secret'])
auth.set_access_token(config['access_token'], config['access_token_secret'])
api = tweepy.API(auth)
stream = TwitterStreamListener()
twitter_stream = tweepy.Stream(auth = api.auth, listener=stream)
twitter_stream.filter(track=['iphone'], async=True)
class TwitterStreamListener(tweepy.StreamListener):
def __init__(self):
self.producer = KafkaProducer(bootstrap_servers='docker:9092', value_serializer=lambda v: json.dumps(v))
self.tweets = []
def on_data(self, data):
text = json.loads(data)[u'text']
self.producer.send('iphone', text)
self.producer.flush()
print(text)
def on_error(self, status_code):
if status_code == 420:
return False
if __name__ == "__main__":
from mongoengine import connect
from models import Tweet
connect(MONGODB_NAME, host=MONGODB_URI)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user = api.get_user(username)
# Geolocator to convert address to coordinates
geolocator = Nominatim()
class ViolentStreamListener(tweepy.StreamListener):
def on_status(self, status):
name = status.author.name
handle = status.author.screen_name
date = int(status.created_at.strftime('%s')) * 1000
num_retweets = status.retweet_count
location = status.coordinates
if location is None and status.place is not None:
place = geolocator.geocode(status.place.full_name)
location = [place.longitude, place.latitude]
content = status.text
risk_level = random.uniform(70, 90)
new_tweet = Tweet(name=name, handle=handle, date=date, num_retweets=num_retweets, location=location, content=content, risk_level=risk_level)
new_tweet.save()
stream_listener = ViolentStreamListener()
stream = tweepy.Stream(auth = api.auth, listener=stream_listener)