def get_tweets(query, result_type, max_tweets): # Search the Twitter API for the given title twitter_client = Twitter(CONFIG['twitter']) tweet_data = twitter_client.search( "\"%s\" movie -download -stream -#nw -#nowwatching -RT" % (query, ), result_type=result_type, count=max_tweets) if DEBUG: print "Retrieved %d tweets (with result type %s)." % (len(tweet_data), result_type) # if DEBUG: # print json.dumps(tweet_data[0], sort_keys=True, indent=4, separators=(',', ': ')) # Build simple Twitter objects tweets = map(Tweet, tweet_data) # Add a filtered version of the tweet text pattern = re.compile(query.replace(" ", " ?"), re.I) for tweet in tweets: tweet.sentiment = get_cached_tweet_sentiment(tweet.id) tweet.filtered_text = pattern.sub('', tweet.text) return tweets
def tweet_earthquake(self, earthquake_key): # Always call this method after adding to the DB. earthquake = db.get(earthquake_key) if earthquake.magnitude <= 0.0 and len(earthquake.location) > 0: cnf = config.Config() t_creds = cnf.twitter_credentials() t = Twitter(t_creds['username'], t_creds['password']) bitly_creds = cnf.bitly_credentials() url = "http://zemjotresi.appspot.com/zemjotres/%s" % earthquake_key short_url = t.shorten_url(url) if short_url['good']: short_url = short_url['url'] else: logging.error("Coulnd't shorten url (%s). Data: %s." % (url, short_url)) return False template_id = int(round(random.uniform(1, 2))) template_path = os.path.join(os.path.dirname(__file__), "../template/tweets/tweet_%s.txt" % template_id) tweet = template.render(template_path, {'earthquake': earthquake, 'short_url': short_url}) status = t.update_status(tweet) if status['good']: logging.info("Tweeted status_id: %s !" % status["id"]) elif status['good'] == False and status["error"] == 403: logging.error("Couldn't tweet status for (%s). Exceeded update limit, got message: %s!" % (earthquake_key, status['message'])) else: logging.error("Couldn't tweet status for (%s). Got error %s with data: %s!" % (earthquake_key, status['error'], status['data'])) else: logging.info("Not tweeting status for (%s)!" % earthquake_key)
def read_twitter(): try: twitter = Twitter() twitter.get_list_id(setting['TWITTER_LIST']) messages = twitter.read_list_tl() tweet = Tweet() tweet.add_values(messages) except requests.exceptions.ConnectionError: pass
def add_tweetstream(body): # convert the query to a search query string s = Twitter.search(body["query"]) twitter.TweetStream(station_id=body["station_id"], stream_query=str(s)).put() return body
def add_tweetstream(body): # convert the query to a search query string s = Twitter.search( body["query"] ) twitter.TweetStream(station_id = body["station_id"], stream_query = str(s) ).put() return body
def start(pk): job = Job.objects.get(pk=pk) twitter = Twitter(job.screen_name) job.total_followers = twitter.total_followers job.save() for user in twitter.followers: check_account.delay(user.screen_name, job.pk)
def refresh_tweetstream(tweetstream_key): log.info("refreshing tweetstream %s" % tweetstream_key) # the stream to update stream = twitter.TweetStream.get_by_urlsafe(tweetstream_key) # refresh the query s = Twitter.search(qs=stream.stream_query) # based on the query string s.fetch(process_tweets, {"stream-key": tweetstream_key})
def refresh_tweetstream( tweetstream_key ): log.info("refreshing tweetstream %s" % tweetstream_key ) # the stream to update stream = twitter.TweetStream.get_by_urlsafe(tweetstream_key) # refresh the query s = Twitter.search( qs = stream.stream_query ) # based on the query string s.fetch(process_tweets, {"stream-key":tweetstream_key})
def get_tweets(query, result_type, max_tweets): # Search the Twitter API for the given title twitter_client = Twitter(CONFIG['twitter']) tweet_data = twitter_client.search("\"%s\" movie -download -stream -#nw -#nowwatching -RT" % (query,), result_type=result_type, count=max_tweets) if DEBUG: print "Retrieved %d tweets (with result type %s)." % (len(tweet_data), result_type) # if DEBUG: # print json.dumps(tweet_data[0], sort_keys=True, indent=4, separators=(',', ': ')) # Build simple Twitter objects tweets = map(Tweet, tweet_data) # Add a filtered version of the tweet text pattern = re.compile(query.replace(" ", " ?"), re.I) for tweet in tweets: tweet.sentiment = get_cached_tweet_sentiment(tweet.id) tweet.filtered_text = pattern.sub('', tweet.text) return tweets
def start(pk): job = Job.objects.get(pk=pk) try: twitter = Twitter(job.screen_name) except RateLimitError: return restart(job) job.total_followers = twitter.total_followers job.save() try: for user in twitter.followers: check_account.delay(user.screen_name, job.pk) except RateLimitError: return restart(job)
import os.path import sys import yaml dir_path = os.path.abspath(os.path.dirname(__file__)) sys.path.append(os.path.join(dir_path, "../../")) conf_path = path = os.path.join(dir_path, "../tweets.yaml") from lib.twitter import Twitter if __name__ == '__main__': with open(conf_path) as f: config_map = yaml.safe_load(f) for key, value in config_map['auth'].items(): print("Deleting {} tweets".format(key)) twitter = Twitter(**value) twitter.clean_up() print("Delete completed for {} tweets".format(key))
def collect_image_urls(apps, _schema_editor): for job in apps.get_model("core", "Job").objects.filter(image=""): job.image = Twitter(job.screen_name).image job.save()