def testInit(self): '''Test the twitter.Status constructor''' status = twitter.Status( created_at='Fri Jan 26 23:17:14 +0000 2007', id=4391023, text=u'A légpárnás hajóm tele van angolnákkal.', user=self._GetSampleUser())
def test_check_delete_after_date(mocker): mocker.patch("tweet_delete.deleter.Deleter.delete") import datetime d = Deleter( "Mq0PdSJPMQwJwpMm3RtQKGkWA", "kWPpBJvSk7gW59J59WxoWdy5yeA7T6Jr6OJ4yOwxta9I4qtjjG", "959446912159158273-4sLsH3PpTRh93f733s7EZLmLGL4haAD", "98lOut16loWFuHn2uADQfUxP8F4Oxsa3wq6HpdDtbsMbH", datetime.timedelta(seconds=1), dateutil.parser.parse("2008-09-03T20:56:35.450686Z").replace( tzinfo=None), 5, False, ) status = twitter.Status( id=1, favorite_count=1, retweet_count=1, created_at="Wed Mar 13 15:16:59 +0000 2019", ) assert d.to_be_deleted(status) == True assert d.delete.called
def main(): api = twitter.Api() previous_status = twitter.Status() raspberry_pi_farm.init() chicken = raspberry_pi_farm.Chicken() # what are we searching for? if len(sys.argv) > 1: search_term = sys.argv[1] else: search_term = DEFAULT_SEARCH_TERM print "Listening to tweets containing the word '%s'." % search_term while True: # grab the first tweet containing the search_term current_status = api.GetSearch(term=search_term, per_page=1)[0] # if the status is different then give it to the chicken if current_status.id != previous_status.id: chicken.start_wobble() chicken.say(current_status.text) chicken.stop_wobble() previous_status = current_status # wait for a short while before checking again time.sleep(TIME_DELAY)
def main(): api = twitter.Api() previous_status = twitter.Status() raspberry_pi_farm.init() chicken = raspberry_pi_farm.Chicken() # who are we listening to? if len(sys.argv) > 1: user = sys.argv[1] else: user = DEFAULT_USER print "Listening to tweets from '%s'." % user while True: # grab the users current status current_status = api.GetUser(user).status # if the status is different then give it to the chicken if current_status.id != previous_status.id: chicken.start_wobble() chicken.say(current_status.text) chicken.stop_wobble() previous_status = current_status # wait for a short while before checking again time.sleep(TIME_DELAY)
def testEq(self): '''Test the twitter.Status __eq__ method''' status = twitter.Status() status.created_at = 'Fri Jan 26 23:17:14 +0000 2007' status.id = 4391023 status.text = u'A légpárnás hajóm tele van angolnákkal.' status.user = self._GetSampleUser() self.assertEqual(status, self._GetSampleStatus())
def test_constructed_status(self, text): s = twitter.Status() s.text = text s.created_at = "016-02-13T23:00:00" s.screen_name = "himawari8bot" s.id = 1 try: s.__repr__() except Exception as e: self.fail(e)
def testProperties(self): '''Test all of the twitter.Status properties''' status = twitter.Status() status.id = 1 self.assertEqual(1, status.id) created_at = calendar.timegm((2007, 1, 26, 23, 17, 14, -1, -1, -1)) status.created_at = 'Fri Jan 26 23:17:14 +0000 2007' self.assertEqual('Fri Jan 26 23:17:14 +0000 2007', status.created_at) self.assertEqual(created_at, status.created_at_in_seconds) status.user = self._GetSampleUser() self.assertEqual(718443, status.user.id)
def test_schedule_delete(mocker, check_fixture_cm): mocker.patch("tweet_delete.deleter.Deleter.delete") import datetime d = Deleter( "Mq0PdSJPMQwJwpMm3RtQKGkWA", "kWPpBJvSk7gW59J59WxoWdy5yeA7T6Jr6OJ4yOwxta9I4qtjjG", "959446912159158273-4sLsH3PpTRh93f733s7EZLmLGL4haAD", "98lOut16loWFuHn2uADQfUxP8F4Oxsa3wq6HpdDtbsMbH", datetime.timedelta(seconds=10), dateutil.parser.parse("2008-09-03T20:56:35.450686Z").replace( tzinfo=None), 5, False, ) statuses = [] for i in range(100, 90, -1): statuses.append( twitter.Status( id=i, favorite_count=1, retweet_count=1, created_at=datetime.datetime.utcnow().isoformat(), )) with check_fixture_cm(statuses) as mock: mock.side_effect = [statuses, []] max_id = d.check_for_tweets() mock.assert_has_calls([ call( d.api, include_rts=True, exclude_replies=False, max_id=None, count=200, since_id=None, ), call( d.api, include_rts=True, exclude_replies=False, max_id=91, count=200, since_id=None, ), ]) assert len(mock.call_args_list) == 2 assert max_id == 100 for s in statuses: assert s.id in d.ids_scheduled_for_deletion
def test_check_dont_delete_after_date(mocker): mocker.patch('tweet_delete.deleter.Deleter.delete') import datetime d = Deleter('Mq0PdSJPMQwJwpMm3RtQKGkWA', 'kWPpBJvSk7gW59J59WxoWdy5yeA7T6Jr6OJ4yOwxta9I4qtjjG', '959446912159158273-4sLsH3PpTRh93f733s7EZLmLGL4haAD', '98lOut16loWFuHn2uADQfUxP8F4Oxsa3wq6HpdDtbsMbH', datetime.timedelta(seconds=1), datetime.datetime.utcnow(), 5) status = twitter.Status(id=1, favorite_count=1, retweet_count=1, created_at="Wed Mar 13 15:16:59 +0000 2019") assert d.to_be_deleted(status) == False assert not d.delete.called
def testGettersAndSetters(self): '''Test all of the twitter.Status getters and setters''' status = twitter.Status() status.SetId(4391023) self.assertEqual(4391023, status.GetId()) created_at = calendar.timegm((2007, 1, 26, 23, 17, 14, -1, -1, -1)) status.SetCreatedAt('Fri Jan 26 23:17:14 +0000 2007') self.assertEqual('Fri Jan 26 23:17:14 +0000 2007', status.GetCreatedAt()) self.assertEqual(created_at, status.GetCreatedAtInSeconds()) status.SetNow(created_at + 10) self.assertEqual("about 10 seconds ago", status.GetRelativeCreatedAt()) status.SetText(u'A légpárnás hajóm tele van angolnákkal.') self.assertEqual(u'A légpárnás hajóm tele van angolnákkal.', status.GetText()) status.SetUser(self._GetSampleUser()) self.assertEqual(718443, status.GetUser().id)
def get_premium_all_tweets(self): rule_str = "from:" + self.twitter_user.screen_name print("get_all_twitter_user_tweets: rule_str: " + rule_str) rule = gen_rule_payload(rule_str) tweets_paresd = [] try: tweets = collect_results(rule, max_results=100, result_stream_args=search_args) print("tweets len:" + str(len(tweets))) for t in tweets: tweets_paresd.append(twitter.Status().NewFromJsonDict(t)) print("tweets_paresd len:" + str(len(tweets_paresd))) self.place.add_tweet_list(tweets_paresd, self.user, self.region, old_user=True) except Exception as exc: print("In get_all_twitter_user_tweets, Problem loading tweets") print(exc) return tweets_paresd
def generate_tweet_sendaway(t): cordinates = "0" if isinstance(t, Tweet): t = twitter.Status().NewFromJsonDict(t) if t.coordinates is not None: cordinates = str(t.coordinates['coordinates']) place = "none" if t.place is not None: place = str(t.place['full_name']) location = "none" if t.user.location is not None: location = str(t.user.location) list = [ str(t.id), str(t.text), str(t.user.screen_name), location, cordinates, place, str(t.created_at) ] return list
def get(self): config = get_config() # Tweets count = Tweet.all().count(1000) page = int(self.request.get('page', 1)) prev_page = page - 1 next_page = page + 1 show_prev_page = (page > 1) show_next_page = (page * 20 < count) tweets = [] for tweet in Tweet.all().order('-id').fetch(20, (page - 1) * 20): tweet.status = twitter.Status(id=tweet.id, created_at=tweet.created_at) tweet.text = unescape(tweet.text) tweet.source = unescape(tweet.source) tweets.append(tweet) # User cloud cloud_tweets = Tweet.all().order('-id').fetch(300) cloud_items = {} cloud = [] max = 0 for tw in cloud_tweets: cloud_items[tw.from_user] = 0 for tw in cloud_tweets: cloud_items[tw.from_user] = cloud_items[tw.from_user] + 1 if cloud_items[tw.from_user] > max: max = cloud_items[tw.from_user] for k in cloud_items: cloud.append({ 'name': k, 'lower_name': k.lower(), 'count': cloud_items[k], 'html': '<a href="http://twitter.com/%s" style="font-size:%spx">%s</a>' % (k, (9 + 16 * (1.0 * cloud_items[k] / max)), k) }) path = os.path.join(os.path.dirname(__file__), 'index.html') self.response.out.write(template.render(path, locals()))
def favorite_action(request): result = False if request.COOKIES.has_key("newsrivr_userid_md5"): userid = request.COOKIES["newsrivr_userid_md5"] if userid!="None": user = getUserByMD5(request.COOKIES["newsrivr_userid_md5"]) if user: data = request.POST access_token = dict(urlparse.parse_qsl(user["access_token"])) api = twitter.Api(consumer_key, consumer_secret, access_token["oauth_token"], access_token["oauth_token_secret"]) if data.has_key("id_str"): s = twitter.Status(id=data["id_str"]) if data["action"]=="destroy": try: api.DestroyFavorite(s) except twitter.TwitterError, e: d = getCollDrops().find_one({"retweet_id_str":data["id_str"]}) retweet = False if d: if "favorites" in d: if user["id_str"] in d["favorites"]: retweet = True if not d or not retweet: d = getCollDrops().find_one({"id_str":data["id_str"]}) if d: if "favorites" in d: if int(user["id_str"]) in d["favorites"]: d["favorites"].remove(int(user["id_str"])) favs = [] for i in d["favorites"]: favs.append(int(i)) d["favorites"] = list(set(favs)) clog("destroy favorite") getCollDrops().save(d, safe=True) elif data["action"]=="create": clog(s) try: api.CreateFavorite(s) except twitter.TwitterError, e: clog(e) api.DestroyFavorite(s) api.CreateFavorite(s)
def update_last_update(self): status = twitter.Status( text="Last updated: %s" % (time.strftime("%c")), created_at=rfc822.formatdate(), id='%s:last_update' % self.call) status.sender_screen_name = "Khweeteur" try: # Open the file exclusively. filename = os.path.join(self.getCacheFolder(), "last_update") with open(filename, 'wb') as fhandle: pickle.dump(status, fhandle, pickle.HIGHEST_PROTOCOL) logging.debug("LAST_UPDATE: Wrote %s: %s" % (filename, status.text)) except pickle.PickleError, exception: logging.debug('Serialization of %s failed: %s' % (filename, str (exception))) # Remove the empty file. os.remove(filename)
def _post_tweet(self, tweet): # Checking can_post is the responsibility of the caller. try: if self.test: try: last_id = self.db.posted_tweets[-1].id except IndexError: last_id = 0 status = twitter.Status( id=last_id + 1, full_text=tweet.text, in_reply_to_status_id=tweet.in_reply_to_status_id) else: status = self.api.PostUpdate( tweet.text, in_reply_to_status_id=tweet.in_reply_to_status_id) except (twitter.TwitterError, ConnectionError) as e: logger.error(f"Twitter raised error for {tweet}: {e}") else: # Logging the request here, since I guess it counts towards # the rate limit regardless of whether we succeed or not self._set_post_status_limit(subtract=1) tweet.extend(status) self.db.posted_tweets.append(tweet) if tweet.in_reply_to_status_id: # This was a reply to a mention original_tweet = self.db.mentions.get_by_id( tweet.in_reply_to_status_id) if original_tweet: original_tweet.is_answered = True logger.info(f"Posted: {tweet} as reply to: {original_tweet}") else: logger.info(f"Posted: {tweet}") if not tweet.in_reply_to_status_id and self.generate_random_lock.locked( ): # This was a random tweet, so release lock (regardless of success) logger.debug("Releasing generate_random_lock") self.generate_random_lock.release()
def origin_prem_tweet_collector(self, user, region, word_list=None): toDate = (datetime.today() - timedelta(days=7)).date().isoformat() fromDate = self.last_tweet_date.date().isoformat() if fromDate > toDate: return rule = gen_rule_payload("point_radius:[" + str(self.longitude) + " " + str(self.latitude)+ " " + str(self.radius) + "km] lang:" + self.language , to_date=toDate, from_date=fromDate , results_per_call=100) try: tweets = collect_results(rule, max_results=1000000, result_stream_args=search_args) except Exception as exc: print("In origin_prem_tweet_collector:") print(exc) return tweets_paresd = [] for t in tweets: tweets_paresd.append(twitter.Status().NewFromJsonDict(t)) self.add_tweet_list(tweets_paresd, user, region) self.collector = 0 user.save_me_to_db() print("finish - origin_prem_tweet_collector")
def testRelativeCreatedAt(self): '''Test various permutations of Status relative_created_at''' status = twitter.Status(created_at='Fri Jan 01 12:00:00 +0000 2007') status.now = self._ParseDate('Jan 01 12:00:00 2007') self.assertEqual('about a second ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:00:01 2007') self.assertEqual('about a second ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:00:02 2007') self.assertEqual('about 2 seconds ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:00:05 2007') self.assertEqual('about 5 seconds ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:00:50 2007') self.assertEqual('about a minute ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:01:00 2007') self.assertEqual('about a minute ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:01:10 2007') self.assertEqual('about a minute ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:02:00 2007') self.assertEqual('about 2 minutes ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:31:50 2007') self.assertEqual('about 31 minutes ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 12:50:00 2007') self.assertEqual('about an hour ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 13:00:00 2007') self.assertEqual('about an hour ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 13:10:00 2007') self.assertEqual('about an hour ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 14:00:00 2007') self.assertEqual('about 2 hours ago', status.relative_created_at) status.now = self._ParseDate('Jan 01 19:00:00 2007') self.assertEqual('about 7 hours ago', status.relative_created_at) status.now = self._ParseDate('Jan 02 11:30:00 2007') self.assertEqual('about a day ago', status.relative_created_at) status.now = self._ParseDate('Jan 04 12:00:00 2007') self.assertEqual('about 3 days ago', status.relative_created_at) status.now = self._ParseDate('Feb 04 12:00:00 2007') self.assertEqual('about 34 days ago', status.relative_created_at)
def test_check_low_quality_tweets2(mocker): mocker.patch("tweet_delete.deleter.Deleter.delete") import datetime d = Deleter( "Mq0PdSJPMQwJwpMm3RtQKGkWA", "kWPpBJvSk7gW59J59WxoWdy5yeA7T6Jr6OJ4yOwxta9I4qtjjG", "959446912159158273-4sLsH3PpTRh93f733s7EZLmLGL4haAD", "98lOut16loWFuHn2uADQfUxP8F4Oxsa3wq6HpdDtbsMbH", datetime.timedelta(seconds=1), None, 5, False, ) status = twitter.Status( id=1, favorite_count=100, retweet_count=100, created_at="Wed Mar 13 15:16:59 +0000 2019", ) assert d.to_be_deleted(status) == False assert not d.delete.called
def _GetSampleStatus(self): return twitter.Status(created_at='Fri Jan 26 23:17:14 +0000 2007', id=4391023, text=u'A légpárnás hajóm tele van angolnákkal.', user=self._GetSampleUser())
def _GetSampleStatus(self): return twitter.Status( created_at='Fri Jan 26 17:28:19 +0000 2007', id=4212713, text='"Select all" and archive your Gmail inbox. ' ' The page loads so much faster!')
def mongo_tweet_to_status(tweet): status = twitter.Status().NewFromJsonDict(tweet) return status
def test_check_for_tweets2(mocker, check_fixture_cm): mocker.patch("tweet_delete.deleter.Deleter.delete") import datetime d = Deleter( "Mq0PdSJPMQwJwpMm3RtQKGkWA", "kWPpBJvSk7gW59J59WxoWdy5yeA7T6Jr6OJ4yOwxta9I4qtjjG", "959446912159158273-4sLsH3PpTRh93f733s7EZLmLGL4haAD", "98lOut16loWFuHn2uADQfUxP8F4Oxsa3wq6HpdDtbsMbH", datetime.timedelta(seconds=1), dateutil.parser.parse("2008-09-03T20:56:35.450686Z").replace( tzinfo=None), 5, False, ) statuses1 = [] for i in range(100, 90, -1): statuses1.append( twitter.Status( id=i, favorite_count=1, retweet_count=1, created_at="Wed Mar 13 15:16:59 +0000 2019", )) with check_fixture_cm(statuses1) as mock: mock.side_effect = [statuses1, []] max_id = d.check_for_tweets() mock.assert_has_calls([ call( d.api, include_rts=True, exclude_replies=False, max_id=None, count=200, since_id=None, ), call( d.api, include_rts=True, exclude_replies=False, max_id=91, count=200, since_id=None, ), ]) assert len(mock.call_args_list) == 2 assert max_id == 100 calls = [call(s) for s in statuses1] d.delete.assert_has_calls(calls) d.delete.reset_mock() statuses2 = [] for i in range(110, 100, -1): statuses2.append( twitter.Status( id=i, favorite_count=1, retweet_count=1, created_at="Wed Mar 13 15:16:59 +0000 2019", )) with check_fixture_cm(statuses2) as mock: mock.side_effect = [statuses2, statuses1, []] max_id = d.check_for_tweets(max_id) mock.assert_has_calls([ call( d.api, include_rts=True, exclude_replies=False, max_id=None, count=200, since_id=100, ) ]) assert len(mock.call_args_list) == 3 assert max_id == 110 calls = [call(s) for s in statuses2] d.delete.assert_has_calls(calls) d.delete.reset_mock()
def get_status(cls, **kwargs): return twitter.Status(**kwargs)