Exemplo n.º 1
0
def get_bible_passage(query):
    BASE_URL = 'http://nasb.literalword.com/'
    url = BASE_URL
    params = {
        'q' : query,
    }
    response = requests.get(url, params)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        text_container = soup.select('.bMeatWrapper')
        if len(text_container):
            container = text_container
            # remove <p> tags
            for p in container.find_all('p'):
                p.decompose()
            html = u'%s' % container
            text = html2markdown(html)
        else:
            meta_description_tag = soup.meta.find(attrs={'name' : 'description',})
            text = meta_description_tag['content']
    else:
        text = 'Could not find passage in Bible. Please review your query or try again later.'

    passage = {
        'url' : response.url,
        'text' : text,
    }
    return passage
Exemplo n.º 2
0
def get_bible_passage(query, version=None):
    version = get_bible_version(version)
    url = LITERAL_WORD_URLS.get(version, DEFAULT_BIBLE_VERSION)
    params = {
        'q' : query,
    }
    response = requests.get(url, params)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        text_container = soup.select('.bMeatWrapper')
        if len(text_container):
            container = text_container[0]
            # remove <p> tags
            for p in container.find_all('p'):
                p.decompose()
            # have to extract text using BeautifulSoup since Markdown syntax allows span tags, apparently
            html = u'%s' % container.text
            text = html2markdown(html)
        else:
            meta_description_tag = soup.meta.find(attrs={'name' : 'description',})
            text = meta_description_tag['content']
    else:
        text = 'Could not find passage in Bible. Please review your query or try again later.'

    passage = {
        'url' : response.url,
        'text' : text,
    }
    return passage
Exemplo n.º 3
0
def get_bible_passage(query, version=None):
    version = get_bible_version(version)
    url = LITERAL_WORD_URLS.get(version, DEFAULT_BIBLE_VERSION)
    params = {
        'q': query,
    }
    response = requests.get(url, params)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        text_container = soup.select('.bMeatWrapper')
        if len(text_container):
            container = text_container[0]
            # remove <p> tags
            for p in container.find_all('p'):
                p.decompose()
            # have to extract text using BeautifulSoup since Markdown syntax allows span tags, apparently
            html = u'%s' % container.text
            text = html2markdown(html)
        else:
            meta_description_tag = soup.meta.find(attrs={
                'name': 'description',
            })
            text = meta_description_tag['content']
    else:
        text = 'Could not find passage in Bible. Please review your query or try again later.'

    passage = {
        'url': response.url,
        'text': text,
    }
    return passage
Exemplo n.º 4
0
def get_bible_passage(query, version=None):
    version = get_bible_version(version)
    url = LITERAL_WORD_URLS.get(version, DEFAULT_BIBLE_VERSION)
    params = {
        'q': query,
    }
    response = requests.get(url, params)

    title = query

    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        text_container = soup.select('.bMeatWrapper')
        if len(text_container):
            container = text_container[0]

            title_container = container.select('.bTitle')
            if title_container:
                title = title_container[0].text.title()
            else:
                pass

            # add linebreaks for paragraphs
            paragraphs = container.select('.bPara')
            for paragraph in paragraphs:
                paragraph.replaceWith('<br/>')

            # add newlines for pericope headings
            pericopes = container.select('.bPeri')
            pericope_count = 0
            for pericope in pericopes:
                template = '<br/><br/><i>{}</i>' if pericope_count > 0 else '<i>{}</i>'
                pericope.replaceWith(template.format(pericope.text))
                pericope_count += 1

            # remove <p> tags
            for p in container.find_all('p'):
                p.decompose()
            # have to extract text using BeautifulSoup since Markdown syntax allows span tags, apparently
            html = '%s' % container.text
            text = html2markdown(html)
        else:
            meta_description_tag = soup.meta.find(attrs={
                'name': 'description',
            })
            text = meta_description_tag['content']
    else:
        text = 'Could not find passage in Bible. Please review your query or try again later.'

    passage = {
        'url': response.url,
        'text': text,
    }
    return passage
