Ejemplo n.º 1
0
    def __init__(self,
                 username,
                 account_id,
                 protocol_id,
                 password,
                 auth,
                 config=None):
        self.id_ = account_id  # username-protocol_id
        self.username = username
        self.protocol_id = protocol_id

        if protocol_id == ProtocolType.TWITTER:
            self.protocol = twitter.Main(username, self.id_, auth)
        elif protocol_id == ProtocolType.IDENTICA:
            self.protocol = identica.Main(username, self.id_, auth)

        self.profile = Profile()
        self.profile.username = username
        self.profile.password = password
        self.friends = None
        self.columns = []
        self.lists = None
        self.token = None
        self.logged_in = LoginStatus.NONE
        if config:
            self.config = config
        else:
            self.config = AccountConfig(account_id, password)
Ejemplo n.º 2
0
    def save(self):
        """
        Save to disk the configuration and credentials for the account. If the
        account hasn't been authenticated it will raise an
        :class:`libturpial.exceptions.AccountNotAuthenticated` exception.
        """
        if not self.is_authenticated():
            raise AccountNotAuthenticated

        self.config = AccountConfig(self.id_)
        token = self.get_oauth_token()
        if token:
            self.config.save_oauth_credentials(token.key, token.secret)
Ejemplo n.º 3
0
    def load(self, account_id):
        cfg = AccountConfig(account_id)
        auth = cfg.read_section("OAuth")
        username = cfg.read("Login", "username")
        protocol = cfg.read("Login", "protocol")
        p = cfg.revert(cfg.read("Login", "password"), username)

        if self.__accounts.has_key(account_id):
            self.log.debug("Account %s is already registered" % account_id)
        else:
            account = Account(username, account_id, protocol, p, auth, cfg)
            timeout = int(self.config.read("Advanced", "socket-timeout"))
            account.protocol.timeout = timeout
            self.log.debug("Using %i sec for socket timeout in account %s" % (account.protocol.timeout, account_id))
            self.__accounts[account_id] = account
            self.log.debug("Account %s loaded successfully" % account_id)
        return account_id
Ejemplo n.º 4
0
    def load(self, account_id):
        cfg = AccountConfig(account_id)
        auth = cfg.read_section('OAuth')
        username = cfg.read('Login', 'username')
        protocol = cfg.read('Login', 'protocol')
        p = cfg.revert(cfg.read('Login', 'password'), username)

        if account_id in self.__accounts:
            self.log.debug('Account %s is already registered' % account_id)
        else:
            account = Account(username, account_id, protocol, p, auth, cfg)
            timeout = int(self.config.read('Advanced', 'socket-timeout'))
            account.protocol.timeout = timeout
            self.log.debug('Using %i sec for socket timeout in account %s' % (
                account.protocol.timeout, account_id))
            self.__accounts[account_id] = account
            self.log.debug('Account %s loaded successfully' % account_id)
        return account_id
Ejemplo n.º 5
0
 def change_id(self, original_id, destination_id):
     account = self.get(original_id)
     new_id = "%s-%s" % (destination_id, account.protocol_id)
     self.unregister(original_id, True)
     account.username = destination_id
     account.profile.username = destination_id
     account.config = AccountConfig(new_id, "")
     account.store_token()
     account.id_ = new_id
     self.__accounts[new_id] = account
     return new_id
Ejemplo n.º 6
0
    def load(account_id):
        """
        Return the account object associated to *account_id* loaded from
        existing configuration. If the *account_id* does not correspond to a
        valid account it returns a
        :class:`libturpial.exceptions.ErrorLoadingAccount` exception.

        If credentials in configuration file are empty it returns a
        :class:`libturpial.exceptions.EmptyOAuthCredentials` exception.
        """
        if not AccountConfig.exists(account_id):
            raise ErrorLoadingAccount("Account has no stored credentials")

        username = get_username_from(account_id)
        protocol_id = get_protocol_from(account_id)

        account = Account.new(protocol_id, username)
        account.config = AccountConfig(account_id)
        key, secret = account.config.load_oauth_credentials()
        account.setup_user_credentials(account.id_, key, secret)
        account.fetch()
        return account
