def test_connected_status_after_exception_is_false(self): """ When an exception is thrown the client is no longer connected. """ test_utils.generate_test_config_file() client = PowerTrackClient(_dummy_callback, config_file_path=config_file) client.connect() sleep(2) self.assertFalse(client.connected())
def test_exception_with_callback(self): """ When exception_callback is provided, worker uses it to communicate errors. """ test_utils.generate_test_config_file() exception_callback = mock.Mock() client = PowerTrackClient(_dummy_callback, exception_callback, config_file_path=config_file) client.connect() self.assertTrue(exception_callback.called) actual_exinfo = exception_callback.call_args[0][0] actual_ex = actual_exinfo[1] self.assertIsInstance(actual_ex, Exception) self.assertEqual(actual_ex.message, "This is a test exception")
def test_no_args(self): """ Check if a ~/.gnippy file is present and run a no-arg test. """ possible_paths = test_utils.get_possible_config_locations() for config_path in possible_paths: if os.path.isfile(config_path): client = PowerTrackClient(_dummy_callback) self.assertIsNotNone(client.auth) self.assertIsNotNone(client.url) self.assertTrue("http" in client.url and "://" in client.url)
def test_constructor_only_file(self): """ Initialize PowerTrackClient with only a config file path. """ test_utils.generate_test_config_file() client = PowerTrackClient(_dummy_callback, config_file_path=config_file) expected_auth = (test_utils.test_username, test_utils.test_password) expected_url = test_utils.test_powertrack_url self.assertEqual(expected_auth[0], client.auth[0]) self.assertEqual(expected_auth[1], client.auth[1]) self.assertEqual(expected_url, client.url)
def test_backfill_value_appended_to_url(self, mocked_requests_get): """When passing a backfill value it is appended to the url call to GNIP""" backfill_minutes = 3 expected_powertrack_url = "{0}?backfillMinutes={1}".format( test_utils.test_powertrack_url, backfill_minutes) test_utils.generate_test_config_file() client = PowerTrackClient(_dummy_callback, config_file_path=config_file) client.connect(backfill_minutes=backfill_minutes) client.disconnect() mocked_requests_get.assert_called_with( expected_powertrack_url, auth=(test_utils.test_username, test_utils.test_password), stream=True )
def test_backfill_value_greater_than_five_raises_exception(self): """When passing a backfill value that isn't an integer raises an exception""" backfill_minutes = 12 test_utils.generate_test_config_file() client = PowerTrackClient(_dummy_callback, config_file_path=config_file) self.assertRaises(AssertionError, client.connect, backfill_minutes)
def test_connected_status_true_when_running(self): """ Once the client connect method is called the client reports as connected. """ test_utils.generate_test_config_file() client = PowerTrackClient(_dummy_callback, config_file_path=config_file) client.connect() self.assertTrue(client.connected()) client.disconnect() self.assertFalse(client.connected())
def test_constructor_all_args(self): """ Initialize PowerTrackClient with all args. """ test_utils.generate_test_config_file() expected_auth = ("hello", "world") expected_url = "http://wat.com/testing.json" client = PowerTrackClient(_dummy_callback, auth=expected_auth, url=expected_url, config_file_path=config_file) self.assertEqual(expected_auth[0], client.auth[0]) self.assertEqual(expected_auth[1], client.auth[1]) self.assertEqual(expected_url, client.url)
def test_constructor_only_auth(self): """ Initialize PowerTrackClient with only a config the auth tuple. The config file is provided for testability. """ test_utils.generate_test_config_file_with_only_powertrack() expected_auth = (test_utils.test_username, test_utils.test_password) expected_url = test_utils.test_powertrack_url client = PowerTrackClient(_dummy_callback, auth=expected_auth, config_file_path=config_file) self.assertEqual(expected_auth[0], client.auth[0]) self.assertEqual(expected_auth[1], client.auth[1]) self.assertEqual(expected_url, client.url)
def test_backfill_value_appended_to_url(self, mocked_requests_get): """When passing a backfill value it is appended to the url call to GNIP""" backfill_minutes = 3 expected_powertrack_url = "{0}?backfillMinutes={1}".format( test_utils.test_powertrack_url, backfill_minutes) test_utils.generate_test_config_file() client = PowerTrackClient(_dummy_callback, config_file_path=config_file) client.connect(backfill_minutes=backfill_minutes) client.disconnect() mocked_requests_get.assert_called_with(expected_powertrack_url, auth=(test_utils.test_username, test_utils.test_password), stream=True)
#!/usr/bin/env python import time from gnippy import PowerTrackClient # Define a callback def callback(activity): print activity # Create the client client = PowerTrackClient(callback,url="https://stream.gnip.com:443/accounts/{ACCOUNT_NAME}/publishers/twitter/streams/track/{STREAM_LABEL}.json",auth=("*****@*****.**","nOtMyPaSsWoRd")) print "Connecting..." client.connect() time.sleep(1800) #run for 30 minutes... client.disconnect() #... then disconnet. print "finished streaming, exiting..."
from gnippy import PowerTrackClient import json import subprocess #calls Tweeting app. # Define a callback def callback(activity): print(activity) #to standard out. tweet_hash = json.loads(activity) user = tweet_hash['user']['screen_name'] #Let do some trigger-tweet to response-tweet latency. created_time_str = tweet_hash['created_at'] time_created = datetime.datetime.strptime(created_time_str,'%a %b %d %H:%M:%S +0000 %Y') time_now = datetime.datetime.utcnow() seconds = (time_now - time_created).total_seconds() #Details for calling separate Tweeting app. app_to_call = 'ruby' post_tweet_script = 'twitter_post.rb' tweet_args = '@' + user + ' thanks for Tweeting! Responding (in ' + "{0:.2f}".format(seconds) + ' seconds) from a Raspberry Pi 3 running a Ruby-based Tweeting app.' result = subprocess.check_output([app_to_call, post_tweet_script, tweet_args], cwd='./') # Create the client client = PowerTrackClient(callback, url="https://{my.gnip.stream.json", auth=("*****@*****.**", "mYPaSsWoRd")) client.connect() while True: x = 1
#!/usr/bin/env python import time from gnippy import PowerTrackClient # Define a callback def callback(activity): print activity # Create the client client = PowerTrackClient( callback, url= "https://stream.gnip.com:443/accounts/{ACCOUNT_NAME}/publishers/twitter/streams/track/{STREAM_LABEL}.json", auth=("*****@*****.**", "nOtMyPaSsWoRd")) print "Connecting..." client.connect() time.sleep(1800) #run for 30 minutes... client.disconnect() #... then disconnet. print "finished streaming, exiting..."
''' # [Credentials] # username = [email protected] # password = limpiezaexcelente # # [PowerTrack] # url = https://gnip-stream.twitter.com/stream/powertrack/accounts/Bproces/publishers/twitter/prod.json # rules_url = https://gnip-stream.twitter.com/stream/powertrack/accounts/Bproces/publishers/twitter/rules.json #!/usr/bin/env python import time from gnippy import PowerTrackClient # Define a callback def callback(activity): print(activity) # Create the client client = PowerTrackClient( callback, url= "https://gnip-stream.twitter.com/stream/powertrack/accounts/Bproces/publishers/twitter/prod.json", auth=("*****@*****.**", "limpiezaexcelente")) client.connect() # Wait for 2 minutes and then disconnect time.sleep(120) client.disconnect()