def __init__(self, webfinger, alias=None, app=None): self.app = app if not webfinger: self.webfinger = raw_input( 'Enter webfinger ([email protected]) for your %s account: ' % alias) else: self.webfinger = webfinger self.cfg = self.app.cfg.get(self.webfinger, {}) client = Client(webfinger=self.webfinger, type='native', name=self.app.name, key=self.cfg.get('key'), secret=self.cfg.get('secret')) self.pump = PyPump(client=client, verifier_callback=self.verifier, token=self.cfg.get('token'), secret=self.cfg.get('token_secret')) self.key, self.secret, self.expiry = self.pump.get_registration() self.token, self.token_secret = self.pump.get_token() self.write_config() self.get_following() self.backup_following() self.app.say('%s: account ready (following %s contacts)\n----' % (self.webfinger, len(self.following)))
def pump_login(self): print 'Logging into the Pump server...' username = self._parser.get('pump', 'username') key = self._parser.get('pump', 'key') secret = self._parser.get('pump', 'secret') token = self._parser.get('pump', 'token') token_secret = self._parser.get('pump', 'token_secret') client = Client(webfinger=username, name="Pump.io", type="native", key=key, secret=secret) pump = PyPump(client=client, verifier_callback=simple_verifier) pump.store["oauth-access-token"] = token pump.store["oauth-access-secret"] = token_secret me = pump.Person(username) self._username = username self._pump = pump self._me = me
def pump_login(self): print 'Logging into the Pump server...' username = self._parser.get('pump', 'username') key = self._parser.get('pump', 'key') secret = self._parser.get('pump', 'secret') token = self._parser.get('pump', 'token') token_secret = self._parser.get('pump', 'token_secret') client = Client( webfinger = username, name = "Pump.io", type = "native", key = key, secret = secret) pump = PyPump( client = client, verifier_callback = simple_verifier) pump.store["oauth-access-token"] = token pump.store["oauth-access-secret"] = token_secret me = pump.Person(username) self._username = username self._pump = pump self._me = me
def __init__(self, site, conf, bearer_token=None, get_token=False, streaming=False, upload=False): self.site = site.lower() self.conf = conf[site.upper()] # Identi.ca service only supported for commands "ping" and "microblog" if self.site == "identica": self.domain = "identi.ca" # New Pump.io Identi.ca connection: self.user = "******" % (self.conf['USER'].lower(), self.domain) from gazouilleur.identica_auth_config import identica_auth self.conf.update(identica_auth[self.conf['USER'].lower()]) iclient = Client(webfinger=self.user, type="native", name="Gazouilleur", key=self.conf['key'], secret=self.conf['secret']) self.conn = PyPump(client=iclient, token=self.conf['token'], secret=self.conf['token_secret'], verifier_callback=lambda: "") elif self.site == "twitter": self.domain = "api.twitter.com" self.user = self.conf['USER'] self.post = 'FORBID_POST' not in conf['TWITTER'] or str( conf['TWITTER']['FORBID_POST']).lower() != "true" args = {"api_version": self.twitter_api_version, "secure": True} format = "json" if get_token: format = "" args["api_version"] = None args["auth"] = OAuth2(self.conf['KEY'], self.conf['SECRET']) elif bearer_token: args["auth"] = OAuth2(bearer_token=bearer_token) else: args["auth"] = OAuth(self.conf['OAUTH_TOKEN'], self.conf['OAUTH_SECRET'], self.conf['KEY'], self.conf['SECRET']) if streaming: self.domain = "stream.twitter.com" args['block'] = False args['timeout'] = 10 conn = TwitterStream else: if upload: self.domain = "upload.twitter.com" args['format'] = format conn = Twitter args['domain'] = self.domain self.conn = conn(**args)
def __init__(self): """ Init app and log in to our pump.io account """ # Set up argparse self.parser = argparse.ArgumentParser( description='Post a note to the pump.io network') self.parser.add_argument('-u', '--user', dest='webfinger', required=True, help='*****@*****.**') self.parser.add_argument('-t', '--title', dest='note_title', default=None, help='Note title') self.parser.add_argument('-n', '--note', dest='note_content', required=True, help='Note content') self.args = self.parser.parse_args() self.read_config() # Try to login to our pump.io account using account credentials # from config, if our webfinger is not found in config we will # have to authorize the app with our account. webfinger = self.args.webfinger client = Client( webfinger=webfinger, type='native', name=self.client_name, key=self.config.get(webfinger, {}).get('key'), secret=self.config.get(webfinger, {}).get('secret'), ) self.pump = PyPump( client=client, token=self.config.get(webfinger, {}).get('token'), secret=self.config.get(webfinger, {}).get('token_secret'), verifier_callback=self.verifier, ) # Add account credentials to config in case we didnt have it already self.config[webfinger] = { 'key': self.pump.get_registration()[0], 'secret': self.pump.get_registration()[1], 'token': self.pump.get_token()[0], 'token_secret': self.pump.get_token()[1], } self.write_config()
def __init__(self): """ Init app and log in to our pump.io account """ # Set up argparse self.parser = argparse.ArgumentParser(description='Post a note to the pump.io network') self.parser.add_argument('-u', '--user', dest='webfinger', required=True, help='*****@*****.**') self.parser.add_argument('-t', '--title', dest='note_title', default=None, help='Note title') self.parser.add_argument('-n', '--note', dest='note_content', required=True, help='Note content') self.args = self.parser.parse_args() self.read_config() # Try to login to our pump.io account using account credentials # from config, if our webfinger is not found in config we will # have to authorize the app with our account. webfinger = self.args.webfinger self.pump = PyPump( webfinger, client_name = self.client_name, **self.config.get(webfinger, {}) ) # Add account credentials to config in case we didnt have it already self.config[webfinger] = { 'key' : self.pump.get_registration()[0], 'secret' : self.pump.get_registration()[1], 'token' : self.pump.get_token()[0], 'token_secret' : self.pump.get_token()[1], } self.write_config()
def __init__(self, webfinger, alias=None, app=None): self.app = app if not webfinger: self.webfinger = raw_input('Enter webfinger ([email protected]) for your %s account: ' % alias) else: self.webfinger = webfinger self.cfg = self.app.cfg.get(self.webfinger, {}) client=Client( webfinger=self.webfinger, type='native', name=self.app.name, key=self.cfg.get('key'), secret=self.cfg.get('secret') ) self.pump = PyPump( client=client, verifier_callback=self.verifier, token=self.cfg.get('token'), secret=self.cfg.get('token_secret') ) self.key, self.secret, self.expiry = self.pump.get_registration() self.token, self.token_secret = self.pump.get_token() self.write_config() self.get_following() self.backup_following() self.app.say('%s: account ready (following %s contacts)\n----' % (self.webfinger, len(self.following)))
def setup(self, message): """ Sets up pypump object """ try: webfinger = message.body["webfinger"] self.pump = PyPump( webfinger, client_name="Muon", client_type="native", key=message.body["key"], secret=message.body["secret"], token=message.body["token"], token_secret=message.body["token_secret"] ) webfinger = message.body["webfinger"] except KeyError: webfinger = raw_input("Webfinger: ").lstrip(" ").rstrip(" ") self.pump = PyPump( webfinger, client_name="Muon", client_type="native" ) config = { "webfinger": webfinger, "key": self.pump.get_registration()[0], "secret": self.pump.get_registration()[1], "token": self.pump.get_token()[0], "token_secret": self.pump.get_token()[1], } self.hive.send_message( to="muon", directive="write_config", body=config ) self.me = self.pump.Person(webfinger) self.send_message( to="muon", directive="start_gui" )
def test_https_failover(self, requests_mock): store = mock.MagicMock() store.__iter__.return_value = [] client = mock.Mock() verifier = mock.Mock() requests_mock.post.return_value.text = "%s=thingy&%s=secretthingy" % (PyPump.PARAM_TOKEN, PyPump.PARAM_TOKEN_SECRET) # re-add exceptions to mocked library requests_mock.exceptions = request_excs pump = PyPump(client, verifier, store=store) self.assertEqual(pump.protocol, "https") # verify == True fnc_mock = mock.Mock() fnc_mock.side_effect = request_excs.ConnectionError with self.assertRaises(request_excs.ConnectionError): pump._requester(fnc_mock, "") self.assertEqual(len(fnc_mock.call_args_list), 1) self.assertTrue(fnc_mock.call_args_list[0][0][0].startswith("https://")) self.assertEqual(pump.protocol, "https") # verify == False fnc_mock.reset_mock() pump.verify_requests = False with self.assertRaises(request_excs.ConnectionError): pump._requester(fnc_mock, "") self.assertEqual(len(fnc_mock.call_args_list), 2) self.assertTrue(fnc_mock.call_args_list[0][0][0].startswith("https://")) self.assertTrue(fnc_mock.call_args_list[1][0][0].startswith("http://")) # make sure that we're reset to https after self.assertEqual(pump.protocol, "https")
def pump_login(self): print 'Logging into the Pump server...' username = self._parser.get('pump', 'username') client = Client(webfinger=username, name="Pump.io", type="native") try: pump = PyPump(client=client, verifier_callback=simple_verifier) except ConnectionError, e: domain = username.split('@')[1] print 'Error: Unable to connect to ' + domain + '.' print e sys.exit()
def pump_login(self): print('Logging into the Pump server...') username = self._parser.get('pump', 'username') client = Client(webfinger=username, name="Pump.io", type="native") try: pump = PyPump(client=client, verifier_callback=simple_verifier) except ConnectionError as e: domain = username.split('@')[1] print('Error: Unable to connect to ' + domain + '.') print(e) sys.exit() except ClientException: domain = username.split('@')[1] print('Error: Pump server not found at ' + domain + '.') sys.exit() me = pump.Person(username) self._username = username self._pump = pump self._me = me
def CreatePumpClient(self, webfinger, client_credentials, client_tokens): client = Client( webfinger=self.webfinger, type="native", name="NavierStokes", key=client_credentials[0], secret=client_credentials[1], ) pump = PyPump( client=client, token=client_tokens[0], # the token secret=client_tokens[1], # the token secret verifier_callback=self.simple_verifier) return pump
def test_https_failover(self, requests_mock): store = mock.MagicMock() store.__iter__.return_value = [] client = mock.Mock() verifier = mock.Mock() requests_mock.post.return_value.text = "%s=thingy&%s=secretthingy" % ( PyPump.PARAM_TOKEN, PyPump.PARAM_TOKEN_SECRET) # re-add exceptions to mocked library requests_mock.exceptions = request_excs pump = PyPump(client, verifier, store=store) self.assertEqual(pump.protocol, "https") # verify == True fnc_mock = mock.Mock() fnc_mock.side_effect = request_excs.ConnectionError with self.assertRaises(request_excs.ConnectionError): pump._requester(fnc_mock, "") self.assertEqual(len(fnc_mock.call_args_list), 1) self.assertTrue( fnc_mock.call_args_list[0][0][0].startswith("https://")) self.assertEqual(pump.protocol, "https") # verify == False fnc_mock.reset_mock() pump.verify_requests = False with self.assertRaises(request_excs.ConnectionError): pump._requester(fnc_mock, "") self.assertEqual(len(fnc_mock.call_args_list), 2) self.assertTrue( fnc_mock.call_args_list[0][0][0].startswith("https://")) self.assertTrue(fnc_mock.call_args_list[1][0][0].startswith("http://")) # make sure that we're reset to https after self.assertEqual(pump.protocol, "https")
class Account(object): app = None webfinger = None key = None secret = None token = None token_secret = None following = None cfg = None def __init__(self, webfinger, alias=None, app=None): self.app = app if not webfinger: self.webfinger = raw_input('Enter webfinger ([email protected]) for your %s account: ' % alias) else: self.webfinger = webfinger self.cfg = self.app.cfg.get(self.webfinger, {}) client=Client( webfinger=self.webfinger, type='native', name=self.app.name, key=self.cfg.get('key'), secret=self.cfg.get('secret') ) self.pump = PyPump( client=client, verifier_callback=self.verifier, token=self.cfg.get('token'), secret=self.cfg.get('token_secret') ) self.key, self.secret, self.expiry = self.pump.get_registration() self.token, self.token_secret = self.pump.get_token() self.write_config() self.get_following() self.backup_following() self.app.say('%s: account ready (following %s contacts)\n----' % (self.webfinger, len(self.following))) def verifier(self, url): print("Please open and follow the instructions:") print(url) return raw_input("Verifier: ").strip() def prompt_enter(self, msg): if self.app.parser.args.quiet and self.app.parser.args.noprompt: pass else: print(msg) if not self.app.parser.args.noprompt: try: raw_input("Hit enter to continue or ctrl+c to quit") except KeyboardInterrupt: sys.exit() def write_config(self): self.app.cfg[self.webfinger] = { 'key':self.key, 'secret':self.secret, 'token':self.token, 'token_secret':self.token_secret } self.app.write_config() def backup_following(self): self.app.backup_following(self) def get_following(self): self.app.say("%s: getting contacts (this may take a while)" % self.webfinger) self.following = list(self.pump.me.following) def follow_webfinger(self, webfinger): if self.app.parser.args.dryrun: return True try: obj = { "id":"acct:%s" % webfinger, "objectType":"person", } activity = { "verb":"follow", "object":obj, "to":[obj], } self.pump.me._post_activity(activity, unserialize=False) return True except: return False def unfollow_webfinger(self, webfinger): if self.app.parser.args.dryrun: return True try: obj = { "id":"acct:%s" % webfinger, "objectType":"person", } activity = { "verb":"stop-following", "object":obj, "to":[obj], } self.pump.me._post_activity(activity, unserialize=False) return True except: return False def follow_many(self, webfingers): self.prompt_enter("%s: will now follow %s new contacts" % (self.webfinger, len(webfingers))) for webfinger in webfingers: # we dont want to follow ourselves if webfinger == self.webfinger: self.app.say(" Skipped: %s (your account)" % webfinger) # skip people we are already following elif webfinger in [i.webfinger for i in self.following]: self.app.say(" Skipped: %s (already following)" % webfinger) elif self.follow_webfinger(webfinger): self.app.say(" Followed: %s" % webfinger) else: self.app.say(" Failed: %s" % webfinger) self.get_following() self.app.say("%s: now following %s contacts\n----" % (self.webfinger, len(self.following))) def unfollow_many(self, webfingers): self.prompt_enter("%s: will now unfollow %s contacts" % (self.webfinger, len(webfingers))) for webfinger in webfingers: # skip contacts we are not following if webfinger not in [i.webfinger for i in self.following]: self.app.say(" Skipped %s (not following)" % webfinger) elif self.unfollow_webfinger(webfinger): self.app.say(" Unfollowed: %s" % webfinger) else: self.app.say(" Failed: %s" % webfinger) self.get_following() self.app.say("%s: now following %s contacts\n----" % (self.webfinger, len(self.following)))
class App(object): client_name = 'pypump-post-note' pump = None config_file = os.path.join(os.environ['HOME'],'.config', client_name, 'config.json') config = dict() def __init__(self): """ Init app and log in to our pump.io account """ # Set up argparse self.parser = argparse.ArgumentParser(description='Post a note to the pump.io network') self.parser.add_argument('-u', '--user', dest='webfinger', required=True, help='*****@*****.**') self.parser.add_argument('-t', '--title', dest='note_title', default=None, help='Note title') self.parser.add_argument('-n', '--note', dest='note_content', required=True, help='Note content') self.args = self.parser.parse_args() self.read_config() # Try to login to our pump.io account using account credentials # from config, if our webfinger is not found in config we will # have to authorize the app with our account. webfinger = self.args.webfinger client=Client( webfinger=webfinger, type='native', name=self.client_name, key=self.config.get(webfinger, {}).get('key'), secret=self.config.get(webfinger, {}).get('secret') ) self.pump = PyPump( client=client, token=self.config.get(webfinger, {}).get('token'), secret=self.config.get(webfinger, {}).get('token_secret') ) # Add account credentials to config in case we didnt have it already self.config[webfinger] = { 'key' : self.pump.get_registration()[0], 'secret' : self.pump.get_registration()[1], 'token' : self.pump.get_token()[0], 'token_secret' : self.pump.get_token()[1], } self.write_config() def write_config(self): """ Write config to file """ if not os.path.exists(os.path.dirname(self.config_file)): os.makedirs(os.path.dirname(self.config_file)) with open(self.config_file, 'w') as f: f.write(json.dumps(self.config)) f.close() def read_config(self): """ Read config from file """ try: with open(self.config_file, 'r') as f: self.config = json.loads(f.read()) f.close() except IOError: return False return True def post_note(self): """ Post note and return the URL of the posted note """ if self.args.note_title: note_title = self.args.note_title else: note_title = None note_content = self.args.note_content mynote = self.pump.Note(display_name=note_title, content = note_content) mynote.to = self.pump.me.followers mynote.cc = self.pump.Public mynote.send() return mynote.id or None
#!/usr/bin/env python # Copyright 2014, Stephen Jacob Sekula # http://polari.us/dokuwiki/doku.php?id=navierstokes#a_simple_program_to_authenticate_pypump_against_your_pumpio_instance # modified to take arguements, and work with python3 from pypump import PyPump, Client import sys """ arg 1 webfinger arg 2 client name """ client = Client( webfinger=sys.argv[1], type="native", # Can be "native" or "web" name=sys.argv[2]) def simple_verifier(url): print('Go to: ' + url) return input('Verifier: ') # they will get a code back pump = PyPump(client=client, verifier_callback=simple_verifier) print(pump.client.key + ': key') print(pump.client.secret + ': secret') print('Get the Token + Verification from the browser')
class App(object): client_name = 'pypump-post-note' pump = None config_file = os.path.join(os.environ['HOME'], '.config', client_name, 'config.json') config = dict() def __init__(self): """ Init app and log in to our pump.io account """ # Set up argparse self.parser = argparse.ArgumentParser( description='Post a note to the pump.io network') self.parser.add_argument('-u', '--user', dest='webfinger', required=True, help='*****@*****.**') self.parser.add_argument('-t', '--title', dest='note_title', default=None, help='Note title') self.parser.add_argument('-n', '--note', dest='note_content', required=True, help='Note content') self.args = self.parser.parse_args() self.read_config() # Try to login to our pump.io account using account credentials # from config, if our webfinger is not found in config we will # have to authorize the app with our account. webfinger = self.args.webfinger client = Client( webfinger=webfinger, type='native', name=self.client_name, key=self.config.get(webfinger, {}).get('key'), secret=self.config.get(webfinger, {}).get('secret'), ) self.pump = PyPump( client=client, token=self.config.get(webfinger, {}).get('token'), secret=self.config.get(webfinger, {}).get('token_secret'), verifier_callback=self.verifier, ) # Add account credentials to config in case we didnt have it already self.config[webfinger] = { 'key': self.pump.get_registration()[0], 'secret': self.pump.get_registration()[1], 'token': self.pump.get_token()[0], 'token_secret': self.pump.get_token()[1], } self.write_config() def verifier(self, url): """ Will ask user to click link to accept app and write code """ webbrowser.open(url) print( 'A browser should have opened up with a link to allow us to access' ) print( 'your account, follow the instructions on the link and paste the verifier' ) print( 'Code into here to give us access, if the browser didn\'t open, the link is:' ) print(url) print() return input('Verifier: ').lstrip(" ").rstrip(" ") def write_config(self): """ Write config to file """ if not os.path.exists(os.path.dirname(self.config_file)): os.makedirs(os.path.dirname(self.config_file)) with open(self.config_file, 'w') as f: f.write(json.dumps(self.config)) f.close() def read_config(self): """ Read config from file """ try: with open(self.config_file, 'r') as f: self.config = json.loads(f.read()) f.close() except IOError: return False return True def post_note(self): """ Post note and return the URL of the posted note """ if self.args.note_title: note_title = self.args.note_title else: note_title = None note_content = self.args.note_content mynote = self.pump.Note(display_name=note_title, content=note_content) mynote.to = self.pump.me.followers mynote.cc = self.pump.Public mynote.send() return mynote.id or None
class Account(object): app = None webfinger = None key = None secret = None token = None token_secret = None following = None cfg = None def __init__(self, webfinger, alias=None, app=None): self.app = app if not webfinger: self.webfinger = raw_input( 'Enter webfinger ([email protected]) for your %s account: ' % alias) else: self.webfinger = webfinger self.cfg = self.app.cfg.get(self.webfinger, {}) client = Client(webfinger=self.webfinger, type='native', name=self.app.name, key=self.cfg.get('key'), secret=self.cfg.get('secret')) self.pump = PyPump(client=client, verifier_callback=self.verifier, token=self.cfg.get('token'), secret=self.cfg.get('token_secret')) self.key, self.secret, self.expiry = self.pump.get_registration() self.token, self.token_secret = self.pump.get_token() self.write_config() self.get_following() self.backup_following() self.app.say('%s: account ready (following %s contacts)\n----' % (self.webfinger, len(self.following))) def verifier(self, url): print("Please open and follow the instructions:") print(url) return raw_input("Verifier: ").strip() def prompt_enter(self, msg): if self.app.parser.args.quiet and self.app.parser.args.noprompt: pass else: print(msg) if not self.app.parser.args.noprompt: try: raw_input("Hit enter to continue or ctrl+c to quit") except KeyboardInterrupt: sys.exit() def write_config(self): self.app.cfg[self.webfinger] = { 'key': self.key, 'secret': self.secret, 'token': self.token, 'token_secret': self.token_secret } self.app.write_config() def backup_following(self): self.app.backup_following(self) def get_following(self): self.app.say("%s: getting contacts (this may take a while)" % self.webfinger) self.following = list(self.pump.me.following) def follow_webfinger(self, webfinger): if self.app.parser.args.dryrun: return True try: obj = { "id": "acct:%s" % webfinger, "objectType": "person", } activity = { "verb": "follow", "object": obj, "to": [obj], } self.pump.me._post_activity(activity, unserialize=False) return True except: return False def unfollow_webfinger(self, webfinger): if self.app.parser.args.dryrun: return True try: obj = { "id": "acct:%s" % webfinger, "objectType": "person", } activity = { "verb": "stop-following", "object": obj, "to": [obj], } self.pump.me._post_activity(activity, unserialize=False) return True except: return False def follow_many(self, webfingers): self.prompt_enter("%s: will now follow %s new contacts" % (self.webfinger, len(webfingers))) for webfinger in webfingers: # we dont want to follow ourselves if webfinger == self.webfinger: self.app.say(" Skipped: %s (your account)" % webfinger) # skip people we are already following elif webfinger in [i.webfinger for i in self.following]: self.app.say(" Skipped: %s (already following)" % webfinger) elif self.follow_webfinger(webfinger): self.app.say(" Followed: %s" % webfinger) else: self.app.say(" Failed: %s" % webfinger) self.get_following() self.app.say("%s: now following %s contacts\n----" % (self.webfinger, len(self.following))) def unfollow_many(self, webfingers): self.prompt_enter("%s: will now unfollow %s contacts" % (self.webfinger, len(webfingers))) for webfinger in webfingers: # skip contacts we are not following if webfinger not in [i.webfinger for i in self.following]: self.app.say(" Skipped %s (not following)" % webfinger) elif self.unfollow_webfinger(webfinger): self.app.say(" Unfollowed: %s" % webfinger) else: self.app.say(" Failed: %s" % webfinger) self.get_following() self.app.say("%s: now following %s contacts\n----" % (self.webfinger, len(self.following)))
sys.stderr.write("ERROR: Could not read `gazouilleur/config.py`.\nERROR: Please edit it to fix the following syntax issue:\nERROR: %s\n%s\n" % (e, "\n".join(traceback.format_exc().splitlines()[-3:-1]))) exit(1) def verifier(url): print url return raw_input('Paste here the "Verifier" value given in Identi.ca\'s "Authorization Complete" page: ') confs = [] for chan, conf in config.CHANNELS.iteritems(): if 'IDENTICA' in conf and 'USER' in conf['IDENTICA']: user = conf['IDENTICA']['USER'].lower() print "Configuring Identi.ca OAuth for @%s for channel %s..." % (user, chan) print "Please make sure to be not logged in on Identi.ca with another account than this one in your browser before clicking the authorize url." try: client = Client(webfinger="*****@*****.**" % user, type="native", name="Gazouilleur") pump = PyPump(client=client, verifier_callback=verifier) client_credentials = pump.get_registration() client_tokens = pump.get_token() confs.append('"%s": {"key": "%s", "secret": "%s", "token": "%s", "token_secret": "%s"}' % (user, client_credentials[0], client_credentials[1], client_tokens[0], client_tokens[1])) except Exception as e: print "Could not properly get Identi.ca OAuth credits for user @%s:" % user print e if confs: with open('gazouilleur/identica_auth_config.py', 'w') as conf_file: conf_file.write("""#!/usr/bin/env python # -*- coding: utf-8 -*- # This file was generated automatically by `python bin/auth_identica.py` on %s # Do not modify unless you are sure of what you are doing. identica_auth={%s}""" % (datetime.datetime.today(), ", ".join(confs)))
class Pump(Actor): pump = None me = None def __init__(self, *args, **kwargs): super(Pump, self).__init__(*args, **kwargs) self.message_routing.update({ "fetch_inbox": self.fetch_inbox, "setup": self.setup, }) def setup(self, message): """ Sets up pypump object """ try: webfinger = message.body["webfinger"] self.pump = PyPump( webfinger, client_name="Muon", client_type="native", key=message.body["key"], secret=message.body["secret"], token=message.body["token"], token_secret=message.body["token_secret"] ) webfinger = message.body["webfinger"] except KeyError: webfinger = raw_input("Webfinger: ").lstrip(" ").rstrip(" ") self.pump = PyPump( webfinger, client_name="Muon", client_type="native" ) config = { "webfinger": webfinger, "key": self.pump.get_registration()[0], "secret": self.pump.get_registration()[1], "token": self.pump.get_token()[0], "token_secret": self.pump.get_token()[1], } self.hive.send_message( to="muon", directive="write_config", body=config ) self.me = self.pump.Person(webfinger) self.send_message( to="muon", directive="start_gui" ) def fetch_inbox(self, message): if self.me is None: time.sleep(1) return self.fetch_inbox(message) appender, redraw, update = message.body inbox = self.me.inbox[:100] for activity in inbox: appender(activity) redraw() update()
class Microblog(object): twitter_api_limit = 15 twitter_api_version = "1.1" def __init__(self, site, conf, bearer_token=None, get_token=False, streaming=False, upload=False): self.site = site.lower() self.conf = conf[site.upper()] # Identi.ca service only supported for commands "ping" and "microblog" if self.site == "identica": self.domain = "identi.ca" # New Pump.io Identi.ca connection: self.user = "******" % (self.conf['USER'].lower(), self.domain) from gazouilleur.identica_auth_config import identica_auth self.conf.update(identica_auth[self.conf['USER'].lower()]) iclient = Client(webfinger=self.user, type="native", name="Gazouilleur", key=self.conf['key'], secret=self.conf['secret']) self.conn = PyPump(client=iclient, token=self.conf['token'], secret=self.conf['token_secret'], verifier_callback=lambda: "") elif self.site == "twitter": self.domain = "api.twitter.com" self.user = self.conf['USER'] self.post = 'FORBID_POST' not in conf['TWITTER'] or str( conf['TWITTER']['FORBID_POST']).lower() != "true" args = {"api_version": self.twitter_api_version, "secure": True} format = "json" if get_token: format = "" args["api_version"] = None args["auth"] = OAuth2(self.conf['KEY'], self.conf['SECRET']) elif bearer_token: args["auth"] = OAuth2(bearer_token=bearer_token) else: args["auth"] = OAuth(self.conf['OAUTH_TOKEN'], self.conf['OAUTH_SECRET'], self.conf['KEY'], self.conf['SECRET']) if streaming: self.domain = "stream.twitter.com" args['block'] = False args['timeout'] = 10 conn = TwitterStream else: if upload: self.domain = "upload.twitter.com" args['format'] = format conn = Twitter args['domain'] = self.domain self.conn = conn(**args) def get_oauth2_token(self): res = self.conn.oauth2.token(grant_type="client_credentials") obj = load_json(res) if "token_type" not in obj or obj[ "token_type"] != "bearer" or "access_token" not in obj: raise Exception("Wrong token type given by twitter, weird : %s" % res) return obj["access_token"] def _send_query(self, function, args={}, tryout=0, previous_exception=None, return_result=False, extended_tweets=False, channel=None): if tryout > 2: return previous_exception.encode('utf-8') try: if not return_result: args['trim_user'] = '******' if extended_tweets: args['tweet_mode'] = 'extended' args['source'] = config.BOTNAME setdefaulttimeout(15) res = function(**args) if return_result: if extended_tweets: res = reformat_extended_tweets(res) return res if config.DEBUG and not 'media[]' in args: loggvar("%s %s" % (res.get('text', unicode( res.get('event'))).encode('utf-8'), args), action=self.site) if self.site == 'twitter' and channel and 'id_str' in res: save_lasttweet_id(channel, res['id_str']) imgstr = "" if "media_ids" in args: nimg = args["media_ids"].count(",") imgstr = " sending tweet with %s attached" % ( "%s images" % (nimg + 1) if nimg else "image") return "[%s] Huge success%s!" % (self.site, imgstr) except Exception as e: code, exception = get_error_message(e) if code in [None, 32, 183, 187, 400, 403, 404, 429, 500, 503]: return "[%s] %s" % (self.site, exception.encode('utf-8')) if config.DEBUG and exception != previous_exception: loggerr("http://%s/%s.%s %s : %s" % (self.domain, "/".join( function.uriparts), function.format, args, exception), action=self.site) return self._send_query(function, args, tryout + 1, exception, return_result, extended_tweets, channel) def ping(self): setdefaulttimeout(35) try: if self.site == "identica": return "%s@%s" % (self.conn.Person( self.user).username, self.domain) == self.user creds = self.conn.account.verify_credentials( include_entities='false', skip_status='true') dms = True if self.post: trydms = self.get_dms() dms = (isinstance(trydms, object) and "events" in trydms) or (isinstance(trydms, str) and "ERROR 429" in trydms) if config.DEBUG and not (creds and dms): raise Exception("%s\n%s" % (creds, dms)) return creds is not None and dms except Exception as e: if config.DEBUG: loggerr("Ping failed: %s" % e, action=self.site) return False def get_twitter_rates(self): return self._send_query(self.conn.application.rate_limit_status, return_result=True) def get_twitter_conf(self): res = self._send_query(self.conn.help.configuration, return_result=True) return res.get('short_url_length_https', res.get('short_url_length', 22) + 1), res.get( 'photo_size_limit', 3145728) def send_media(self, imgdata): return self._send_query(self.conn.media.upload, {"media": imgdata}, return_result=True) def microblog(self, text="", tweet_id=None, imgs=None, quote_tweet=None, channel=None, length=0, force=False): if text.startswith("%scount" % COMMAND_CHAR_REG): text = text.replace("%scount" % COMMAND_CHAR_REG, "").strip() if self.site == "identica": try: note = self.conn.Note(text) note.to = (self.conn.Public, self.conn.me.followers, self.conn.me.following) setdefaulttimeout(15) note.send() return "[identica] Huge success!" except Exception as e: err_msg = re_clean_identica_error.sub('', str(e)) if "[Errno 111] Connection refused" in err_msg or "ECONNREFUSED" in err_msg: err_msg = "https://identi.ca seems down" exception = "[identica] %s" % err_msg if config.DEBUG: loggerr(e, action=self.site) return exception args = {'status': text.replace('\\n', '\n')} if tweet_id: args['in_reply_to_status_id'] = str(tweet_id) if not force: args['auto_populate_reply_metadata'] = 'true' if quote_tweet: args['attachment_url'] = quote_tweet if imgs: args['media_ids'] = ",".join(imgs) return self._send_query(self.conn.statuses.update, args, channel=channel) def delete(self, tweet_id): return self._send_query(self.conn.statuses.destroy, {'id': tweet_id}) def retweet(self, tweet_id, channel=None): return self._send_query(self.conn.statuses.retweet, {'id': tweet_id}, channel=channel) def like(self, tweet_id, channel=None): return self._send_query(self.conn.favorites.create, { '_id': tweet_id, 'include_entities': False }, channel=channel) def show_status(self, tweet_id): return self._send_query(self.conn.statuses.show, { 'id': tweet_id, 'tweet_mode': 'extended' }, return_result=True, extended_tweets=True) def get_mytweets(self, **kwargs): return self._send_query(self.conn.statuses.user_timeline, { 'screen_name': self.user, 'count': 15, 'include_rts': 1, 'tweet_mode': 'extended' }, return_result=True, extended_tweets=True) def get_mentions(self, **kwargs): return self._send_query(self.conn.statuses.mentions_timeline, { 'count': 200, 'include_rts': 1, 'tweet_mode': 'extended' }, return_result=True, extended_tweets=True) def get_retweets(self, retweets_processed={}, bearer_token=None, **kwargs): tweets = self._send_query(self.conn.statuses.retweets_of_me, { 'count': 50, 'trim_user': '******', 'include_user_entities': 'false' }, return_result=True) done = 0 retweets = [] check_twitter_results(tweets) if type(tweets) is str: return retweets, retweets_processed if bearer_token: helper = Microblog("twitter", {"TWITTER": self.conf}, bearer_token=bearer_token) limitfactor = 4 else: helper = self limitfactor = 1 for tweet in tweets: if tweet['id_str'] not in retweets_processed or tweet[ 'retweet_count'] > retweets_processed[tweet['id_str']]: new_rts = helper.get_retweets_by_id(tweet['id']) if "ERROR 429" in new_rts: break retweets += new_rts done += 1 retweets_processed[tweet['id_str']] = tweet['retweet_count'] if done >= limitfactor * int(self.twitter_api_limit / 3): break return retweets, retweets_processed def get_retweets_by_id(self, tweet_id, **kwargs): return self._send_query(self.conn.statuses.retweets, { 'id': tweet_id, 'count': 100 }, return_result=True, extended_tweets=True) def directmsg(self, username, text, length=0): user = self.resolve_users([username]) if "ERROR 429" in user or "ERROR 404" in user or not (isinstance( user, list) and len(user) == 1): return "[twitter] Cannot find account %s" % username event = { "type": "message_create", "message_create": { "target": { "recipient_id": user[0]["id_str"] }, "message_data": { "text": text } } } return self._send_query(self.conn.direct_messages.events.new, {"_json": { "event": event }}) def get_dms(self, **kwargs): return self._send_query(self.conn.direct_messages.events.list, {'count': 50}, return_result=True) @inlineCallbacks def get_stats(self, **kwargs): timestamp = timestamp_hour(datetime.today()) try: last = yield find_stats({'user': self.user.lower()}, limit=1, filter=sortdesc('timestamp')) last = last[0] except: last = {} if last and last['timestamp'] == timestamp: res = None else: res = self._send_query(self.conn.users.show, {'screen_name': self.user}, return_result=True) check_twitter_results(res) returnValue((res, last, timestamp)) def search(self, query, count=50, max_id=None): args = { 'q': query, 'count': count, 'result_type': 'recent', 'include_entities': True, 'tweet_mode': 'extended' } if max_id: args['max_id'] = max_id return self._send_query(self.conn.search.tweets, args, return_result=True, extended_tweets=True) def search_stream(self, follow=[], track=[]): if not "stream" in self.domain or not len(follow) + len(track): return None args = {'filter_level': 'none', 'stall_warnings': 'true'} if track: args['track'] = ",".join(track) if follow: args['follow'] = ",".join(follow) if config.DEBUG: loggvar(args, action="stream") return self.conn.statuses.filter(**args) def search_users(self, query, count=3): query = urlquote( cleanblanks(query).strip('@').lower().replace(' ', '+'), '') users = self._send_query(self.conn.users.search, { 'q': query, 'count': count, 'include_entities': 'false' }, return_result=True) if isinstance(users, str): return [] return [u['screen_name'] for u in users] def lookup_users(self, list_users, cache_users={}, return_first_result=False): good = {} todo = [] for name in list_users: name = name.lower().lstrip('@') if name in cache_users: good[name] = cache_users[name] else: todo.append(name) if not todo: return good, cache_users users = self.resolve_users(todo) if "ERROR 429" in users or "ERROR 404" in users or not isinstance( users, list): return good, cache_users list_users = [l.decode('utf-8') for l in list_users] for u in users: if return_first_result: return u, cache_users name = u['screen_name'].decode('utf-8').lower() if name in list_users: good[name] = u['id_str'] cache_users.update(good) return good, cache_users re_twitter_account = re.compile('(^|\W)@([A-Za-z0-9_]{1,15})') re_bad_account = re.compile( '(^|\W)(@[A-Za-z0-9_]{1,14}[%s]+[A-Za-z0-9_]*)([^A-Za-z0-9_]|$)' % ACCENTS) def test_microblog_users(self, text, cache_users={}): force = ". Please correct your tweet of force by adding --force" match = self.re_bad_account.search(text) if match: return False, cache_users, "Sorry but %s does not seem like a valid account%s" % ( match.group(2), force) match = self.re_twitter_account.findall(text) if not len(match): return True, cache_users, "No user quoted" check = [] for _, m in match: user = m.lower().lstrip('@') if user not in cache_users: check.append(user) good, cache_users = self.lookup_users(check, cache_users) for user in check: if user not in good.keys(): extra = "" proposals = self.search_users(user) if proposals: extra = " (maybe you meant @%s ?)" % " or @".join( [p.encode('utf-8') for p in proposals]) return False, cache_users, "Sorry but @%s doesn't seem like a real account%s%s" % ( user, extra, force) return True, cache_users, "All users quoted passed" def follow(self, user, **kwargs): return self._send_query(self.conn.friendships.create, { 'screen_name': user, 'follow': 'true' }, return_result=True) def unfollow(self, user, **kwargs): return self._send_query(self.conn.friendships.destroy, {'screen_name': user}, return_result=True) @inlineCallbacks def update_followers(self, db): # Get followers list from Twitter curfolls = set() cursor = -1 while cursor: res = self._send_query(self.conn.followers.ids, { 'screen_name': self.user, "cursor": cursor, "count": 5000 }, return_result=True) if "ERROR 429" in res or "ERROR 404" in res or not isinstance( res, dict): loggerr(res) returnValue([]) cursor = res.get("next_cursor", res.get("next_cursor_str", 0)) curfolls |= set([str(i) for i in res["ids"]]) # Get known info on followers from DB foll_coll = db_foll_coll(self.user) oldfolls = yield db[foll_coll].find({"follows_me": True}, fields=[]) oldfolls = set([f["_id"] for f in oldfolls]) oldlost = yield db[foll_coll].find({"follows_me": False}, fields=[]) oldlost = set([f["_id"] for f in oldlost]) oldtodo = yield db[foll_coll].find({"screen_name": None}, fields=[]) oldtodo = [f["_id"] for f in oldtodo] # Save new found followers newids = curfolls - oldfolls oldusers = newids & oldlost if oldusers: yield db[foll_coll].update({"_id": { "$in": list(oldusers) }}, {"$set": { "follows_me": True, "last_update": time.time() }}, multi=True) newusers = [{ "_id": str(u), "follows_me": True, "last_update": time.time() if len(oldfolls) else 0 } for u in list(newids - oldusers)] if newusers: yield db[foll_coll].insert(newusers) # Update old followers lost lostids = list(oldfolls - curfolls) todolostids = [] if lostids: # only keep for display lost ones with old activity to avoid repeated unfollow weird accounts todolostids = yield db[foll_coll].find( { "_id": { "$in": lostids }, "last_update": { "$lte": time.time() - 604800 } }, fields=[]) todolostids = [f["_id"] for f in todolostids] yield db[foll_coll].update({"_id": { "$in": lostids }}, {"$set": { "follows_me": False, "last_update": time.time() }}, multi=True) # Collect metas on missing profiles todo = todolostids + list(newids) + oldtodo lost = [] for chunk in chunkize(todo, 100): users = self.resolve_userids(chunk) if "ERROR 429" in users or "ERROR 404" in users or not isinstance( users, list): break for user in users: if str(user["id"]) in lostids: lost.append(user) for f in ["status", "entities"]: if f in user: del (user[f]) yield db[foll_coll].update({"_id": str(user["id"])}, {"$set": user}) returnValue(lost) def resolve_users(self, list_users): return self._send_query(self.conn.users.lookup, { 'screen_name': ','.join(list_users), 'include_entities': 'false' }, return_result=True) def resolve_userids(self, list_ids): return self._send_query(self.conn.users.lookup, { 'user_id': ','.join([str(i) for i in list_ids]), 'include_entities': 'false' }, return_result=True)
status_text = status_text + " " if len(status_text) > 140: print """Sorry, Exceeded 140 Characters. Tweet Again with Limited thoughts.""" sys.exit(1) else: print """Usage: tw "Enter your status here\"""" sys.exit(1) consumer_key = u'' consumer_secret = u'' oauth_token = '' oauth_secret = '' pump = PyPump("*****@*****.**", key=consumer_key, secret=consumer_secret, token=oauth_token, token_secret=oauth_secret) note = pump.Note(status_text) a = pump.Address() note.set_to(a.createPublic()) note.send() # == OAuth Authentication == # # This mode of authentication is the new preferred way # of authenticating with Twitter. # The consumer keys can be found on your application's Details # page located at https://dev.twitter.com/apps (under "OAuth settings") consumer_key = ""
webfinger = "*****@*****.**" # Webfinger for your account def simple_verifier(url): print('Go to: ' + url) return input('Verifier: ') # they will get a code back client = Client( webfinger, name="CornerBot", type="native", key=client_credentials[0], # client key secret=client_credentials[1] # client secret ) pump = PyPump(client=client, verifier_callback=simple_verifier) b_set = set() # the B set is TODAYS postings # logfile_name = "cornerbot.activity.log" # process_log = open(logfile_name,'a+') # process_log.write(blah) while 99 == 99: listing = "" header = "" print("\n\nInitiating\n\n") timestamp = str(datetime.now()) header += timestamp + " - Paris - " """ Gather the Le bon Coin Annonces