<commit_before>import requests

from mhn.tasks import celery
from mhn.api.models import Rule
from mhn.common.ruleutils import from_buffer


@celery.task
def render_rules():
    from flask import current_app
    sbuffer = Rule.renderall()
    fpath = current_app.config['RENDERED_RULES_PATH']
    with open(fpath, 'w') as rfile:
        rfile.write(sbuffer)


@celery.task
def fetch_source(uri):
    resp = requests.get(uri)
    if resp.status_code == 200:
        rules = from_buffer(resp.text)
        Rule.bulk_import(rules)
        render_rules.delay()
    else:
        pass<commit_msg>fetch_sources task now loops through all the sources.<commit_after>from flask import current_app as app
import requests

from mhn.tasks import celery
from mhn.api.models import Rule, RuleSource
from mhn.common.ruleutils import from_buffer


@celery.task
def render_rules():
    app.logger.info('Rendering rules.')
    from flask import current_app
    sbuffer = Rule.renderall()
    fpath = current_app.config['RENDERED_RULES_PATH']
    with open(fpath, 'w') as rfile:
        rfile.write(sbuffer)
    app.logger.info('Finished rendering rules.')


@celery.task
def fetch_sources():
    app.logger.info('Fetching sources from {} sources.'.format(
        RuleSource.query.count()))
    rules = []
    for src in RuleSource.query:
        # Download rules from every source.
        app.logger.info('Downloading from "{}".'.format(src.uri))
        resp = requests.get(src.uri)
        if resp.status_code == 200:
            # rules will contain all parsed rules.
            rules.extend(from_buffer(resp.text))
        else:
            pass
    app.logger.info('Bulk importing {} rules.'.format(len(rules)))
    Rule.bulk_import(rules)
    render_rules()
{'fraction_non_alphanumeric': 0.10532341156267888, 'fraction_numerical': 0.0034344590726960505, 'mean_word_length': 3.132387706855792, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2068474', 'n_tokens_mistral': 556, 'n_tokens_neox': 540, 'n_words': 134}