def post_to_twitter(text, url, force=False):
    logger.info('Setting status to "%s" for url "%s"' % (text, url))
    init_twitter_client(logger)
    if force or not remember.already_tweeted(url):
        twitter_client.statuses.update(status=text)
        remember.remember_tweet(url)
    else:
        logger.info('Not tweeting "%s": already tweeted url "%s"'
                % (text, url))
Exemple #2
0
def do_story(conn, story):
    url = 'http://api.reddit.com'+story['data']['permalink']
    url = url.encode('utf-8', 'replace')
    time.sleep(5)
    logger.info('Fetching %s' % (url,))
    story_data = json.loads(opener.open(url).read())
    for key, value in iterate_string_leaves(story_data, 'body_html', 'author'):
        if key == 'body_html':
            value = unescaper.unescape(value)
            soup = BeautifulSoup(value)
            for anchor in soup.find_all('a'):
                found_url = anchor.attrs['href']
                print colored(found_url, 'green' if
                        should_post_image(found_url)  else 'red')
                if should_post_image(found_url):
                    message = str(found_url)
                    if not remember.already_tweeted(conn, found_url):
                        logger.info('Emitting url %s' % (found_url,))
                        twitter_bot.post_to_twitter.delay(message, found_url)