# Path: importer/importer/utils/parsing.py

# 
# Path: importer/importer/utils/collections.py
# def find_first(needles, haystack):
#     for needle in needles:
#         if needle in haystack:
#             return needle
# 
# Path: importer/importer/utils/strings.py
# def strip_links(text):
#     return link_expression.sub('\\1', text)
# 
# def find_numbers(text):
#     return [
#         int(''.join(filter(str.isdigit, s)))
#         for s in number_expression.findall(text)
#         if s.strip()
#     ]
# 
# def is_from_string(text):
#     return bool(from_expression.match(text))
# 
# def is_up_to_string(text):
#     return bool(up_to_expression.match(text))


from operator import itemgetter
from funcy import flatten, project, first
from datetime import datetime, time, timedelta
from isodate import parse_date
from .utils.parsing import maybe_parse_date, maybe_parse_time
from .utils.collections import find_first
from .utils.strings import (
    strip_links,
    find_numbers,
    is_from_string,
    is_up_to_string,
)

def transform_stub_agent(kudago_agent):
    return {
        '_id': kudago_agent['id'],
        '_type': 'agent',

        'kind': kudago_agent['agent_type'],
        'is_stub': kudago_agent['is_stub'],

        'name': kudago_agent['title'],

        'source': {
            'name': 'kudago.com',
            'url': kudago_agent['site_url'],
        }
    }


# related objects

def transform_price(price_text, is_free):
    if is_free:
        lower = 0
        upper = 0
    else:
        numbers = find_numbers(price_text)
        if numbers:
            if is_from_string(price_text):
                lower = numbers[0]
                upper = None

# Path: importer/importer/utils/parsing.py

# 
# Path: importer/importer/utils/collections.py
# def find_first(needles, haystack):
#     for needle in needles:
#         if needle in haystack:
#             return needle
# 
# Path: importer/importer/utils/strings.py
# def strip_links(text):
#     return link_expression.sub('\\1', text)
# 
# def find_numbers(text):
#     return [
#         int(''.join(filter(str.isdigit, s)))
#         for s in number_expression.findall(text)
#         if s.strip()
#     ]
# 
# def is_from_string(text):
#     return bool(from_expression.match(text))
# 
# def is_up_to_string(text):
#     return bool(up_to_expression.match(text))


# Path: importer/importer/transform.py
from operator import itemgetter
from funcy import flatten, project, first
from datetime import datetime, time, timedelta
from isodate import parse_date
from .utils.parsing import maybe_parse_date, maybe_parse_time
from .utils.collections import find_first
from .utils.strings import (
    strip_links,
    find_numbers,
    is_from_string,
    is_up_to_string,
)


def transform_stub_agent(kudago_agent):
    return {
        '_id': kudago_agent['id'],
        '_type': 'agent',

        'kind': kudago_agent['agent_type'],
        'is_stub': kudago_agent['is_stub'],

        'name': kudago_agent['title'],

        'source': {
            'name': 'kudago.com',
            'url': kudago_agent['site_url'],
        }
    }


# related objects

def transform_price(price_text, is_free):
    if is_free:
        lower = 0
        upper = 0
    else:
        numbers = find_numbers(price_text)
        if numbers:
            if is_from_string(price_text):
                lower = numbers[0]
                upper = None

            elif is_up_to_string(price_text):