Ejemplo n.º 1
0
    def __init__(self):
        """ Initialize Lusha API client.
        
        Though it uses ERGAL for most request processing,
        this class has blocks to parse out the desired data from
        returned records.

        This initialization assumes that the API profile for Lusha
        has already been created and is stored in a local ergal.db
        sqlite database file.

        """
        self.profile = Profile('Lusha API', base='https://api.lusha.co')
Ejemplo n.º 2
0
    def __init__(self):
        """ Initialize the DiscoverOrg API client.

        Also integrates pretty directly with ERGAL, but has some
        utility code to parse responses of different types.
        
        """
        self.profile = Profile('DiscoverOrg API',
                               base='https://papi.discoverydb.com/papi/v1')

        session_key = self._get_session_key()
        self.profile.set_auth('key-header',
                              key=session_key,
                              name='X-AUTH-TOKEN')
Ejemplo n.º 3
0
def build_profile():
    profile = Profile(
        'httpbin',
        base='https://httpbin.org',
        test=True)

    return profile
Ejemplo n.º 4
0
    async def test_init(self):
        profile =  build_profile()
        assert type(profile) is Profile

        profile = Profile(1, base=1, test=True)
        assert profile.name == 'default'
        assert profile.base == 'default'

        profile.db.close()
Ejemplo n.º 5
0
    async def test_update(self):
        profile = build_profile()

        profile.base = 'http://httpbin.org'
        profile.update()
        del profile

        profile = Profile('httpbin', test=True)
        assert profile.base == 'http://httpbin.org'

        profile.db.close()
Ejemplo n.º 6
0
class DiscoverOrgClient:
    def __init__(self):
        """ Initialize the DiscoverOrg API client.

        Also integrates pretty directly with ERGAL, but has some
        utility code to parse responses of different types.
        
        """
        self.profile = Profile('DiscoverOrg API',
                               base='https://papi.discoverydb.com/papi/v1')

        session_key = self._get_session_key()
        self.profile.set_auth('key-header',
                              key=session_key,
                              name='X-AUTH-TOKEN')

    def _get_session_key(self):
        """ Get a session key. """
        auth_url = 'https://papi.discoverydb.com/papi/login'
        auth_data = {
            'username': os.environ['DO_USERNAME'],
            'password': os.environ['DO_PASSWORD'],
            'partnerKey': os.environ['DO_KEY']
        }

        response = requests.post(auth_url, data=json.dumps(auth_data))
        session_key = response.headers['X-AUTH-TOKEN']

        return session_key

    def search(self, account):
        """ Search for contacts.

        Using the properties of an account object, a search
        is made in DiscoverOrg for all contacts under the
        given account. All of these records are returned in
        a list of model-friendly dictionaries.

        Arguments:
            app.Account:account -- an account object.
        
        Returns:
            list:contacts -- a list of contact dicts.
        
        """
        response = self.profile.call('Person Search',
                                     headers={
                                         'X-AUTH-TOKEN':
                                         str(self._get_session_key()),
                                         'Accept':
                                         'application/json',
                                         'Content-Type':
                                         'application/json'
                                     },
                                     data=json.dumps({
                                         'companyCriteria': {
                                             'websiteUrls': [account.domain]
                                         }
                                     }))

        if len(response['content']) > 0:
            results = response['content']
        else:
            return

        classes = ['A', 'B', 'C']
        titles = [[
            'SENIOR VICE PRESIDENT', 'SVP', 'VICE PRESIDENT', 'VP',
            'PRESIDENT', 'CHIEF', 'DIRECTOR', 'HEAD', 'LEAD'
        ], ['SENIOR MANAGER', 'MANAGER', 'COORDINATOR', 'BUSINESS PARTNER'],
                  [
                      'ANALYST', 'GENERALIST', 'RECRUITER', 'ASSISTANT',
                      'SPECIALIST'
                  ]]

        contacts = []
        for result in results:
            contact = {}

            contact['category'] = 'enrich'
            contact['status'] = 'enrich'
            contact['dorgid'] = result['id']
            contact['name'] = result['fullName']
            if 'officeTelNumber' in result:
                contact['direct'] = result['officeTelNumber']
            if 'mobileTelNumber' in result:
                contact['mobile'] = result['mobileTelNumber']
            if 'email' in result:
                contact['email'] = result['email']

            if 'title' in result:
                contact['title'] = result['title']
            else:
                contact['title'] = 'Unknown'

            for sect in titles:
                for title in sect:
                    if title in contact['title'].upper():
                        contact['rating'] = classes[titles.index(sect)]
                        break
                    else:
                        continue

            if not 'rating' in contact:
                contact['rating'] = 'C'

            contact['status'] = 'enrich'

            contacts.append(contact)

        return contacts
Ejemplo n.º 7
0
class LushaClient:
    def __init__(self):
        """ Initialize Lusha API client.
        
        Though it uses ERGAL for most request processing,
        this class has blocks to parse out the desired data from
        returned records.

        This initialization assumes that the API profile for Lusha
        has already been created and is stored in a local ergal.db
        sqlite database file.

        """
        self.profile = Profile('Lusha API', base='https://api.lusha.co')

    def enrich(self, contact):
        """ Enrich a contact record.

        Using the properties of a contact object, a search
        is made in Lusha for direct, mobile, and email. If any
        data is added, the contact is updated.

        Arguments:
            app.Contact:contact -- a contact object.

        """
        name = contact.name.split()
        if len(name) > 2:
            name.pop(1)

        response = self.profile.call('Enrich Person',
                                     params={
                                         'firstName': name[0],
                                         'lastName': name[1],
                                         'company': contact.account.name,
                                         'property': 'phoneNumbers'
                                     })

        if 'errors' in response:
            return

        Record.objects.create(source='Lusha',
                              content=response,
                              context='LushaClient: enrich')

        if 'phoneNumbers' in response['data']:
            numbers = response['data']['phoneNumbers']
        else:
            return

        if not 'emailAddresses' in response['data']:
            emails = response['data']['emailAddresses']
        else:
            emails = None

        if not contact.direct:
            contact.direct = numbers[0]['localizedNumber']

        if not contact.mobile:
            if len(numbers) > 1:
                contact.mobile = numbers[1]['localizedNumber']

        if emails:
            contact.email = emails[0]['email']

        contact.save()
Ejemplo n.º 8
0
    async def test_delete(self):
        profile = build_profile()
        profile.delete()

        profile = Profile('httpbin', 'http://httpbin.org', test=True)
        assert profile.base == 'http://httpbin.org'