Beispiel #1
0
def top_5_news():
    top_5 = get_collection().aggregate([
        {
            "$addFields": {
                "total_shares_and_comments": {
                    "$sum": ["$shares_count", "$comments_count"]
                }
            },
        },
        {
            "$sort": {
                "total_shares_and_comments": -1,
                "title": 1
            }
        },
        {
            "$limit": 5
        },
        {
            "$project": {
                "_id": False,
                "title": True,
                "url": True
            }
        },
    ])
    return [(category["title"], category["url"]) for category in top_5]
Beispiel #2
0
def search_by_date(date):
    try:
        datetime.strptime(date, "%Y-%m-%d")
    except ValueError:
        raise ValueError("Data inválida")
    results = get_collection().find(
        {"timestamp": {"$regex": re.compile(date)}},
        {"title": True, "_id": False, "url": True},
    )
    return [(result["title"], result["url"]) for result in results]
Beispiel #3
0
def top_5_categories():
    top_5 = get_collection().aggregate([
        {
            "$unwind": "$categories"
        },
        {
            "$group": {
                "_id": "$categories",
                "quantity": {
                    "$sum": 1
                }
            }
        },
        {
            "$sort": {
                "quantity": -1,
                "_id": 1
            }
        },
        {
            "$limit": 5
        },
    ])
    return [category["_id"] for category in top_5]
Beispiel #4
0
def search_by_title(title):
    results = get_collection().find(
        {"title": {"$regex": re.compile(title, re.IGNORECASE)}},
        {"title": True, "_id": False, "url": True},
    )
    return [(result["title"], result["url"]) for result in results]