Ejemplo n.º 7
0
    def __init__(self, username, account_id, protocol_id, password, auth, config=None):
        self.id_ = account_id # username-protocol_id
        self.username = username
        self.protocol_id = protocol_id

        if protocol_id == ProtocolType.TWITTER:
            self.protocol = twitter.Main(username, self.id_, auth)
        elif protocol_id == ProtocolType.IDENTICA:
            self.protocol = identica.Main(username, self.id_, auth)

        self.profile = Profile()
        self.profile.username = username
        self.profile.password = password
        self.friends = None
        self.columns = []
        self.lists = None
        self.token = None
        self.logged_in = LoginStatus.NONE
        if config:
            self.config = config
        else:
            self.config = AccountConfig(account_id, password)
Ejemplo n.º 8
0
    def load(self, account_id):
        cfg = AccountConfig(account_id)
        auth = cfg.read_section('OAuth')
        username = cfg.read('Login', 'username')
        protocol = cfg.read('Login', 'protocol')
        p = cfg.revert(cfg.read('Login', 'password'), username)

        if self.__accounts.has_key(account_id):
            self.log.debug('Account %s is already registered' % account_id)
        else:
            account = Account(username, account_id, protocol, p, auth, cfg)
            timeout = int(self.config.read('Advanced', 'socket-timeout'))
            account.protocol.timeout = timeout
            self.log.debug('Using %i sec for socket timeout in account %s' %
                           (account.protocol.timeout, account_id))
            self.__accounts[account_id] = account
            self.log.debug('Account %s loaded successfully' % account_id)
        return account_id
