<commit_before>import re

from peewee import fn

import const
import settings
from model import Bot
from model import Category
from model import Keyword


def search_bots(query):
    query = query.lower().strip()
    split = query.split(' ')

    # easter egg
    if query == 'awesome bot':
        return [Bot.by_username('@botlistbot')]

    # exact results
    where_query = (
        (fn.lower(Bot.username).contains(query)) |
        (fn.lower(Bot.name) << split) |
        (fn.lower(Bot.extra) ** query)
    )
    results = set(Bot.select().distinct().where(where_query))

    # keyword results
    keyword_results = Bot.select(Bot).join(Keyword).where(fn.lower(Keyword.name) << split)
    results.update(keyword_results)

    # many @usernames
    usernames = re.findall(settings.REGEX_BOT_ONLY, query)
    if usernames:
        try:
            bots = Bot.many_by_usernames(usernames)
            results.update(bots)<commit_msg>Only search for bots whose revision number is LEQ than current<commit_after>import re

from peewee import fn

import const
import settings
from model import Bot
from model import Category
from model import Keyword
from model.revision import Revision


def search_bots(query):
    query = query.lower().strip()
    split = query.split(' ')

    # easter egg
    if query == 'awesome bot':
        return [Bot.by_username('@botlistbot')]

    # exact results
    where_query = (
        (fn.lower(Bot.username).contains(query)) |
        (fn.lower(Bot.name) << split) |
        (fn.lower(Bot.extra) ** query) &
        (Bot.revision <= Revision.get_instance().nr &
         Bot.approved == True)
    )
    results = set(Bot.select().distinct().where(where_query))

    # keyword results
    keyword_results = Bot.select(Bot).join(Keyword).where(fn.lower(Keyword.name) << split)
    results.update(keyword_results)

    # many @usernames
    usernames = re.findall(settings.REGEX_BOT_ONLY, query)
    if usernames:
        try:
            bots = Bot.many_by_usernames(usernames)
            results.update(bots)
{'fraction_non_alphanumeric': 0.11779081133919844, 'fraction_numerical': 0.0, 'mean_word_length': 2.990253411306043, 'pattern_counts': {'":': 0, '<': 12, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '698171', 'n_tokens_mistral': 647, 'n_tokens_neox': 642, 'n_words': 143}