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 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')