File size: 5,600 Bytes
46ea0ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""URL manipulation utilities."""

from urllib.parse import urljoin, urlparse
from typing import Set


class URLUtils:
    """Utilities for URL manipulation and validation."""
    
    # File extensions to skip
    IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico', '.bmp'}
    MEDIA_EXTENSIONS = {'.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.mp3', '.wav'}
    DOCUMENT_EXTENSIONS = {'.pdf', '.doc', '.docx', '.xls', '.xlsx', '.zip', '.rar'}
    SKIP_EXTENSIONS = IMAGE_EXTENSIONS | MEDIA_EXTENSIONS | DOCUMENT_EXTENSIONS
    
    @staticmethod
    def is_same_domain(url1: str, url2: str) -> bool:
        """Check if two URLs are from the same domain."""
        return urlparse(url1).netloc == urlparse(url2).netloc
    
    @staticmethod
    def clean_url(url: str) -> str:
        """
        Clean URL by removing fragments and query parameters.
        
        Args:
            url: The URL to clean
            
        Returns:
            Cleaned URL
        """
        return url.split('#')[0].split('?')[0]
    
    @staticmethod
    def make_absolute(url: str, base_url: str) -> str:
        """
        Convert relative URL to absolute URL.
        
        Args:
            url: The URL (relative or absolute)
            base_url: The base URL
            
        Returns:
            Absolute URL
        """
        if not url.startswith('http'):
            return urljoin(base_url, url)
        return url
    
    @staticmethod
    def is_valid_url(url: str) -> bool:
        """
        Check if URL is valid and not an anchor or mailto link.
        
        Args:
            url: The URL to validate
            
        Returns:
            True if valid, False otherwise
        """
        if url.startswith('#') or url.startswith('mailto:'):
            return False
        return True
    
    @staticmethod
    def is_media_file(url: str) -> bool:
        """
        Check if URL points to a media file (image, video, document).
        
        Args:
            url: The URL to check
            
        Returns:
            True if media file, False otherwise
        """
        # Parse URL and get the path
        parsed = urlparse(url)
        path = parsed.path.lower()
        
        # Check if path ends with any media extension
        for ext in URLUtils.SKIP_EXTENSIONS:
            if path.endswith(ext):
                return True
        
        # Also check for common media directory patterns
        media_patterns = ['/wp-content/uploads/', '/images/', '/media/', '/assets/']
        for pattern in media_patterns:
            if pattern in path and any(path.endswith(ext) for ext in URLUtils.SKIP_EXTENSIONS):
                return True
        
        return False
    
    @staticmethod
    def should_crawl(url: str) -> bool:
        """
        Determine if a URL should be crawled.
        
        Args:
            url: The URL to check
            
        Returns:
            True if should crawl, False otherwise
        """
        # Skip invalid URLs
        if not URLUtils.is_valid_url(url):
            return False
        
        # Skip media files
        if URLUtils.is_media_file(url):
            return False
        
        # Skip common non-page URLs
        skip_patterns = [
            '/feed/',
            '/rss/',
            '/wp-json/',
            '/xmlrpc.php',
            '/wp-login.php',
            '/wp-admin/',
        ]
        
        url_lower = url.lower()
        for pattern in skip_patterns:
            if pattern in url_lower:
                return False
        
        return True
    
    @staticmethod
    def get_path_segment(url: str, segment: int = -1) -> str:
        """
        Get a specific path segment from URL.
        
        Args:
            url: The URL
            segment: Which segment to get (default: last)
            
        Returns:
            Path segment
        """
        path = urlparse(url).path
        segments = [s for s in path.split('/') if s]
        if segments:
            return segments[segment]
        return ""


# Standalone helper functions for convenience
def normalize_url(url: str) -> str:
    """
    Normalize URL by removing fragments, standardizing format.
    
    Args:
        url: URL to normalize
        
    Returns:
        Normalized URL or None if invalid
    """
    if not url or url.startswith('#') or url.startswith('mailto:'):
        return None
    
    try:
        parsed = urlparse(url)
        
        # Must have scheme and netloc
        if not parsed.scheme or not parsed.netloc:
            return None
        
        # Lowercase domain
        netloc = parsed.netloc.lower()
        
        # Remove trailing slash from path
        path = parsed.path.rstrip('/')
        
        # Reconstruct without fragment
        from urllib.parse import urlunparse
        normalized = urlunparse((
            parsed.scheme,
            netloc,
            path,
            parsed.params,
            parsed.query,
            ''  # No fragment
        ))
        
        return normalized
    
    except Exception:
        return None


def same_domain(url1: str, url2: str) -> bool:
    """
    Check if two URLs are from the same domain.
    
    Args:
        url1: First URL
        url2: Second URL
        
    Returns:
        True if same domain, False otherwise
    """
    try:
        domain1 = urlparse(url1).netloc.lower()
        domain2 = urlparse(url2).netloc.lower()
        return domain1 == domain2
    except Exception:
        return False