Spaces:
Sleeping
Sleeping
File size: 4,068 Bytes
c85c606 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
from pymongo import MongoClient
from typing import List, Any
from connect_mongo import connect_to_mongo
class NewsDatabaseHandler:
def __init__(self, uri: str, db_name: str):
"""
Khởi tạo kết nối MongoDB một lần duy nhất.
"""
self.client = MongoClient(uri)
self.db = self.client[db_name]
def get_all(self, collection_name: str) -> List[dict]:
"""
Lấy tất cả bản ghi từ collection.
"""
collection = self.db[collection_name]
return list(collection.find())
def find_by_category(self, collection_name: str, category: str) -> List[dict]:
"""
Tìm các bản ghi theo category.
"""
collection = self.db[collection_name]
query = {"category": category}
return list(collection.find(query))
def save_to_mongo(self, data: List[dict], collection_name: str):
"""
Lưu dữ liệu vào MongoDB.
"""
collection = self.db[collection_name]
collection.insert_many(data)
def close(self):
"""
Đóng kết nối MongoDB.
"""
self.client.close()
from datetime import datetime
def find_key():
db_name = "news"
collection_name = "articles"
collection = connect_to_mongo(db_name, collection_name)
# Tìm tất cả các bài viết có "name" chứa từ "Campuchia" và thuộc về TuoiTre hoặc VnExpress
vnexpress_or_tuoitre_news = collection.find({
"$in": [
{"VnExpress.name": {"$regex": "Campuchia", "$options": "i"}} # Tìm kiếm trong VnExpres # Tìm kiếm trong TuoiTre
]
})
# In ra các kết quả tìm được
for result in vnexpress_or_tuoitre_news:
# Nếu bạn chỉ muốn thấy bài viết chứa từ "Campuchia" trong tên:
if "Campuchia" in result.get('VnExpress', [{}])[0].get('name', '') or "Campuchia" in result.get('TuoiTre', [{}])[0].get('name', ''):
print(result)
# Gọi hàm tìm kiếm
# find_key()
def get_vnexpress_news(category):
db_name = "news"
collection_name = "articles"
collection = connect_to_mongo(db_name, collection_name)
# # Lấy thời gian hiện tại
# today = datetime.now().strftime("%Y-%m-%d")
# start_time = f"{today} 21:40:00"
# print(f"Start time: {start_time}")
# # Truy vấn MongoDB
# vnexpress_news = collection.find({
# "category": category,
# "VnExpress.time": {"$gte": start_time} # Điều kiện so sánh thời gian
# })
# filtered_news = []
# # Duyệt qua các bài báo và lọc ra các bài hợp lệ
# for result in vnexpress_news:
# if "VnExpress" in result:
# for news in result["VnExpress"]:
# # Lọc bỏ các mục trống hoặc không có dữ liệu
# if news and "time" in news:
# print(f"Checking time for news: {news['time']}")
# if news["time"] >= start_time:
# filtered_news.append(news)
# return filtered_news
# Gọi hàm và in kết quả
# news = get_vnexpress_news("VnExpress")
# if not news:
# print("No news found matching the criteria.")
# else:
# for article in news:
# print(f"Found article: {article}")
# Cấu hình kết nối
db_name = "test"
collection_name = "articles"
collection = connect_to_mongo(db_name, collection_name)
import datetime
# Tính thời gian 1h30 trước từ thời điểm hiện tại
time_limit = datetime.datetime.now() - datetime.timedelta(minutes=20)
time_limit = time_limit.replace(tzinfo=datetime.timezone.utc)
print(time_limit)
query = {
"category": "VnExpress",
"time": {"$gte": time_limit}
}
results = collection.find(query)
for result in results:
print("Bản ghi:")
print(result)
print("\n") |