import sys from libturpial.api.core import Core message = sys.argv[1] if not message or message == '': sys.exit(-1) ACCOUNT = 'turpialve-twitter' BROADCAST = ['satanas82', 'guerrerocarlos'] c = Core() c.login(ACCOUNT) c.auth(ACCOUNT) for acc in BROADCAST: c.send_direct(ACCOUNT, acc, message)
class TwitRss: def __init__(self): parser = OptionParser() parser.add_option('-d', '--debug', dest='debug', action='store_true', help='show debug info in shell during execution', default=False) parser.add_option('--setup', dest='setup', action='store_true', help='execute the setup wizard', default=False) parser.add_option('--add-account', dest='add_account', default=False, action='store_true', help='add a microblogging account to database') parser.add_option('--del-account', dest='delete_account', action='store_true', help='delete a microblogging account from \ database', default=False) parser.add_option('--list-accounts', dest='list_accounts', action='store_true', help='list all microblogging accounts', default=False) parser.add_option('--add-feed', dest='add_feed', action='store_true', help='add feed to database', default=False) parser.add_option('--del-feed', dest='delete_feed', action='store_true', help='delete feed from database', default=False) parser.add_option('--list-feeds', dest='list_feeds', action='store_true', help='list all registered feeds', default=False) parser.add_option('--associate', dest='associate_feed', action='store_true', help='associate feed with account', default=False) parser.add_option('--deassociate', dest='deassociate_feed', action='store_true', help='deassociate feed from account', default=False) parser.add_option('--change-prefix', dest='change_prefix', action='store_true', default=False, help='change the publish prefix for certain feed/account') parser.add_option('--show-info', dest='show_info', action='store_true', help='show information about feeds and accounts', default=False) parser.add_option('--test', dest='test', action='store_true', help='poll feeds without posting or saving in db', default=False) parser.add_option('--empty-records', dest='empty_records', action='store_true', default=False, help='delete posts and update records from database') (options, args) = parser.parse_args() if options.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) self.log = logging.getLogger('TwitRSS') self.db = DBEngine() Feed.db = self.db Account.db = self.db AccountFeed.db = self.db Post.db = self.db self.core = Core() Account.core = self.core if options.setup: self.setup() self.quit() if options.add_account: self.add_account() self.quit() if options.delete_account: self.delete_account() self.quit() if options.add_feed: self.add_feed() self.quit() if options.delete_feed: self.delete_feed() self.quit() if options.associate_feed: self.associate_feed() self.quit() if options.deassociate_feed: self.deassociate_feed() self.quit() if options.show_info: self.show_info() self.quit() if options.empty_records: self.empty_records() self.quit() self.test = options.test self.log.info("Starting service") self.queue = Queue.Queue() Post.queue = self.queue self.start() def __user_input(self, message, blank=False): while 1: text = raw_input(message) if text == '' and not blank: print "You can't leave this field blank" continue break return text def __user_password(self, message): passwd = None while 1: passwd = getpass.unix_getpass(message) if passwd: return passwd else: print "Password can't be blank" def __build_confirm_menu(self, message): confirm = raw_input(message + ' [y/N]: ') if confirm.lower() == 'y': return True else: return False def __build_protocols_menu(self): index = None protocols = self.core.list_protocols() while 1: print "Available protocols:" for i in range(len(protocols)): print "[%i] %s" % (i, protocols[i]) index = raw_input('Select protocol: ') if not self.__validate_index(index, protocols): print "Invalid protocol" else: break return protocols[int(index)] def __build_accounts_menu(self, _all=False): index = None while 1: accounts = self.__show_accounts() if _all: index = raw_input('Select account (or Enter for all): ') else: index = raw_input('Select account: ') if not self.__validate_index(index, accounts, _all): print "Invalid account" else: break if index == '': return accounts else: return accounts[int(index)] def __show_accounts(self, just_list=False): accounts = [] reg_accs = Account.get_from_libturpial() if len(reg_accs) == 0: return None print "\nAvailable accounts:" for acc in reg_accs: if just_list: print "* %s (%s)" % (acc.username, acc.protocol) else: print "[%i] %s - %s" % (len(accounts), acc.username, acc.protocol) full_acc = Account.save_from_obj(acc) accounts.append(full_acc) return accounts def __build_feeds_menu(self): index = None while 1: feeds = self.__show_feeds() index = raw_input('Select feed: ') if not self.__validate_index(index, feeds, False): print "Invalid feed" else: break return feeds[int(index)] def __show_feeds(self, just_list=False): rtn = Feed.get_all() if len(rtn) == 0: self.log.info("There are no registered feeds") return None feeds = [] print "\nAvailable feeds:" for feed in rtn: if just_list: print "* %s" % (feed.url) else: print "[%i] %s" % (len(feeds), feed.url) feeds.append(feed) return feeds def __build_account_feeds_menu(self): index = None while 1: afs = self.__show_account_feeds() index = raw_input('Select account/feed: ') if not self.__validate_index(index, afs, False): print "Invalid account/feed" else: break return afs[int(index)] def __show_account_feeds(self, just_list=False): rtn = AccountFeed.get_all() if len(rtn) == 0: self.log.info("There are no feeds associated with accounts") return None account_feeds = [] print "\nFeeds associated with accounts:" for af in rtn: if just_list: print "* %-35s %-35s" % (af.account, af.feed.url) else: print "[%i] %-35s %-35s" % (len(account_feeds), af.account, af.feed.url) account_feeds.append(af) return account_feeds def __validate_index(self, index, array, blank=False): try: a = array[int(index)] return True except IndexError: return False except ValueError: if blank and index == '': return True elif not blank and index == '': return False elif blank and index != '': return False except TypeError: if index is None: return False def start(self): accounts = Account.count() feeds = Feed.count() account_feeds = AccountFeed.count() if (accounts == 0) or (feeds == 0) or (account_feeds == 0): self.log.info('You need to fully configure your bot. Please execute script with --setup param') return self.login() def login(self): accounts = self.core.all_accounts() for acc in accounts: response = self.core.login(acc.id_) if response.code > 0: print "Login error:", response.errmsg return auth_obj = response.items if auth_obj.must_auth(): print "Please visit %s, authorize Turpial and type the pin \ returned" % auth_obj.url pin = self.user_input('Pin: ') self.core.authorize_oauth_token(acc.id_, pin) rtn = self.core.auth(acc.id_) if rtn.code > 0: print rtn.errmsg return else: self.log.debug('Logged in with account %s' % acc.id_) self.main() # ======================================================================= # Commands # ======================================================================= def setup(self): accounts = self.__show_accounts(True) if not accounts: print 'You need to create at least one account' self.add_account() while 1: if self.__build_confirm_menu('Do you want to add more accounts?'): self.add_account() else: break self.__show_feeds(True) while 1: if self.__build_confirm_menu('Do you want to add more feeds?'): self.add_feed() else: break while 1: if self.__build_confirm_menu('\nDo you want to associate feeds with accounts?'): self.associate_feed() else: break def add_account(self): username = self.__user_input('Username: '******'' if protocol == ProtocolType.IDENTICA: passwd = self.__user_password('Password: '******'Pin: ') self.core.authorize_oauth_token(acc_id, pin) rtn = self.core.auth(acc_id) if rtn.code > 0: self.log.error(response.errmsg) return # Save in DB Account.save(acc_id, username, protocol) self.log.info('Account added successfully') except Exception, e: self.log.exception(e) self.log.error('Error registering account. Please try again')
class Adopta: def __init__(self): parser = OptionParser() parser.add_option('-d', '--debug', dest='debug', action='store_true', help='show debug info in shell during execution', default=False) (options, args) = parser.parse_args() if options.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) self.core = Core() self.followers = [] self.following = [] self.last_dm = None self.log = logging.getLogger('Server') self.start_login() def start_login(self): self.core.register_account( ACCOUNT.split('-')[0], ACCOUNT.split('-')[1], '') response = self.core.login(ACCOUNT) if response.code > 0: print "Login error:", response.errmsg return auth_obj = response.items if auth_obj.must_auth(): print "Please visit %s, authorize Turpial and type the pin returned" % auth_obj.url pin = self.user_input('Pin: ') self.core.authorize_oauth_token(ACCOUNT, pin) rtn = self.core.auth(ACCOUNT) if rtn.code > 0: print rtn.errmsg else: self.log.debug('Logged in with account %s' % ACCOUNT.split('-')[0]) self.main() def user_input(self, message, blank=False): while 1: text = raw_input(message) if text == '' and not blank: print "You can't leave this field blank" continue break return text def get_followers(self): response = self.core.get_followers(ACCOUNT, True) if response.code > 0: self.log.error("Error getting followers list:", response.errmsg) else: self.followers = response.items def get_following(self): response = self.core.get_following(ACCOUNT, True) if response.code > 0: self.log.error("Error getting following list:", response.errmsg) else: self.following = response.items def process_follow_back(self): temp = [] for item in self.followers: if item not in self.following: if len(temp) < MAX_FOLLOW: temp.append(item) else: break for item in temp: response = self.core.follow(ACCOUNT, str(item), by_id=True) if response.code > 0: self.log.error("Error following to %s: %s" % (item, response.errmsg)) else: self.log.debug("Follow back to %s" % item) self.following.append(item) def process_dms(self): response = self.core.get_column_statuses(ACCOUNT, ColumnType.DIRECTS, 200) if response.code > 0: self.log.error("Error fetching DMs: %s" % response.errmsg) else: for dm in response.items: msg_id = dm.id_ valid = self.validate_dm(dm) if not valid: continue via = ' (via @%s)' % dm.username text = dm.text length = len(text) + len(via) if length > 140: text = text[:len(text) - len(via)] message = text + via message.encode('utf-8') rtn = self.core.update_status(ACCOUNT, message) if rtn.code > 0: self.log.error("Error posting message '%s': %s" % (message, rtn.errmsg)) else: self.register_message(dm) def validate_dm(self, dm): # TODO: Search in database for msg_id, if exist then return False # otherwiser return True if len(Tweet.objects.filter(status_id=dm.id_)) > 0: return False else: return True def register_message(self, dm): # TODO: Add dm to database t = Tweet(status_id=dm.id_, username=dm.username, content=dm.text) t.save() pass def main(self): while True: try: # Processing followbacks self.get_followers() self.get_following() self.process_follow_back() # Processing DMs self.process_dms() time.sleep(POLLING_TIME * 60) except KeyboardInterrupt: break self.log.debug('Bye')
class Adopta: def __init__(self): parser = OptionParser() parser.add_option('-d', '--debug', dest='debug', action='store_true', help='show debug info in shell during execution', default=False) (options, args) = parser.parse_args() if options.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) self.core = Core() self.followers = [] self.following = [] self.last_dm = None self.log = logging.getLogger('Server') self.start_login() def start_login(self): self.core.register_account(ACCOUNT.split('-')[0], ACCOUNT.split('-')[1], '') response = self.core.login(ACCOUNT) if response.code > 0: print "Login error:", response.errmsg return auth_obj = response.items if auth_obj.must_auth(): print "Please visit %s, authorize Turpial and type the pin returned" % auth_obj.url pin = self.user_input('Pin: ') self.core.authorize_oauth_token(ACCOUNT, pin) rtn = self.core.auth(ACCOUNT) if rtn.code > 0: print rtn.errmsg else: self.log.debug('Logged in with account %s' % ACCOUNT.split('-')[0]) self.main() def user_input(self, message, blank=False): while 1: text = raw_input(message) if text == '' and not blank: print "You can't leave this field blank" continue break return text def get_followers(self): response = self.core.get_followers(ACCOUNT, True) if response.code > 0: self.log.error("Error getting followers list:", response.errmsg) else: self.followers = response.items def get_following(self): response = self.core.get_following(ACCOUNT, True) if response.code > 0: self.log.error("Error getting following list:", response.errmsg) else: self.following = response.items def process_follow_back(self): temp = [] for item in self.followers: if item not in self.following: if len(temp) < MAX_FOLLOW: temp.append(item) else: break for item in temp: response = self.core.follow(ACCOUNT, str(item), by_id=True) if response.code > 0: self.log.error("Error following to %s: %s" % (item, response.errmsg)) else: self.log.debug("Follow back to %s" % item) self.following.append(item) def process_dms(self): response = self.core.get_column_statuses(ACCOUNT, ColumnType.DIRECTS, 200) if response.code > 0: self.log.error("Error fetching DMs: %s" % response.errmsg) else: for dm in response.items: msg_id = dm.id_ valid = self.validate_dm(dm) if not valid: continue via = ' (via @%s)' % dm.username text = dm.text length = len(text) + len(via) if length > 140: text = text[:len(text) - len(via)] message = text + via message.encode('utf-8') rtn = self.core.update_status(ACCOUNT, message) if rtn.code > 0: self.log.error("Error posting message '%s': %s" % (message, rtn.errmsg)) else: self.register_message(dm) def validate_dm(self, dm): # TODO: Search in database for msg_id, if exist then return False # otherwiser return True if len(Tweet.objects.filter(status_id=dm.id_)) > 0: return False else: return True def register_message(self, dm): # TODO: Add dm to database t = Tweet(status_id=dm.id_,username=dm.username,content=dm.text) t.save() pass def main(self): while True: try: # Processing followbacks self.get_followers() self.get_following() self.process_follow_back() # Processing DMs self.process_dms() time.sleep(POLLING_TIME * 60) except KeyboardInterrupt: break self.log.debug('Bye')
class TwitRss: def __init__(self): parser = OptionParser() parser.add_option('-d', '--debug', dest='debug', action='store_true', help='show debug info in shell during execution', default=False) parser.add_option('--setup', dest='setup', action='store_true', help='execute the setup wizard', default=False) parser.add_option('--add-account', dest='add_account', default=False, action='store_true', help='add a microblogging account to database') parser.add_option('--del-account', dest='delete_account', action='store_true', help='delete a microblogging account from \ database', default=False) parser.add_option('--list-accounts', dest='list_accounts', action='store_true', help='list all microblogging accounts', default=False) parser.add_option('--add-feed', dest='add_feed', action='store_true', help='add feed to database', default=False) parser.add_option('--del-feed', dest='delete_feed', action='store_true', help='delete feed from database', default=False) parser.add_option('--list-feeds', dest='list_feeds', action='store_true', help='list all registered feeds', default=False) parser.add_option('--associate', dest='associate_feed', action='store_true', help='associate feed with account', default=False) parser.add_option('--deassociate', dest='deassociate_feed', action='store_true', help='deassociate feed from account', default=False) parser.add_option( '--change-prefix', dest='change_prefix', action='store_true', default=False, help='change the publish prefix for certain feed/account') parser.add_option('--show-info', dest='show_info', action='store_true', help='show information about feeds and accounts', default=False) parser.add_option('--test', dest='test', action='store_true', help='poll feeds without posting or saving in db', default=False) parser.add_option('--empty-records', dest='empty_records', action='store_true', default=False, help='delete posts and update records from database') (options, args) = parser.parse_args() if options.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) self.log = logging.getLogger('TwitRSS') self.db = DBEngine() Feed.db = self.db Account.db = self.db AccountFeed.db = self.db Post.db = self.db self.core = Core() Account.core = self.core if options.setup: self.setup() self.quit() if options.add_account: self.add_account() self.quit() if options.delete_account: self.delete_account() self.quit() if options.add_feed: self.add_feed() self.quit() if options.delete_feed: self.delete_feed() self.quit() if options.associate_feed: self.associate_feed() self.quit() if options.deassociate_feed: self.deassociate_feed() self.quit() if options.show_info: self.show_info() self.quit() if options.empty_records: self.empty_records() self.quit() self.test = options.test self.log.info("Starting service") self.queue = Queue.Queue() Post.queue = self.queue self.start() def __user_input(self, message, blank=False): while 1: text = raw_input(message) if text == '' and not blank: print "You can't leave this field blank" continue break return text def __user_password(self, message): passwd = None while 1: passwd = getpass.unix_getpass(message) if passwd: return passwd else: print "Password can't be blank" def __build_confirm_menu(self, message): confirm = raw_input(message + ' [y/N]: ') if confirm.lower() == 'y': return True else: return False def __build_protocols_menu(self): index = None protocols = self.core.list_protocols() while 1: print "Available protocols:" for i in range(len(protocols)): print "[%i] %s" % (i, protocols[i]) index = raw_input('Select protocol: ') if not self.__validate_index(index, protocols): print "Invalid protocol" else: break return protocols[int(index)] def __build_accounts_menu(self, _all=False): index = None while 1: accounts = self.__show_accounts() if _all: index = raw_input('Select account (or Enter for all): ') else: index = raw_input('Select account: ') if not self.__validate_index(index, accounts, _all): print "Invalid account" else: break if index == '': return accounts else: return accounts[int(index)] def __show_accounts(self, just_list=False): accounts = [] reg_accs = Account.get_from_libturpial() if len(reg_accs) == 0: return None print "\nAvailable accounts:" for acc in reg_accs: if just_list: print "* %s (%s)" % (acc.username, acc.protocol) else: print "[%i] %s - %s" % (len(accounts), acc.username, acc.protocol) full_acc = Account.save_from_obj(acc) accounts.append(full_acc) return accounts def __build_feeds_menu(self): index = None while 1: feeds = self.__show_feeds() index = raw_input('Select feed: ') if not self.__validate_index(index, feeds, False): print "Invalid feed" else: break return feeds[int(index)] def __show_feeds(self, just_list=False): rtn = Feed.get_all() if len(rtn) == 0: self.log.info("There are no registered feeds") return None feeds = [] print "\nAvailable feeds:" for feed in rtn: if just_list: print "* %s" % (feed.url) else: print "[%i] %s" % (len(feeds), feed.url) feeds.append(feed) return feeds def __build_account_feeds_menu(self): index = None while 1: afs = self.__show_account_feeds() index = raw_input('Select account/feed: ') if not self.__validate_index(index, afs, False): print "Invalid account/feed" else: break return afs[int(index)] def __show_account_feeds(self, just_list=False): rtn = AccountFeed.get_all() if len(rtn) == 0: self.log.info("There are no feeds associated with accounts") return None account_feeds = [] print "\nFeeds associated with accounts:" for af in rtn: if just_list: print "* %-35s %-35s" % (af.account, af.feed.url) else: print "[%i] %-35s %-35s" % (len(account_feeds), af.account, af.feed.url) account_feeds.append(af) return account_feeds def __validate_index(self, index, array, blank=False): try: a = array[int(index)] return True except IndexError: return False except ValueError: if blank and index == '': return True elif not blank and index == '': return False elif blank and index != '': return False except TypeError: if index is None: return False def start(self): accounts = Account.count() feeds = Feed.count() account_feeds = AccountFeed.count() if (accounts == 0) or (feeds == 0) or (account_feeds == 0): self.log.info( 'You need to fully configure your bot. Please execute script with --setup param' ) return self.login() def login(self): accounts = self.core.all_accounts() for acc in accounts: response = self.core.login(acc.id_) if response.code > 0: print "Login error:", response.errmsg return auth_obj = response.items if auth_obj.must_auth(): print "Please visit %s, authorize Turpial and type the pin \ returned" % auth_obj.url pin = self.user_input('Pin: ') self.core.authorize_oauth_token(acc.id_, pin) rtn = self.core.auth(acc.id_) if rtn.code > 0: print rtn.errmsg return else: self.log.debug('Logged in with account %s' % acc.id_) self.main() # ======================================================================= # Commands # ======================================================================= def setup(self): accounts = self.__show_accounts(True) if not accounts: print 'You need to create at least one account' self.add_account() while 1: if self.__build_confirm_menu('Do you want to add more accounts?'): self.add_account() else: break self.__show_feeds(True) while 1: if self.__build_confirm_menu('Do you want to add more feeds?'): self.add_feed() else: break while 1: if self.__build_confirm_menu( '\nDo you want to associate feeds with accounts?'): self.associate_feed() else: break def add_account(self): username = self.__user_input('Username: '******'' if protocol == ProtocolType.IDENTICA: passwd = self.__user_password('Password: '******'Pin: ') self.core.authorize_oauth_token(acc_id, pin) rtn = self.core.auth(acc_id) if rtn.code > 0: self.log.error(response.errmsg) return # Save in DB Account.save(acc_id, username, protocol) self.log.info('Account added successfully') except Exception, e: self.log.exception(e) self.log.error('Error registering account. Please try again')
class Turpial(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) parser = OptionParser() parser.add_option('-d', '--debug', dest='debug', action='store_true', help='show debug info in shell during execution', default=False) parser.add_option('-m', '--command', dest='command', action='store_true', help='execute a single command', default=False) parser.add_option('-c', '--clean', dest='clean', action='store_true', help='clean all bytecodes', default=False) parser.add_option('-s', '--save-credentials', dest='save', action='store_true', help='save user credentials', default=False) parser.add_option('--version', dest='version', action='store_true', help='show the version of Turpial and exit', default=False) (options, args) = parser.parse_args() if options.debug or options.clean: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) self.log = logging.getLogger('Turpial:Cmd') #self.config = None self.prompt = 'turpial> ' self.intro = '\n'.join(INTRO) self.core = Core() #self.app_cfg = ConfigApp() #self.version = self.app_cfg.read('App', 'version') if options.clean: clean_bytecodes(__file__, self.log) sys.exit(0) if options.version: print "turpial (cmd) v%s" % self.version print "libturpial v%s" % libturpial_ver print "python v%X" % sys.hexversion sys.exit(0) self.account = None try: self.cmdloop() except KeyboardInterrupt: self.do_exit() except EOFError: self.do_exit() def __validate_index(self, index, array, blank=False): try: a = array[int(index)] return True except IndexError: return False except ValueError: if blank and index == '': return True elif not blank and index == '': return False elif blank and index != '': return False except TypeError: if index is None: return False def __validate_accounts(self): if len(self.core.list_accounts()) > 0: return True print "You don't have any registered account. Run 'account add' command" return False def __validate_default_account(self): if self.account: return True print "You don't have a default account. Run 'account change' command" return False def __validate_arguments(self, arg_array, value): if value in arg_array: return True else: print 'Invalid Argument' return False def __build_message_menu(self): text = raw_input('Message: ') if text == '': print 'You must write something to post' return None if len(text) > 140: trunc = raw_input ('Your message has more than 140 characters. Do you want truncate it? [Y/n]: ') if trunc.lower() == 'y' or trunc == '': return text[:140] return None return text def __build_accounts_menu(self, _all=False): if len(self.core.list_accounts()) == 1: return self.core.list_accounts()[0] index = None while 1: accounts = self.__show_accounts() if _all: index = raw_input('Select account (or Enter for all): ') else: index = raw_input('Select account: ') if not self.__validate_index(index, accounts, _all): print "Invalid account" else: break if index == '': return '' else: return accounts[int(index)] def __build_password_menu(self, account): passwd = None while 1: passwd = getpass.unix_getpass("Password for '%s' in '%s': " % ( account.split('-')[0], account.split('-')[1])) if passwd: return passwd else: print "Password can't be blank" def __build_change_account_menu(self): if len(self.core.list_accounts()) == 1: if self.account: print "Your unique account is already your default" else: self.__add_first_account_as_default() elif len(self.core.list_accounts()) > 1: while 1: accounts = self.__show_accounts() index = raw_input('Select you new default account (or Enter for keep current): ') if index == '': print "Default account remain with no changes" return True if not self.__validate_index(index, accounts): print "Invalid account" else: break self.account = accounts[int(index)] print "Set %s in %s as your new default account" % ( self.account.split('-')[0], self.account.split('-')[1]) def __build_protocols_menu(self): index = None protocols = self.core.list_protocols() while 1: print "Available protocols:" for i in range(len(protocols)): print "[%i] %s" % (i, protocols[i]) index = raw_input('Select protocol: ') if not self.__validate_index(index, protocols): print "Invalid protocol" else: break return protocols[int(index)] def __build_confirm_menu(self, message): confirm = raw_input(message + ' [y/N]: ') if confirm.lower() == 'y': return True else: return False def __user_input(self, message, blank=False): while 1: text = raw_input(message) if text == '' and not blank: print "You can't leave this field blank" continue break return text def __add_first_account_as_default(self): self.account = self.core.list_accounts()[0] print "Selected account %s in %s as default (*)" % ( self.account.split('-')[0], self.account.split('-')[1]) def __show_accounts(self): if len(self.core.list_accounts()) == 0: print "There are no registered accounts" return accounts = [] print "Available accounts:" for acc in self.core.list_accounts(): ch = '' if acc == self.account: ch = ' (*)' print "[%i] %s - %s%s" % (len(accounts), acc.split('-')[0], acc.split('-')[1], ch) accounts.append(acc) return accounts def __show_profiles(self, people): if not statuses: print "There are no profiles to show" return if people.code > 0: print people.errmsg return for p in people: protected = '<protected>' if p.protected else '' following = '<following>' if p.following else '' header = "@%s (%s) %s %s" % (p.username, p.fullname, following, protected) print header print '-' * len(header) print "URL: %s" % p.url print "Location: %s" % p.location print "Bio: %s" % p.bio if p.last_update: print "Last: %s" % p.last_update print '' def __show_statuses(self, statuses): if not statuses: print "There are no statuses to show" return if statuses.code > 0: print statuses.errmsg return count = 1 for status in statuses: text = status.text.replace('\n', ' ') inreply = '' client = '' if status.in_reply_to_user: inreply = ' in reply to %s' % status.in_reply_to_user if status.source: client = ' from %s' % status.source print "%d. @%s: %s (id: %s)" % (count, status.username, text, status.id_) print "%s%s%s" % (status.datetime, client, inreply) if status.reposted_by: users = '' for u in status.reposted_by: users += u + ' ' print 'Retweeted by %s' % status.reposted_by print count += 1 def __process_login(self, acc): if not self.core.has_stored_passwd(acc): passwd = self.__build_password_menu(acc) username = acc.split('-')[0] protocol = acc.split('-')[1] self.core.register_account(username, protocol, passwd) rtn = self.core.login(acc) if rtn.code > 0: print rtn.errmsg return auth_obj = rtn.items if auth_obj.must_auth(): print "Please visit %s, authorize Turpial and type the pin returned" % auth_obj.url pin = self.__user_input('Pin: ') self.core.authorize_oauth_token(acc, pin) rtn = self.core.auth(acc) if rtn.code > 0: print rtn.errmsg else: print 'Logged in with account %s' % acc.split('-')[0] def default(self, line): print '\n'.join(['Command not found.', INTRO[1], INTRO[2]]) def emptyline(self): pass def do_account(self, arg): if not self.__validate_arguments(ARGUMENTS['account'], arg): self.help_account(False) return False if arg == 'add': username = raw_input('Username: '******'Password: '******'Remember password') protocol = self.__build_protocols_menu() acc_id = self.core.register_account(username, protocol, password, remember) print 'Account added' if len(self.core.list_accounts()) == 1: self.__add_first_account_as_default() elif arg == 'edit': if not self.__validate_default_account(): return False password = getpass.unix_getpass('New Password: '******'-')[0] protocol = self.account.split('-')[1] remember = self.__build_confirm_menu('Remember password') self.core.register_account(username, protocol, password, remember) print 'Account edited' elif arg == 'delete': if not self.__validate_accounts(): return False account = self.__build_accounts_menu() conf = self.__build_confirm_menu('Do you want to delete account %s?' % account) if not conf: print 'Command cancelled' return False del_all = self.__build_confirm_menu('Do you want to delete all data?') self.core.unregister_account(account, del_all) if self.account == account: self.account = None print 'Account deleted' elif arg == 'change': if not self.__validate_accounts(): return False self.__build_change_account_menu() elif arg == 'list': self.__show_accounts() elif arg == 'default': print "Your default account is %s in %s" % ( self.account.split('-')[0], self.account.split('-')[1]) def help_account(self, desc=True): text = 'Manage user accounts' if not desc: text = '' print '\n'.join([text, 'Usage: account <arg>\n', 'Possible arguments are:', ' add:\t\t Add a new user account', ' edit:\t\t Edit an existing user account', ' delete:\t Delete a user account', ' list:\t\t Show all registered accounts', ' default:\t Show default account', ]) def do_login(self, arg): if not self.__validate_accounts(): return False _all = True if len(self.core.list_accounts()) > 1: _all = self.__build_confirm_menu('Do you want to login with all available accounts?') if _all: work = False for acc in self.core.list_accounts(): if self.core.is_account_logged_in(acc): continue work = True self.__process_login(acc) if not work: print "Already logged in with all available accounts" else: acc = self.__build_accounts_menu() self.__process_login(acc) def help_login(self): print 'Login with one or many accounts' def do_profile(self, arg): if not self.__validate_arguments(ARGUMENTS['profile'], arg): self.help_profile(False) return False if not self.__validate_default_account(): return False if arg == 'me': profile = self.core.get_own_profile(self.account) if profile is None: print 'You must be logged in' else: self.__show_profiles(profile) elif arg == 'user': username = raw_input('Type the username: '******'': print 'You must specify a username' return False profile = self.core.get_user_profile(self.account, username) if profile is None: print 'You must be logged in' else: self.__show_profiles(profile) elif arg == 'update': args = {} name = raw_input('Type your name (ENTER for none): ') bio = raw_input('Type your bio (ENTER for none): ') url = raw_input('Type your url (ENTER for none): ') location = raw_input('Type your location (ENTER for none): ') if name != '': args['name'] = name if bio != '': args['description'] = bio if url != '': args['url'] = url if location != '': args['location'] = location result = self.core.update_profile(self.account, args) if result.code > 0: print result.errmsg else: print 'Profile updated' def help_profile(self, desc=True): text = 'Manage user profile' if not desc: text = '' print '\n'.join([text, 'Usage: profile <arg>\n', 'Possible arguments are:', ' me:\t\t Show own profile', ' user:\t\t Show profile for a specific user', ' update:\t Update own profile', ]) def do_status(self, arg): if not self.__validate_default_account(): return False if not self.__validate_arguments(ARGUMENTS['status'], arg): self.help_status(False) return False if arg == 'update': message = self.__build_message_menu() if not message: print 'You must to write something' return False broadcast = self.__build_confirm_menu('Do you want to post the message in all available accounts?') if broadcast: for acc in self.core.list_accounts(): rtn = self.core.update_status(acc, message) if rtn.code > 0: print rtn.errmsg else: print 'Message posted in account %s' % acc.split('-')[0] else: rtn = self.core.update_status(self.account, message) if rtn.code > 0: print rtn.errmsg else: print 'Message posted in account %s' % self.account.split('-')[0] elif arg == 'reply': reply_id = raw_input('Status ID: ') if reply_id == '': print "You must specify a valid id" return False message = self.__build_message_menu() if not message: print 'You must to write something' return False rtn = self.core.update_status(self.account, message, reply_id) if rtn.code > 0: print rtn.errmsg else: print 'Reply posted in account %s' % self.account.split('-')[0] elif arg == 'delete': status_id = raw_input('Status ID: ') if status_id == '': print "You must specify a valid id" return False rtn = self.core.destroy_status(self.account, status_id) if rtn.code > 0: print rtn.errmsg else: print 'Status deleted' elif arg == 'conversation': status_id = raw_input('Status ID: ') if status_id == '': print "You must specify a valid id" return False rtn = self.core.get_conversation(self.account, status_id) if rtn.code > 0: print rtn.errmsg else: self.__show_statuses(rtn) def help_status(self, desc=True): text = 'Manage statuses for each protocol' if not desc: text = '' print '\n'.join([text, 'Usage: status <arg>\n', 'Possible arguments are:', ' update:\t Update status ', ' delete:\t Delete status', ' conversation:\t Show related tweets as conversation', ]) def do_column(self, arg): if not self.__validate_default_account(): return False lists = self.core.list_columns(self.account) if arg == '': self.help_column(False) elif arg == 'list': if len(lists) == 0: print "No column available. Maybe you need to login" return False print "Available columns:" for li in lists: print " %s" % li elif arg == 'public': rtn = self.core.get_public_timeline(self.account) self.__show_statuses(rtn) else: if len(lists) == 0: print "No column available. Maybe you need to login" return False if arg in lists: rtn = self.core.get_column_statuses(self.account, arg) self.__show_statuses(rtn) else: print "Invalid column '%s'" % arg def help_column(self, desc=True): text = 'Show user columns' if not desc: text = '' print '\n'.join([text, 'Usage: column <arg>\n', 'Possible arguments are:', ' list:\t\t List all available columns for that account', ' timeline:\t Show timeline', ' replies:\t Show replies', ' directs:\t Show directs messages', ' favorites:\t Show statuses marked as favorites', ' public:\t Show public timeline', ' <list_id>:\t Show statuses for the user list with id <list_id>', ]) def do_friend(self, arg): if not self.__validate_default_account(): return False if not self.__validate_arguments(ARGUMENTS['friend'], arg): self.help_friend(False) return False if arg == 'list': friends = self.core.get_friends(self.account) if friends.code > 0: print rtn.errmsg return False if len(friends) == 0: print "Hey! What's wrong with you? You've no friends" return False print "Friends list:" for fn in friends: print "+ @%s (%s)" % (fn.username, fn.fullname) elif arg == 'follow': username = raw_input('Username: '******'': print "You must specify a valid user" return False rtn = self.core.follow(self.account, username) if rtn.code > 0: print rtn.errmsg return False print "Following %s" % user elif arg == 'unfollow': username = raw_input('Username: '******'': print "You must specify a valid user" return False rtn = self.core.unfollow(self.account, username) if rtn.code > 0: print rtn.errmsg return False print "Not following %s" % user elif arg == 'block': username = raw_input('Username: '******'': print "You must specify a valid user" return False rtn = self.core.block(self.account, username) if rtn.code > 0: print rtn.errmsg return False print "Blocking user %s" % username elif arg == 'unblock': username = raw_input('Username: '******'': print "You must specify a valid user" return False rtn = self.core.unblock(self.account, username) if rtn.code > 0: print rtn.errmsg return False print "Unblocking user %s" % username elif arg == 'spammer': username = raw_input('Username: '******'': print "You must specify a valid user" return False rtn = self.core.report_spam(self.account, username) if rtn.code > 0: print rtn.errmsg return False print "Reporting user %s as spammer" % username elif arg == 'check': username = raw_input('Username: '******'': print "You must specify a valid user" return False rtn = self.core.is_friend(self.account, username) if rtn.code > 0: print rtn.errmsg return False if rtn.items: print "%s is following you" % username else: print "%s is not following you" % username def help_friend(self, desc=True): text = 'Manage user friends' if not desc: text = '' print '\n'.join([text, 'Usage: friend <arg>\n', 'Possible arguments are:', ' list:\t\t List all friends', ' follow:\t Follow user', ' unfollow:\t Unfollow friend', ' block:\t Block user', ' unblock:\t Unblock user', ' spammer:\t Report user as spammer', ' check:\t Verify if certain user is following you', ]) def do_direct(self, arg): if not self.__validate_default_account(): return False if not self.__validate_arguments(ARGUMENTS['direct'], arg): self.help_direct(False) return False if arg == 'send': username = raw_input('Username: '******'': print "You must specify a valid user" return False message = self.__build_message_menu() if not message: print 'You must to write something' return False rtn = self.core.send_direct(self.account, username, message) if rtn.code > 0: print rtn.errmsg else: print 'Direct message sent' elif arg == 'delete': dm_id = raw_input('Direct message ID: ') if dm_id == '': print "You must specify a valid id" return False rtn = self.core.destroy_direct(self.account, dm_id) if rtn.code > 0: print rtn.errmsg else: print 'Direct message deleted' def help_direct(self, desc=True): text = 'Manage user direct messages' if not desc: text = '' print '\n'.join([text, 'Usage: direct <arg>\n', 'Possible arguments are:', ' send:\t\t Send direct message', ' delete:\t Destroy direct message', ]) def do_favorite(self, arg): if not self.__validate_default_account(): return False if not self.__validate_arguments(ARGUMENTS['favorite'], arg): self.help_status(False) return False if arg == 'mark': status_id = raw_input('Status ID: ') if status_id == '': print "You must specify a valid id" return False rtn = self.core.mark_favorite(self.account, status_id) if rtn.code > 0: print rtn.errmsg else: print 'Status marked as favorite' elif arg == 'unmark': status_id = raw_input('Status ID: ') if status_id == '': print "You must specify a valid id" return False rtn = self.core.unmark_favorite(self.account, status_id) if rtn.code > 0: print rtn.errmsg else: print 'Status unmarked as favorite' def help_favorite(self, desc=True): text = 'Manage favorite marks of statuses' if not desc: text = '' print '\n'.join([text, 'Usage: direct <arg>\n', 'Possible arguments are:', ' mark:\t\t Mark a status as favorite', ' unmark:\t Remove favorite mark from a status', ]) def do_search(self, arg=None): if not self.__validate_default_account(): return False if arg: self.help_search() return False query = raw_input('Type what you want to search for: ') rtn = self.core.search(self.account, query) self.__show_statuses(rtn) def help_search(self): print 'Search for a pattern' def do_trends(self, arg=None): if not self.__validate_default_account(): return False if arg: self.help_trends() return False trends = self.core.trends(self.account) if trends.code > 0: print trends.errmsg return False for trend in trends: print trend.title print "=" * len(trend.title) for topic in trend.items: promoted = '' if topic.promoted: promoted = '*' print "%s%s |" % (topic.name, promoted), print def help_trends(self): print 'Show global and local trends' def do_EOF(self, line): return self.do_exit('') def do_exit(self, line=None): print self.log.debug('Bye') return True def help_help(self): print 'Show help. Dah!' def help_exit(self): print 'Close the application' def help_EOF(self): print 'Close the application' def show_shorten_url(self, text): print "URL Cortada:", text