You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import re
name = 'Tracking Number Detector'
description = 'Detects tracking numbers and redirects to carrier tracking pages'
default_on = True
#preference_section = 'query'
# Define patterns for major carriers
carriers = {
'ups': r'\b1Z[0-9A-Z]{16}\b',
'fedex': r'\b\d{12,14}\b',
'usps': r'\b\d{20,22}\b',
'dhl': r'\b\d{10,11}\b'
}
# Define tracking URLs for carriers
tracking_urls = {
'ups': 'https://www.ups.com/track?loc=en_US&tracknum={}',
'fedex': 'https://www.fedex.com/fedextrack/?trknbr={}',
'usps': 'https://tools.usps.com/go/TrackConfirmAction?tLabels={}',
'dhl': 'https://www.dhl.com/en/express/tracking.html?AWB={}'
}
def detect_tracking_number(query):
for carrier, pattern in carriers.items():
match = re.search(pattern, query)
if match:
return carrier, match.group()
return None, None
def pre_search(request, search):
query = search.search_query.query
carrier, tracking_number = detect_tracking_number(query)
if carrier and tracking_number:
redirect_url = tracking_urls[carrier].format(tracking_number)
search.redirect_url = redirect_url
return False # Stop the search and redirect
return True # Continue with the normal search
import re
from typing import Dict, Optional, Tuple
name = 'shipping_tracker'
description = 'Detects shipping tracking numbers and redirects to carrier tracking pages'
default_on = False # disabled by default
class TrackingDetector:
CARRIERS = {
'ups': {
'patterns': [
r'\b1Z[0-9A-Z]{16}\b', # UPS
r'\b\d{12}\b', # UPS Alternative
],
'url': 'https://www.ups.com/track?track=yes&tracknum={}'
},
'fedex': {
'patterns': [
r'\b(\d{12}|\d{14}|\d{15})\b', # FedEx Ground/Express
r'\b(\d{10}|\d{22})\b', # FedEx Other
],
'url': 'https://www.fedex.com/fedextrack/?trknbr={}'
},
'usps': {
'patterns': [
r'\b(94|93|92|94|95)[0-9]{20}\b', # USPS IMpb
r'\b(70|14|23|03)[0-9]{14}\b', # USPS Regular
r'\b\d{2}\s?\d{3}\s?\d{3}\s?\d{2}\s?\d{4}\s?\d{2}\s?\d{2}\b', # USPS with spaces
r'\bEC\d{9}US\b', # USPS International
r'\b(C|E|L|V)\d{9}\b' # USPS Certified/Express
],
'url': 'https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1={}'
},
'dhl': {
'patterns': [
r'\b\d{10}\b', # DHL Express
r'\b[0-9A-Z]{12}\b' # DHL Global
],
'url': 'https://www.dhl.com/en/express/tracking.html?AWB={}&brand=DHL'
}
}
@classmethod
def clean_tracking_number(cls, number: str) -> str:
"""Remove spaces and standardize tracking number format."""
return number.replace(' ', '').upper()
@classmethod
def detect_carrier(cls, query: str) -> Optional[Tuple[str, str]]:
"""
Detect carrier and tracking number from query.
Returns tuple of (carrier_key, tracking_number) or None if not found.
"""
query = query.strip().upper()
for carrier, data in cls.CARRIERS.items():
for pattern in data['patterns']:
matches = re.findall(pattern, query, re.IGNORECASE)
if matches:
tracking_number = cls.clean_tracking_number(matches[0])
return carrier, tracking_number
return None
# Initialize detector as a module-level variable
detector = TrackingDetector()
def pre_search(request, search):
"""
Runs BEFORE the search request.
Returns False to stop the search if a tracking number is found,
True to continue with the search otherwise.
"""
query = search.search_query.query
result = detector.detect_carrier(query)
if result:
carrier, tracking_number = result
redirect_url = detector.CARRIERS[carrier]['url'].format(tracking_number)
# Add the redirect URL as the only result
search.result_container.answers.clear()
search.result_container.answers.add({
'answer': f'Redirecting to {carrier.upper()} tracking...',
'url': redirect_url
})
return False # Stop the search
return True # Continue with the search
def post_search(request, search):
"""
Runs AFTER the search request.
Can be used to modify results after the search is complete.
"""
# No post-processing needed for this plugin
return True
def on_result(request, search, result: Dict) -> bool:
"""
Runs for each result of each engine.
Returns True to keep the result, False to remove it.
"""
if 'url' in result:
# Update parsed_url if url is present
result['parsed_url'] = urlparse(result['url'])
# Check if the result URL contains a tracking number
query = result['url']
detected = detector.detect_carrier(query)
if detected:
carrier, tracking_number = detected
# Update the URL to the proper tracking URL
new_url = detector.CARRIERS[carrier]['url'].format(tracking_number)
result['url'] = new_url
result['parsed_url'] = urlparse(new_url)
# Add a note that this is a tracking link
result['title'] = f"[{carrier.upper()} Tracking] {result['title']}"
return True # Keep the result
"""
To use this plugin:
1. Save this file in searx/plugins/shipping_tracker.py
2. Add to settings.yml:
plugins:
- shipping_tracker
The plugin will:
1. Detect tracking numbers in search queries
2. Redirect to appropriate carrier tracking pages
3. Update any results that contain tracking numbers
4. Add tracking information to result titles
Example searches that will trigger redirects:
- "1Z999AA1234567890" (UPS)
- "9400 1234 5678 9000 0000 00" (USPS)
- "7891234567890" (FedEx)
- "JD014600887891234567" (DHL)
"""```
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
which is right way to do this?
Beta Was this translation helpful? Give feedback.
All reactions