Ejemplo n.º 9
0
class Account(object):
    """
    This class represents an user account and holds all it related methods.
    This is done thanks to one
    :class:`libturpial.lib.interfaces.protocol.Protocol`
    instance associated to the user account that handles all the dirty work
    against the service (Twitter, Identi.ca, etc) as well as one
    :class:`libturpial.api.models.profile.Profile` model that store the user
    details.

    This is the class you must instanciate if you want to handle/authenticate
    a user account.

    *Account* let you perform three actions to build an account: create a new
    account from scratch, create a new account from params and load a
    previously registered account. To create a new account from scratch do:

    >>> account = Account.new('twitter')

    If you know the username too, then you can pass it as argument:

    >>> account = Account.new('twitter', 'username')

    At this point, that account is not a valid account yet because it
    hasn't been authenticated. You should do the authentication by yourself.
    This is, request OAuth access:

    >>> url = account.request_oauth_access()

    That method will return an URL that your user must visit to authorize the
    app. After that, you must to ask for the PIN returned by the service and
    execute:

    >>> account.authorize_oauth_access('the_pin')

    And voilá! You now have a valid and fully authenticated account ready to be
    registered in :class:`libturpial.api.core.Core`.

    But *Account* let you create accounts passing all the params needed for
    the OAuth authentication. If you already know those params (user key,
    user secret and PIN) then you just need to execute:

    >>> account = Account.new_from_params('twitter', 'username', 'key', \
                                          'secret', 'the_pin')

    And you will have a valid and fully authenticated account ready to be
    registered in :class:`libturpial.api.core.Core` too.

    Now, what if you did all this process before and registered the account
    in :class:`libturpial.api.core.Core`? Well, you just need to load the
    account then:

    >>> account = Account.load('username-twitter')

    And you will have an account already authenticated and ready to be used.

    From this point you can use the method described here to handle the
    account object.
    """

    def __init__(self, protocol_id, username=None):
        self.id_ = None
        self.config = None
        self.protocol_id = protocol_id

        if username:
            self.__setup(username)

        self.columns = []
        self.profile = None
        self.friends = None
        self.lists = None

        if self.protocol_id == Protocol.TWITTER:
            self.protocol = twitter.Main()
        elif self.protocol_id == Protocol.IDENTICA:
            self.protocol = identica.Main()

    def __repr__(self):
        return "libturpial.api.models.Account %s-%s" % (self.username, self.protocol_id)

    def __setup(self, username):
        self.id_ = build_account_id(username, self.protocol_id)
        self.username = username

    @staticmethod
    def new(protocol_id, username=None):
        """
        Return a new account object associated to the protocol identified by
        *protocol_id*. If *username* is not None it will build the account_id
        for the account.

        This account is empty and must be authenticated before it can be
        registered in :class:`libturpial.api.core.Core`.

        .. warning::
            None information is stored at disk at this point.
        """
        account = Account(protocol_id, username)
        return account

    @staticmethod
    def new_from_params(protocol_id, username, key, secret, verifier):
        """
        Return a new account object associated to the protocol identified by
        *protocol_id* and authenticated against the respective service
        (Twitter, Identi.ca, etc) using *username*, *key*, *secret* and
        *verifier* (aka PIN).

        This account is authenticated after creation, so it can be registered
        in :class:`libturpial.api.core.Core` immediately.

        .. warning::
            None information is stored at disk at this point.
        """
        account = Account(protocol_id, username)
        account.setup_user_credentials(account.id_, key, secret)
        return account

    @staticmethod
    def load(account_id):
        """
        Return the account object associated to *account_id* loaded from
        existing configuration. If the *account_id* does not correspond to a
        valid account it returns a
        :class:`libturpial.exceptions.ErrorLoadingAccount` exception.

        If credentials in configuration file are empty it returns a
        :class:`libturpial.exceptions.EmptyOAuthCredentials` exception.
        """
        if not AccountConfig.exists(account_id):
            raise ErrorLoadingAccount("Account has no stored credentials")

        username = get_username_from(account_id)
        protocol_id = get_protocol_from(account_id)

        account = Account.new(protocol_id, username)
        account.config = AccountConfig(account_id)
        key, secret = account.config.load_oauth_credentials()
        account.setup_user_credentials(account.id_, key, secret)
        account.fetch()
        return account

    def request_oauth_access(self):
        """
        Ask for an OAuth token. Return an URL that must be visited for the user
        in order to authenticate the app.
        """
        return self.protocol.request_token()

    def authorize_oauth_access(self, pin):
        """
        Take the *pin* returned by OAuth service and authenticate the token
        requested with *request_oauth_access*
        """
        self.profile = self.protocol.authorize_token(pin)
        self.__setup(self.profile.username)

    # TODO: Return True if success?
    def save(self):
        """
        Save to disk the configuration and credentials for the account. If the
        account hasn't been authenticated it will raise an
        :class:`libturpial.exceptions.AccountNotAuthenticated` exception.
        """
        if not self.is_authenticated():
            raise AccountNotAuthenticated

        self.config = AccountConfig(self.id_)
        token = self.get_oauth_token()
        if token:
            self.config.save_oauth_credentials(token.key, token.secret)

    def fetch(self):
        """
        Retrieve the user profile information and return the id of the account
        on success. This method authenticate the account.
        """
        self.profile = self.protocol.verify_credentials()
        self.lists = self.protocol.get_lists(self.profile.username)

        self.columns = [
            Column(self.id_, ColumnType.TIMELINE),
            Column(self.id_, ColumnType.REPLIES),
            Column(self.id_, ColumnType.DIRECTS),
            Column(self.id_, ColumnType.SENT),
            Column(self.id_, ColumnType.FAVORITES)] + self.lists
        return self.id_

    def fetch_friends(self):
        """
        Retrieve all the friends for the user and return an array of string
        where each element correspond to the user_id of the friend.
        """
        self.friends = self.protocol.get_following(only_id=True)
        return self.friends

    def get_columns(self):
        """
        Return an array of :class:`libturpial.api.models.column.Column` with
        all the available columns for the user
        """
        return self.columns

    def get_list_id(self, list_name):
        if not self.lists:
            return None

        for li in self.lists:
            if li.slug == list_name:
                return li.id_
        return None

    # TODO: Return True?
    def purge_config(self):
        """
        Delete all config files related to this account
        """
        self.config.dismiss()

    # TODO: Return True?
    def delete_cache(self):
        """
        Delete all files cached for this account
        """
        self.config.delete_cache()

    def get_cache_size(self):
        """
        Return disk space used by cache in bytes
        """
        return self.config.calculate_cache_size()

    def is_authenticated(self):
        """
        Return `True` if the current account has been logged in, `False`
        otherwise
        """
        return self.profile is not None and self.id_ is not None

    def update_profile(self, fullname=None, url=None, bio=None, location=None):
        """
        Update the *fullname*, *url*, *bio* or *location* of the user profile.
        You may specify one or more arguments. Return an
        :class:`libturpial.api.models.profile.Profile` object containing the
        user profile
        """
        self.profile = self.protocol.update_profile(fullname, url, bio,
                                                    location)
        return self.profile

    def __getattr__(self, name):
        try:
            return getattr(self.protocol, name)
        except:
            try:
                return getattr(self.profile, name)
            except:
                raise AttributeError
