Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
907 B
1
Indexable
Never
from tech_news.database import db
from datetime import datetime


# Requisito 7
def search_by_title(title):
    foo = []

    for bar in db.news.find({"title": {"$regex": title, "$options": "i"}}):
        foo.append((bar["title"], bar["url"]))

    return foo


# Requisito 8
def search_by_date(date):
    foo = []

    try:
        datetime.strptime(date, "%Y-%m-%d")
    except Exception:
        raise ValueError("Data inválida")

    normalized_date = datetime.strptime(date, "%Y-%m-%d").strftime("%d/%m/%Y")

    for bar in db.news.find(
        {"timestamp": {"$regex": normalized_date, "$options": "i"}}
    ):
        foo.append((bar["title"], bar["url"]))

    return foo


# Requisito 9
def search_by_category(category):
    foo = []

    bar = db.news.find({"category": {"$regex": category, "$options": "i"}})

    for bar in bar:
        foo.append((bar["title"], bar["url"]))

    return foo
Leave a Comment