Spaces:
Sleeping
Sleeping
kltn20133118
commited on
Upload 2 files
Browse files- requirements.txt +8 -7
- support_function.py +147 -0
requirements.txt
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
-
git+https://github.com/obsei/obsei@master#egg=obsei[all]
|
2 |
-
streamlit
|
3 |
-
trafilatura
|
4 |
-
tornado>=6.3.2
|
5 |
-
fastapi
|
6 |
-
uvicorn
|
7 |
-
pymongo
|
|
|
|
1 |
+
git+https://github.com/obsei/obsei@master#egg=obsei[all]
|
2 |
+
streamlit
|
3 |
+
trafilatura
|
4 |
+
tornado>=6.3.2
|
5 |
+
fastapi
|
6 |
+
uvicorn
|
7 |
+
pymongo
|
8 |
+
crawl4ai
|
support_function.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from typing import Any, Dict
|
3 |
+
import asyncio
|
4 |
+
from datetime import datetime, timedelta
|
5 |
+
import re
|
6 |
+
from crawl4ai import AsyncWebCrawler
|
7 |
+
from crawl4ai.extraction_strategy import JsonCssExtractionStrategy
|
8 |
+
|
9 |
+
|
10 |
+
schema_short= {
|
11 |
+
"name": "VnExpress",
|
12 |
+
"baseSelector": "div.sidebar-1",
|
13 |
+
"fields": [
|
14 |
+
{
|
15 |
+
"name": "description",
|
16 |
+
"selector": "article p.Normal",
|
17 |
+
"type": "list",
|
18 |
+
"fields": [
|
19 |
+
{"name": "description", "type": "text"},
|
20 |
+
]
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"name": "genres",
|
24 |
+
"selector": "div.header-content ul.breadcrumb li a",
|
25 |
+
"type": "list",
|
26 |
+
"fields": [
|
27 |
+
{"name": "genres", "type": "text"},
|
28 |
+
]
|
29 |
+
}
|
30 |
+
]
|
31 |
+
}
|
32 |
+
url = "https://vnexpress.net/co-nen-bo-30-trieu-dong-de-hoc-them-mot-bang-o-tuoi-40-4825599.html"
|
33 |
+
|
34 |
+
|
35 |
+
async def return_json(schema: Dict[str, Any], url: str) -> Dict[str, Any]:
|
36 |
+
extraction_strategy = JsonCssExtractionStrategy(schema, verbose=True)
|
37 |
+
async with AsyncWebCrawler(always_by_pass_cache=True) as crawler:
|
38 |
+
result = await crawler.arun(
|
39 |
+
url=url,
|
40 |
+
exclude_external_links=True,
|
41 |
+
bypass_cache=True,
|
42 |
+
verbose=False,
|
43 |
+
warning=False,
|
44 |
+
extraction_strategy=extraction_strategy
|
45 |
+
)
|
46 |
+
news_teasers = json.loads(result.extracted_content)
|
47 |
+
return news_teasers
|
48 |
+
|
49 |
+
|
50 |
+
from datetime import datetime
|
51 |
+
import re
|
52 |
+
|
53 |
+
def chuan_hoa_time(time_str):
|
54 |
+
days_map = {
|
55 |
+
"thứ bảy": "Saturday",
|
56 |
+
"chủ nhật": "Sunday",
|
57 |
+
"thứ hai": "Monday",
|
58 |
+
"thứ ba": "Tuesday",
|
59 |
+
"thứ tư": "Wednesday",
|
60 |
+
"thứ năm": "Thursday",
|
61 |
+
"thứ sáu": "Friday",
|
62 |
+
}
|
63 |
+
|
64 |
+
month_map = {
|
65 |
+
"tháng 1": "January", "tháng 2": "February", "tháng 3": "March",
|
66 |
+
"tháng 4": "April", "tháng 5": "May", "tháng 6": "June",
|
67 |
+
"tháng 7": "July", "tháng 8": "August", "tháng 9": "September",
|
68 |
+
"tháng 10": "October", "tháng 11": "November", "tháng 12": "December"
|
69 |
+
}
|
70 |
+
|
71 |
+
# Làm sạch chuỗi thời gian
|
72 |
+
if isinstance(time_str, datetime):
|
73 |
+
# Nếu là đối tượng datetime, chuyển sang chuỗi
|
74 |
+
time_str = time_str.strftime('%Y-%m-%d %H:%M:%S')
|
75 |
+
elif not isinstance(time_str, str):
|
76 |
+
# Nếu không phải là chuỗi hoặc datetime, trả về chuỗi rỗng
|
77 |
+
return ""
|
78 |
+
time_str = time_str.lower().strip()
|
79 |
+
|
80 |
+
|
81 |
+
now = datetime.now()
|
82 |
+
if "phút trước" in time_str:
|
83 |
+
minutes = int(re.search(r"(\d+)", time_str).group(1))
|
84 |
+
time_result = now - timedelta(minutes=minutes)
|
85 |
+
return time_result.strftime("%d/%m/%Y %H:%M")
|
86 |
+
elif "giờ trước" in time_str:
|
87 |
+
hours = int(re.search(r"(\d+)", time_str).group(1))
|
88 |
+
time_result = now - timedelta(hours=hours)
|
89 |
+
return time_result.strftime("%d/%m/%Y %H:%M")
|
90 |
+
elif "hôm qua" in time_str:
|
91 |
+
time_result = now - timedelta(days=1)
|
92 |
+
return time_result.strftime("%d/%m/%Y %H:%M")
|
93 |
+
elif "ngày trước" in time_str:
|
94 |
+
days = int(re.search(r"(\d+)", time_str).group(1))
|
95 |
+
time_result = now - timedelta(days=days)
|
96 |
+
return time_result.strftime("%d/%m/%Y %H:%M")
|
97 |
+
|
98 |
+
# Sửa các lỗi chính tả thông thường
|
99 |
+
time_str = re.sub(r"\s?\(gmt[+\-]\d{1,2}\)", "", time_str).strip() # Loại bỏ múi giờ
|
100 |
+
time_str = time_str.replace("|", "") # Loại bỏ ký tự phân cách '|'
|
101 |
+
# time_str = time_str.replace(",", "") # Loại bỏ dấu phẩy nếu có
|
102 |
+
time_str = re.sub(r"\s+", " ", time_str).strip() # Loại bỏ khoảng trắng thừa
|
103 |
+
|
104 |
+
# Nếu chuỗi có dạng "20241202 15:44", cần chèn dấu "/" vào
|
105 |
+
if re.match(r"^\d{8} \d{2}:\d{2}$", time_str):
|
106 |
+
time_str = f"{time_str[:4]}/{time_str[4:6]}/{time_str[6:8]} {time_str[9:]}"
|
107 |
+
|
108 |
+
# Thêm dấu phẩy nếu thiếu sau ngày
|
109 |
+
if re.match(r"^[a-z]+ \d{2}/\d{2}/\d{4} \d{2}:\d{2}$", time_str):
|
110 |
+
time_str = time_str.replace(" ", ", ", 1) # Thêm dấu phẩy sau ngày
|
111 |
+
|
112 |
+
# Thay thế tháng tiếng Việt thành tiếng Anh
|
113 |
+
for vietnamese_month, english_month in month_map.items():
|
114 |
+
time_str = time_str.replace(vietnamese_month, english_month)
|
115 |
+
|
116 |
+
# Thay thế ngày trong tuần
|
117 |
+
for vietnamese_day, english_day in days_map.items():
|
118 |
+
time_str = time_str.replace(vietnamese_day, english_day)
|
119 |
+
|
120 |
+
# Định dạng chuỗi thời gian chuẩn
|
121 |
+
formats = [
|
122 |
+
"%A, %d/%m/%Y %H:%M", # Monday, 2/12/2024 11:33
|
123 |
+
"%Y-%m-%d %H:%M:%S", # 2024-11-29 16:01:23
|
124 |
+
"%Y-%m-%dT%H:%M:%S", # 2024-11-30T10:20:00
|
125 |
+
"%A, %d/%m/%Y - %H:%M", # Monday, 2/12/2024 - 11:33
|
126 |
+
"%A, %d/%m/%Y, %H:%M", # Monday, 2/12/2024, 11:33
|
127 |
+
"%Y-%m-%d %H:%M", # 2024-12-01 08:00
|
128 |
+
"%H:%M, %d/%m/%Y", # 10:45, 01/12/2024
|
129 |
+
"%d %B %Y %H:%M", # 2 December 2024 11:33
|
130 |
+
"%d/%m/%Y %H:%M", # 02/12/2024 15:22
|
131 |
+
"%d %b %Y %H:%M", # 2 Dec 2024 11:33
|
132 |
+
"%d/%m/%Y %H:%M:%S", # 02/12/2024 15:22:00
|
133 |
+
"%A, %d/%m/%Y %H:%M %z", # Monday, 02/12/2024 10:08 +0700
|
134 |
+
"%b %d, %Y %H:%M", # Dec 2, 2024 15:22
|
135 |
+
"%d tháng %m, %Y | %H:%M", # 2 tháng 12, 2024 | 15:10
|
136 |
+
"%A, %d/%m/%Y - %H:%M", # thứ hai, 02/12/2024 - 15:40
|
137 |
+
"%Y,%m,%d %H:%M", # 2024,12,02 15:44
|
138 |
+
"%A, %d/%m/%Y , %H:%M" # Monday, 02/12/2024 , 16:01
|
139 |
+
]
|
140 |
+
|
141 |
+
for fmt in formats:
|
142 |
+
try:
|
143 |
+
dt =datetime.strptime(time_str, fmt)
|
144 |
+
return dt
|
145 |
+
except ValueError:
|
146 |
+
continue
|
147 |
+
raise ValueError(f"Không thể chuẩn hóa chuỗi thời gian: {time_str}")
|