Ejemplo n.º 10
0
class Account:
    def __init__(self, username, account_id, protocol_id,
                 password, auth, config=None):
        self.id_ = account_id  # username-protocol_id
        self.username = username
        self.protocol_id = protocol_id

        if protocol_id == ProtocolType.TWITTER:
            self.protocol = twitter.Main(username, self.id_, auth)
        elif protocol_id == ProtocolType.IDENTICA:
            self.protocol = identica.Main(username, self.id_, auth)

        self.profile = Profile()
        self.profile.username = username
        self.profile.password = password
        self.friends = None
        self.columns = []
        self.lists = None
        self.token = None
        self.logged_in = LoginStatus.NONE
        if config:
            self.config = config
        else:
            self.config = AccountConfig(account_id, password)

    def auth(self):
        self.profile = self.protocol.auth(self.profile.username,
                                          self.profile.password)
        self.lists = self.protocol.get_lists(self.profile.username)

        self.columns = [ColumnType.TIMELINE, ColumnType.REPLIES,
                        ColumnType.DIRECTS, ColumnType.SENT,
                        ColumnType.FAVORITES]
        for li in self.lists:
            self.columns.append(li.name)
        return self.id_

    def get_friends(self):
        self.friends = self.protocol.get_friends()
        return self.friends

    def get_columns(self):
        return self.columns

    def get_list_id(self, list_name):
        for li in self.lists:
            if li.name == list_name:
                return li.id_
        return None

    def update(self, passwd):
        self.profile.password = passwd

    def set_profile(self, profile):
        self.profile = profile

    def remove(self, delete_all):
        if delete_all:
            self.config.dismiss()

    def authorize_oauth_token(self, pin):
        self.token = self.authorize_token(pin)
        self.store_token()

    def store_token(self):
        self.config.write('OAuth', 'key', self.token.key)
        self.config.write('OAuth', 'secret', self.token.secret)
        self.config.write('OAuth', 'verifier', self.token.verifier)

    def delete_cache(self):
        self.config.delete_cache()

    def get_cache_size(self):
        return self.config.calculate_cache_size()

    def __getattr__(self, name):
        try:
            return getattr(self.protocol, name)
        except:
            try:
                return getattr(self.profile, name)
            except:
                raise AttributeError
Ejemplo n.º 11
0
class Account:
    def __init__(self,
                 username,
                 account_id,
                 protocol_id,
                 password,
                 auth,
                 config=None):
        self.id_ = account_id  # username-protocol_id
        self.username = username
        self.protocol_id = protocol_id

        if protocol_id == ProtocolType.TWITTER:
            self.protocol = twitter.Main(username, self.id_, auth)
        elif protocol_id == ProtocolType.IDENTICA:
            self.protocol = identica.Main(username, self.id_, auth)

        self.profile = Profile()
        self.profile.username = username
        self.profile.password = password
        self.friends = None
        self.columns = []
        self.lists = None
        self.token = None
        self.logged_in = LoginStatus.NONE
        if config:
            self.config = config
        else:
            self.config = AccountConfig(account_id, password)

    def auth(self):
        self.profile = self.protocol.auth(self.profile.username,
                                          self.profile.password)
        self.lists = self.protocol.get_lists(self.profile.username)

        self.columns = [
            ColumnType.TIMELINE, ColumnType.REPLIES, ColumnType.DIRECTS,
            ColumnType.SENT, ColumnType.FAVORITES
        ]
        for li in self.lists:
            self.columns.append(li.name)
        return self.id_

    def get_friends(self):
        self.friends = self.protocol.get_friends()
        return self.friends

    def get_columns(self):
        return self.columns

    def get_list_id(self, list_name):
        for li in self.lists:
            if li.name == list_name:
                return li.id_
        return None

    def update(self, passwd):
        self.profile.password = passwd

    def set_profile(self, profile):
        self.profile = profile

    def remove(self, delete_all):
        if delete_all:
            self.config.dismiss()

    def authorize_oauth_token(self, pin):
        self.token = self.authorize_token(pin)
        self.store_token()

    def store_token(self):
        self.config.write('OAuth', 'key', self.token.key)
        self.config.write('OAuth', 'secret', self.token.secret)
        self.config.write('OAuth', 'verifier', self.token.verifier)

    def delete_cache(self):
        self.config.delete_cache()

    def get_cache_size(self):
        return self.config.calculate_cache_size()

    def __getattr__(self, name):
        try:
            return getattr(self.protocol, name)
        except:
            try:
                return getattr(self.profile, name)
            except:
                raise AttributeError