class CliTwi(object): consumer_key = config.consumer_key consumer_secret = config.consumer_secret cli_twi_path = os.path.expanduser('~') + '/.clitwi' token = None secret = None config_ok = False def __init__(self): os.chdir(self.cli_twi_path) # Figure out the action self.read_action() # Verify we have all of the necessary config files self.check_config() self.read_config() # And the power button... self.run() def read_action(self): l = len(sys.argv) if l == 1: self.action = 'list' elif l == 2: arg = sys.argv[1] if arg == 'setup': self.action = 'setup' elif arg == '-m': self.action = 'mentions' elif arg == '--help': self.action = 'help' else: self.action = 'update' def check_config(self): self.config_ok = True if not os.path.isfile('_config'): print 'Missing _config' self.config_ok = False def read_config(self): if self.action == 'setup' or self.action == 'help': return if not self.config_ok: return # Read the config file file = open('_config', 'r') v = file.read() config = json.loads(v) file.close() self.latest = config['latest'] self.mention = config['mention'] self.token = config['token'] self.secret = config['secret'] def setup(self): os.system('clear') twitter = OAuthApi(self.consumer_key, self.consumer_secret) # Get the temporary credentials for our next few calls temp_credentials = twitter.getRequestToken() # User pastes this into their browser to bring back a pin number print(twitter.getAuthorizationURL(temp_credentials)) # Get the pin # from the user and get our permanent credentials oauth_verifier = raw_input('What is the PIN? ') access_token = twitter.getAccessToken(temp_credentials, oauth_verifier) self.token = access_token['oauth_token'] self.secret = access_token['oauth_token_secret'] self.latest = 1 self.mention = 1 print 'Clitwi was successfully set up.' def setup_api(self): self.twitter = OAuthApi(self.consumer_key, self.consumer_secret, self.token, self.secret) def list_tweets(self, type='default'): os.system('clear') if type == 'default': user_timeline = self.twitter.GetHomeTimeline({ 'since_id': self.latest }) else: user_timeline = self.twitter.GetMentions({ 'since_id': self.mention }) if user_timeline == []: print 'No new tweets.' return first = True for tweet in user_timeline: print tweet['user']['screen_name'] + ':' self.print_text(tweet['text']) if first: if type == 'default': self.latest = tweet['id'] else: self.mention = tweet['id'] first = False def print_text(self, text): l = len(text) if l > 77: print ' ' + text[:77] print ' ' + text[77:] else: print ' ' + text def send_tweet(self): tweet = sys.argv[1] if len(tweet) > 140: print 'Too long' sys.exit(1) return else: self.twitter.UpdateStatus(str(tweet)) print 'Sent!' def show_help(self): print 'Cli Twi - version %s' % __version__ print 'main.py [options]' print 'main.py - will print latest tweets' print 'main.py "Some message" - will update your status' print 'main.py -m - will print latest mentions' print 'main.py setup - will run the OAuth process' print 'main.py --help - will display this help' def run(self): # Actions that don't require any config (help, setup) if self.action == 'setup': self.setup() elif self.action == 'help': self.show_help() # Check if we have correct config settings if not self.config_ok: return self.setup_api() if self.action == 'list': self.list_tweets() elif self.action == 'update': self.send_tweet() elif self.action == 'mentions': self.list_tweets('mentions') def finish(self): d = { 'token': self.token, 'secret': self.secret, 'latest': self.latest, 'mention': self.mention } f = open(os.getcwd() + '/_config', 'w') f.write(json.dumps(d)) f.close()
class Bot: # its supposed that all bots will interact using twitter and # facebook apps # tw_data={'mood':10, # mood affects the behaviour # 'tuser':'', # twitter username # 'tpass':'', # twitter password # 'consumer_key':'', # 'consumer_secret':'', # 'atoken':'', # 'stoken':''} mood = 10.0 # mood affects the behaviour tuser = '' # twitter username tpass = '' # twitter password consumer_key = '' consumer_secret = '' atoken = '' stoken = '' tApi = OAuthApi(consumer_key, consumer_secret) # Api email = '' # respectable bots have a valid email # Should be a service providing an API emailpass = '' message = '' # for messages construction def random_item(self, token): """Picks a random item from a list. """ return token[int(random.random() * len(token))] def twit_authenticate(self): """Begin a twitter session. """ self.tApi = OAuthApi(self.consumer_key, self.consumer_secret, self.atoken, self.stoken) def twit_deauthenticate(self): #deprecated #self.tApi.ClearCredentials() """Deprecated. Left for compatibility with older versions. """ pass def twit_twit(self, token): """Post a new status. """ self.tApi.UpdateStatus(token[0:140]) def twit_get_user_updates(self, usrname, num): """Get the last updates from a specific user. """ return self.tApi.GetUserTimeline(user=usrname, count=num) def Bot_sleep_random(self, ctime, sigtime): """ Sleep a randomized amount of time. """ time.sleep(random.gauss(mu=ctime, sigma=sigtime)) def Bot_sleep(self, token): """ Sleep a fixed amount of time """ time.sleep(token) # token in seconds
twitter = OAuthApi(consumer_key, consumer_secret) # Get the temporary credentials for our next few calls temp_credentials = twitter.getRequestToken() # User pastes this into their browser to bring back a pin number print(twitter.getAuthorizationURL(temp_credentials)) oauth_verifier = raw_input('What is the Verifier? ') access_token = twitter.getAccessToken(temp_credentials, oauth_verifier) print("oauth access token: " + access_token['oauth_token']) print("oauth access token secret: " + access_token['oauth_token_secret']) access_tok = access_token['oauth_token'] access_tok_secret = access_token['oauth_token_secret'] # Do a test API call using our new credentials twitter = OAuthApi(consumer_key, consumer_secret, access_tok, access_tok_secret) if True: res = twitter.VerifyCredentials() print( "User Name: " + res['name'] ) print( "Status: " + res['status']['text'] ) if False: res = twitter.UpdateStatus("Test 2 with OAuth") pp = pprint.PrettyPrinter(indent=4) pp.pprint(res)