Beispiel #1
0
def tweet():
    """Send tweet with todays content
    """
    import twitter as tw
    base_url = "http://reformedconfessions.com/westminster-daily/"
    cred = {
        "consumer_key": os.environ['TW_CONSUMER_KEY'],
        "consumer_secret": os.environ['TW_CONSUMER_SECRET'],
        "token": os.environ['TW_TOKEN'],
        "token_secret": os.environ['TW_TOKEN_SECRET'],
    }
    auth = tw.OAuth(**cred)
    t = tw.Twitter(auth=auth)

    month, day, content = get_today_content(tz="US/Eastern")
    description = get_day_title(month, day)
    url = "{base}{month:0>2}/{day:0>2}".format(base=base_url, month=month, day=day)

    try:
        # Attempt tweet
        with open("flask_application/static/images/docs/{:0>2}{:0>2}-full.png".format(month, day), "rb") as imagefile:
            imagedata = imagefile.read()
        t_up = tw.Twitter(domain='upload.twitter.com', auth=auth)
        id_img1 = t_up.media.upload(media=imagedata)["media_id_string"]
        t.statuses.update(status="{} {}".format(description, url),
                          media_ids=id_img1)
    except tw.api.TwitterHTTPError as e:
        if any(error['code'] == 186 for error in e.response_data['errors']):
            # Tweet too long. Try a shorter tweet.
            description = ", ".join(c['citation'] for c in content)
            t.statuses.update(status="{} {}".format(description, url))
        else:
            log.error("%s %s", "Unhandled exception", str(e))
        return
    log.info("%s %s", "Tweeted", str(description))
def generate_day_email(month, day, prooftexts=True):
    email = []
    for c in data.get_day(month, day):
        email.append("<h3 style='{}'>{}</h3>".format(FONT, c['long_citation']))
        if 'body' in c:
            b = re.sub(r"<a.*?footnote.*?>&bull;</a>", "&bull;", c['body'])
            email.append(b)
        else:
            email.append(c['question'])
            email.append("<br />")
            b = re.sub(r"<a.*?footnote.*?>&bull;</a>", "&bull;", c['answer'])
            email.append(b)
        if 'prooftexts' in c:
            email.append(format_prooftexts(c['prooftexts']))
    body = "\n".join(email)
    body = "<span style='{}'>{}</span>".format(FONT, body)
    return body, data.get_day_title(month, day)