Exemplo n.º 5
0
def send_email(
    template=None,
    subject='',
    sender=None,
    to=None,
    cc=None,
    bcc=None,
    context=None,
    text_only=False
):
    """Sends a templated email w/ text and HTML
    """
    template = template or 'base'
    sender = sender or htk_setting('HTK_DEFAULT_EMAIL_SENDER', HTK_DEFAULT_EMAIL_SENDER)
    to = to or htk_setting('HTK_DEFAULT_EMAIL_RECIPIENTS', HTK_DEFAULT_EMAIL_RECIPIENTS)
    bcc = bcc or []
    cc = cc or []

    base_context = get_email_context()
    if context:
        base_context.update(context)
    else:
        pass
    context = base_context

    if settings.ENV_DEV:
        subject = '[%s-dev] %s' % (htk_setting('HTK_SYMBOLIC_SITE_NAME'), subject,)

    # assume HTML template exists, get that first
    try:
        html_template = get_template('emails/%s.html' % template)
        context['base_template'] = htk_setting('HTK_EMAIL_BASE_TEMPLATE_HTML')
        c = Context(context)
        html_content = html_template.render(c)
    except TemplateDoesNotExist:
        html_template = None
        html_content = ''

    # if native text template exists, use it
    try:
        context['base_template'] = htk_setting('HTK_EMAIL_BASE_TEMPLATE_TEXT')
        c = Context(context)
        text_template = get_template('emails/%s.txt' % template)
        text_content = text_template.render(c)
    except TemplateDoesNotExist:
        text_template = None
        # convert HTML to text
        if html_template:
            html_text_content = html_template.render(c)
            text_content = html2markdown(html_text_content)
        else:
            text_content = ''

    msg = EmailMultiAlternatives(subject=subject,
                                 body=text_content,
                                 from_email=sender,
                                 to=to,
                                 bcc=bcc,
                                 cc=cc)

    if not text_only and html_content:
        msg.attach_alternative(html_content, 'text/html')
    else:
        pass

    msg.send()
Exemplo n.º 6
0
def send_email(
    template=None,
    subject='',
    sender=None,
    reply_to=None,
    to=None,
    cc=None,
    bcc=None,
    context=None,
    text_only=False,
    headers=None
):
    """Sends a templated email w/ text and HTML
    """
    if headers is None:
        headers = {}

    if reply_to is not None:
        headers['Reply-To'] = reply_to

    template = template or 'base'
    sender = sender or htk_setting('HTK_DEFAULT_EMAIL_SENDER', HTK_DEFAULT_EMAIL_SENDER)
    to = to or htk_setting('HTK_DEFAULT_EMAIL_RECIPIENTS', HTK_DEFAULT_EMAIL_RECIPIENTS)
    bcc = bcc or []
    cc = cc or []

    base_context = get_email_context()
    if context:
        base_context.update(context)
    else:
        pass
    context = base_context

    if settings.ENV_DEV:
        subject = '[%s-dev] %s' % (htk_setting('HTK_SYMBOLIC_SITE_NAME'), subject,)

    # assume HTML template exists, get that first
    try:
        html_template = get_template('emails/%s.html' % template)
        context['base_template'] = htk_setting('HTK_EMAIL_BASE_TEMPLATE_HTML')
        html_content = html_template.render(context)
    except TemplateDoesNotExist:
        html_template = None
        html_content = ''

    # if native text template exists, use it
    try:
        context['base_template'] = htk_setting('HTK_EMAIL_BASE_TEMPLATE_TEXT')
        text_template = get_template('emails/%s.txt' % template)
        text_content = text_template.render(context)
    except TemplateDoesNotExist:
        text_template = None
        # convert HTML to text
        if html_template:
            html_text_content = html_template.render(context)
            text_content = html2markdown(html_text_content)
        else:
            text_content = ''

    msg = EmailMultiAlternatives(
        subject=subject,
        body=text_content,
        from_email=sender,
        to=to,
        bcc=bcc,
        cc=cc,
        headers=headers
    )

    if not text_only and html_content:
        msg.attach_alternative(html_content, 'text/html')
    else:
        pass

    email_attachments = htk_setting('HTK_EMAIL_ATTACHMENTS')
    if email_attachments:
        attach_images_to_message(msg, email_attachments)
        #for attachment in email_attachments:
        #    msg.attach_file(attachment)

    msg.send()