entari-plugin-hyw 4.0.0rc9__py3-none-any.whl → 4.0.0rc11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of entari-plugin-hyw might be problematic. Click here for more details.

@@ -1,155 +0,0 @@
1
-
2
- import urllib.parse
3
- import re
4
- from typing import List, Dict, Any
5
- from loguru import logger
6
- from .base import SearchEngine
7
-
8
-
9
- class GoogleEngine(SearchEngine):
10
- """
11
- Search engine implementation for Google.
12
- Parses Google Search HTML results.
13
- """
14
-
15
- def build_url(self, query: str, limit: int = 10) -> str:
16
- encoded_query = urllib.parse.quote(query)
17
- return f"https://www.google.com/search?q={encoded_query}&udm=14"
18
-
19
- def parse(self, content: str) -> List[Dict[str, Any]]:
20
- results = []
21
- seen_urls = set()
22
-
23
- # Google search results are in blocks with class="MjjYud" or similar containers
24
- # Split by result blocks first for more accurate extraction
25
-
26
- # Method 1: Split by common result block classes
27
- block_patterns = [
28
- r'<div class="MjjYud"[^>]*>',
29
- r'<div class="tF2Cxc"[^>]*>',
30
- r'<div class="g Ww4FFb"[^>]*>',
31
- ]
32
-
33
- blocks = [content]
34
- for bp in block_patterns:
35
- new_blocks = []
36
- for block in blocks:
37
- parts = re.split(bp, block)
38
- new_blocks.extend(parts)
39
- blocks = new_blocks
40
-
41
- for block in blocks:
42
- if len(block) < 100:
43
- continue
44
-
45
- # Find URL in this block - prefer links with h3 nearby
46
- url_match = re.search(r'<a[^>]+href="(https?://(?!www\.google\.|google\.|webcache\.googleusercontent\.)[^"]+)"[^>]*>', block)
47
- if not url_match:
48
- continue
49
-
50
- url = url_match.group(1)
51
- if url in seen_urls or self._should_skip_url(url):
52
- continue
53
-
54
- # Find h3 title in this block
55
- h3_match = re.search(r'<h3[^>]*>(.*?)</h3>', block, re.IGNORECASE | re.DOTALL)
56
- if not h3_match:
57
- continue
58
-
59
- title = re.sub(r'<[^>]+>', '', h3_match.group(1)).strip()
60
- if not title or len(title) < 2:
61
- continue
62
-
63
- seen_urls.add(url)
64
-
65
- # Extract snippet from VwiC3b class (Google's snippet container)
66
- snippet = ""
67
- snippet_match = re.search(r'<div[^>]*class="[^"]*VwiC3b[^"]*"[^>]*>(.*?)</div>', block, re.IGNORECASE | re.DOTALL)
68
- if snippet_match:
69
- snippet = re.sub(r'<[^>]+>', ' ', snippet_match.group(1)).strip()
70
- snippet = re.sub(r'\s+', ' ', snippet).strip()
71
-
72
- # Fallback: look for any text after h3
73
- if not snippet:
74
- # Try other common snippet patterns
75
- alt_patterns = [
76
- r'<span[^>]*class="[^"]*aCOpRe[^"]*"[^>]*>(.*?)</span>',
77
- r'<div[^>]*data-snc[^>]*>(.*?)</div>',
78
- ]
79
- for ap in alt_patterns:
80
- am = re.search(ap, block, re.IGNORECASE | re.DOTALL)
81
- if am:
82
- snippet = re.sub(r'<[^>]+>', ' ', am.group(1)).strip()
83
- snippet = re.sub(r'\s+', ' ', snippet).strip()
84
- break
85
-
86
- # Extract images from this block
87
- images = []
88
- # Pattern 1: Regular img src (excluding data: and tracking pixels)
89
- # Note: gstatic.com/images/branding is logo, but encrypted-tbn*.gstatic.com are thumbnails
90
- img_matches = re.findall(r'<img[^>]+src="(https?://[^"]+)"', block)
91
- for img_url in img_matches:
92
- # Decode HTML entities
93
- img_url = img_url.replace('&amp;', '&')
94
- # Skip tracking/icon/small images (but allow encrypted-tbn which are valid thumbnails)
95
- if any(x in img_url.lower() for x in ['favicon', 'icon', 'tracking', 'pixel', 'logo', 'gstatic.com/images/branding', '1x1', 'transparent', 'gstatic.com/images/icons']):
96
- continue
97
- if img_url not in images:
98
- images.append(img_url)
99
-
100
- # Pattern 2: data-src (lazy loaded images)
101
- data_src_matches = re.findall(r'data-src="(https?://[^"]+)"', block)
102
- for img_url in data_src_matches:
103
- img_url = img_url.replace('&amp;', '&')
104
- if any(x in img_url.lower() for x in ['favicon', 'icon', 'tracking', 'pixel', 'logo']):
105
- continue
106
- if img_url not in images:
107
- images.append(img_url)
108
-
109
- results.append({
110
- "title": title,
111
- "url": url,
112
- "domain": urllib.parse.urlparse(url).hostname or "",
113
- "content": snippet[:1000],
114
- "images": images[:3] # Limit to 3 images per result
115
- })
116
-
117
- if len(results) >= 15:
118
- break
119
-
120
- total_images = sum(len(r.get("images", [])) for r in results)
121
- logger.info(f"GoogleEngine parsed {len(results)} results with {total_images} images total.")
122
- return results
123
-
124
- def _should_skip_url(self, url: str) -> bool:
125
- """Check if URL should be skipped."""
126
- skip_patterns = [
127
- "google.com",
128
- "googleusercontent.com",
129
- "gstatic.com",
130
- "youtube.com/watch", # Keep channel/playlist but skip individual videos
131
- "maps.google",
132
- "translate.google",
133
- "accounts.google",
134
- "support.google",
135
- "policies.google",
136
- "schema.org",
137
- "javascript:",
138
- "data:",
139
- "#",
140
- ]
141
-
142
- for pattern in skip_patterns:
143
- if pattern in url.lower():
144
- return True
145
-
146
- # Skip very short URLs (likely invalid)
147
- if len(url) < 20:
148
- return True
149
-
150
- # Skip URLs that are just root domains without path
151
- parsed = urllib.parse.urlparse(url)
152
- if not parsed.path or parsed.path == "/":
153
- return True
154
-
155
- return False