Ejemplo n.º 1
0
def main():
    """Fetch issues/PRs from GitHub and post them to Slack."""
    logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
    logger = logging.getLogger('gh2slack')
    args = parse_args()
    if args.verbosity:
        logger.setLevel(logging.DEBUG)

    try:
        slack_token = rss2slack.get_slack_token()
        url = get_gh_api_url(args.gh_owner, args.gh_repo, args.gh_section)
        pages = gh_request(logger, url)

        logger.debug('Got %i pages from GH.', len(pages))
        if not pages:
            logger.info('No %s for %s/%s.', args.gh_section, args.gh_owner,
                        args.gh_repo)
            sys.exit(0)

        cache = rss2irc.read_cache(logger, args.cache)
        scrub_cache(logger, cache)

        # Note: I have failed to find web link to repo in GH response.
        # Therefore, let's create one.
        repository_url = get_gh_repository_url(args.gh_owner, args.gh_repo)
        item_expiration = int(time.time()) + args.cache_expiration
        to_publish = process_page_items(logger, cache, pages, item_expiration,
                                        repository_url)

        if not args.cache_init and to_publish:
            slack_client = rss2slack.get_slack_web_client(
                slack_token, args.slack_base_url, args.slack_timeout)
            for html_url in to_publish:
                cache_item = cache.items[html_url]
                try:
                    msg_blocks = [
                        format_message(logger, args.gh_owner, args.gh_repo,
                                       ALIASES[args.gh_section], html_url,
                                       cache_item)
                    ]
                    rss2slack.post_to_slack(
                        logger,
                        msg_blocks,
                        slack_client,
                        args.slack_channel,
                    )
                except Exception:
                    logger.error(traceback.format_exc())
                    cache.items.pop(html_url)
                finally:
                    time.sleep(args.sleep)

        rss2irc.write_cache(cache, args.cache)
    except Exception:
        logger.debug(traceback.format_exc())
        # TODO(zstyblik):
        # 1. touch error file
        # 2. send error message to the channel
    finally:
        sys.exit(0)
Ejemplo n.º 2
0
def main():
    """Main."""
    logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
    logger = logging.getLogger('phpbb2slack')
    args = parse_args()
    if args.verbosity:
        logger.setLevel(logging.DEBUG)

    if args.cache_expiration < 0:
        logger.error("Cache expiration can't be less than 0.")
        sys.exit(1)

    slack_token = rss2slack.get_slack_token()
    authors = get_authors_from_file(logger, args.authors_file)

    news = {}
    for rss_url in args.rss_urls:
        data = rss2irc.get_rss(logger, rss_url, args.rss_http_timeout)
        if not data:
            logger.error('Failed to get RSS from %s', rss_url)
            sys.exit(1)

        parse_news(data, news, authors)

    if not news:
        logger.info('No news?')
        sys.exit(0)

    cache = rss2irc.read_cache(logger, args.cache)
    scrub_cache(logger, cache)

    for key in news.keys():
        if key not in cache:
            continue

        logger.debug('Key %s found in cache', key)
        if int(cache[key]['comments_cnt']) == int(news[key]['comments_cnt']):
            cache[key]['expiration'] = int(time.time()) + args.cache_expiration
            news.pop(key)

    slack_client = SlackClient(slack_token)
    if not args.cache_init:
        for url in news.keys():
            message = format_message(url, news[url], args.handle)
            try:
                rss2slack.post_to_slack(logger, message, slack_client,
                                        args.slack_channel, args.slack_timeout)
            except ValueError:
                news.pop(url)
            finally:
                time.sleep(args.sleep)

    expiration = int(time.time()) + args.cache_expiration
    update_cache(cache, news, expiration)
    rss2irc.write_cache(cache, args.cache)
Ejemplo n.º 3
0
def main():
    """Main."""
    logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
    logger = logging.getLogger('gh2slack')
    args = parse_args()
    if args.verbosity:
        logger.setLevel(logging.DEBUG)

    slack_token = rss2slack.get_slack_token()
    url = get_gh_api_url(args.gh_owner, args.gh_repo, args.gh_section)
    pages = gh_request(logger, url)

    logger.debug('Got %i pages from GH.', len(pages))
    if not pages:
        logger.info('No %s for %s/%s.', args.gh_section, args.gh_owner,
                    args.gh_repo)
        sys.exit(0)

    cache = rss2irc.read_cache(logger, args.cache)
    scrub_cache(logger, cache)

    # Note: I have failed to find web link to repo in GH response. Therefore,
    # let's create one.
    repository_url = get_gh_repository_url(args.gh_owner, args.gh_repo)
    item_expiration = int(time.time()) + args.cache_expiration
    to_publish = process_page_items(
        logger, cache, pages, item_expiration, repository_url
    )

    if not args.cache_init and to_publish:
        slack_client = SlackClient(slack_token)
        for html_url in to_publish:
            cache_item = cache[html_url]
            try:
                message = assembly_slack_message(
                    logger, args.gh_owner, args.gh_repo,
                    ALIASES[args.gh_section], html_url, cache_item
                )
                rss2slack.post_to_slack(
                    logger, message, slack_client, args.slack_channel,
                    args.slack_timeout
                )
            except Exception:
                logger.error(traceback.format_exc())
                cache.pop(html_url)
            finally:
                time.sleep(args.sleep)

    rss2irc.write_cache(cache, args.cache)
Ejemplo n.º 4
0
def main():
    """Fetch phpBB RSS feed and post RSS news to Slack."""
    logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
    logger = logging.getLogger('phpbb2slack')
    args = parse_args()
    if args.verbosity:
        logger.setLevel(logging.DEBUG)

    if args.cache_expiration < 0:
        logger.error("Cache expiration can't be less than 0.")
        sys.exit(1)

    try:
        slack_token = rss2slack.get_slack_token()
        authors = get_authors_from_file(logger, args.authors_file)

        data = rss2irc.get_rss(logger, args.rss_url, args.rss_http_timeout)
        if not data:
            logger.error('Failed to get RSS from %s', args.rss_url)
            sys.exit(1)

        news = parse_news(data, authors)
        if not news:
            logger.info('No news?')
            sys.exit(0)

        cache = rss2irc.read_cache(logger, args.cache)
        scrub_cache(logger, cache)

        for key in list(news.keys()):
            if key not in cache.items:
                continue

            logger.debug('Key %s found in cache', key)
            comments_cached = int(cache.items[key]['comments_cnt'])
            comments_actual = int(news[key]['comments_cnt'])
            if comments_cached == comments_actual:
                cache.items[key]['expiration'] = (int(time.time()) +
                                                  args.cache_expiration)
                news.pop(key)

        slack_client = rss2slack.get_slack_web_client(slack_token,
                                                      args.slack_base_url,
                                                      args.slack_timeout)
        if not args.cache_init:
            for url in list(news.keys()):
                msg_blocks = [format_message(url, news[url], args.handle)]
                try:
                    rss2slack.post_to_slack(
                        logger,
                        msg_blocks,
                        slack_client,
                        args.slack_channel,
                    )
                except ValueError:
                    news.pop(url)
                finally:
                    time.sleep(args.sleep)

        expiration = int(time.time()) + args.cache_expiration
        update_cache(cache, news, expiration)
        rss2irc.write_cache(cache, args.cache)
    except Exception:
        logger.debug(traceback.format_exc())
        # TODO(zstyblik):
        # 1. touch error file
        # 2. send error message to the channel
    finally:
        sys.exit(0)