如何在django中使用rapportive.py

Abi*_*ham 5 python email django

我在我的python django项目中使用rapportive.我收到一个错误说no information found

该文件如下:

import sys
import logging
from textwrap import dedent

# Requests, from python-requests.org
from docopt import docopt


# pylint: disable=C0103
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# pylint: enable=C0103

STATUS_URL = 'https://rapportive.com/login_status?user_email={0}'
URL = 'https://profiles.rapportive.com/contacts/email/{0}'


# pylint: disable=R0903
class Profile(object):

    """
Profile object for collecting profile information before printing
"""

    def __init__(self, person):
        if person:
            self.name = person.get('name')
            self.jobinfo = [
                (occupation.get('job_title'), occupation.get('company'))
                for occupation in person.get('occupations', [])
            ]

            self.memberships = [
                (membership.get('site_name'), membership.get('profile_url'))
                for membership in person.get('memberships', [])
            ]
            self.success = person.get('success')

    def __str__(self):
        return dedent("""
Name: {0}
{1}
{2}
""").format(
            self.name,
            "\n".join(
                "{0} {1}".format(title, company)
                for title, company in self.jobinfo),
            "\n".join(
                "\t{0} {1}".format(site_name, url)
                for site_name, url in self.memberships)
        )


def request(email):
    '''
rapportive_request(email): Sends a query to the undocumented Rapportive API
Returns the response as a dict
'''
    status_url = STATUS_URL.format(email)
    response = requests.get(status_url).json()
    session_token = response.get('session_token')
    # fail gracefully if there is an error
    if 'error' in response:
        return response['error']
    elif response['status'] == 200 and session_token:
        logger.debug('Session token: {0}'.format(session_token))
        url = URL.format(email)
        headers = {'X-Session-Token': session_token}
        response = requests.get(url, headers=headers).json()
        if response.get('success') != 'nothing_useful':
        return Profile(response.get('contact'))
    return {}


def ___process_email(email, output_file=None):
    """
Find email address in rapportive and print profile info
if found
"""
    profile = request(email)
    if profile and profile.success != 'nothing_useful':
        logger.info('Found match for {0}'.format(email))
        print(profile)
        if output_file:
            output_file.write(summary + '\n')
    else:
        print("No information found\n")


def main():
    '''
main(): Expect a list of email addresses via stdin
and check them with the Rapportive API
'''
    options = docopt(__doc__, version="0.1.0")

    logging.basicConfig(
        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        datefmt='%m-%d %H:%M')

    if options["--verbose"]:
        logger.setLevel(logging.DEBUG)

    email = options.get("--email")
    args = [email] if email else [line.rstrip() for line in sys.stdin]
    output = options.get("--output")
    output = output and open(output, "w")
    for arg in args:
        ___process_email(arg, output)


if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)