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 update_facebook(): api = facebook.GraphAPI(os.environ['FB_ACCESS_TOKEN']) month, day, content = get_today_content(tz="US/Eastern", prooftexts=False) base_url = "http://reformedconfessions.com/westminster-daily/" url = "{base}{month:0>2}/{day:0>2}".format(base=base_url, month=month, day=day) attachment = {'link': url} content = make_facebook_string(content) if (month, day) in [(1, 4), (7, 5)]: content = "" # Days have html formatting _post_facebook(api, content, attachment)
def test_today_content(): month, day, content = data.get_today_content(tz='UTC') tz = pytz.timezone('UTC') assert month == dt.datetime.now(tz=tz).month assert day == dt.datetime.now(tz=tz).day