-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(route): Add New Route for UPS Tracking #17941
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b88daab
New Routes for UPS Tracking
Aquabet 2274ea3
New Routes for UPS Tracking
Aquabet 612d9cd
trying to fix Vitest puppeteer on Node lts/-1
Aquabet 437d524
Add tracking feature to UPS route
Aquabet b98da60
Merge branch 'DIYgod:master' into ups
Aquabet 66b15c6
fix: update tracking logic in UPS route
Aquabet 8e92abc
Add guid to track request
Aquabet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: 'UPS', | ||
url: 'ups.com', | ||
description: 'United Parcel Service (UPS) updates, news, and tracking RSS feeds.', | ||
|
||
zh: { | ||
name: 'UPS(联合包裹服务公司)', | ||
description: '联合包裹服务公司(UPS)的更新、新闻和追踪 RSS 源。', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { Route } from '@/types'; | ||
import { load } from 'cheerio'; | ||
import { parseDate } from '@/utils/parse-date'; | ||
import puppeteer from '@/utils/puppeteer'; | ||
|
||
export const route: Route = { | ||
path: '/track/:trackingNumber', | ||
categories: ['other'], | ||
example: '/ups/track/1Z78R6790470567520', | ||
parameters: { trackingNumber: 'The UPS tracking number (e.g., 1Z78R6790470567520).' }, | ||
features: { | ||
requireConfig: false, | ||
requirePuppeteer: false, | ||
antiCrawler: false, | ||
supportBT: false, | ||
supportPodcast: false, | ||
supportScihub: false, | ||
}, | ||
name: 'Tracking', | ||
maintainers: ['Aquabet'], | ||
handler, | ||
}; | ||
|
||
async function handler(ctx) { | ||
const { trackingNumber } = ctx.req.param(); | ||
const url = `https://www.ups.com/track?loc=en_US&tracknum=${trackingNumber}`; | ||
|
||
const browser = await puppeteer(); | ||
const page = await browser.newPage(); | ||
|
||
await page.setRequestInterception(true); | ||
|
||
// skip loading images, stylesheets, and fonts | ||
page.on('request', (request) => { | ||
if (['image', 'stylesheet', 'font', 'ping', 'fetch'].includes(request.resourceType())) { | ||
request.abort(); | ||
} else { | ||
request.continue(); | ||
} | ||
}); | ||
|
||
await page.goto(url, { waitUntil: 'domcontentloaded' }); | ||
|
||
const viewDetailsButton = '#st_App_View_Details'; | ||
try { | ||
await page.waitForSelector(viewDetailsButton); | ||
await page.click(viewDetailsButton); | ||
} catch { | ||
return { | ||
title: `UPS Tracking - ${trackingNumber}`, | ||
link: url, | ||
item: [], | ||
}; | ||
} | ||
|
||
await page.waitForSelector('tr[id^="stApp_activitydetails_row"]'); | ||
|
||
const content = await page.content(); | ||
await browser.close(); | ||
|
||
const $ = load(content); | ||
|
||
const rows = $('tr[id^="stApp_activitydetails_row"]'); | ||
const items: { title: string; link: string; description: string; pubDate: Date }[] = []; | ||
|
||
rows.each((i, el) => { | ||
const dateTimeRaw = $(el).find(`#stApp_activitiesdateTime${i}`).text() || 'Not Provided'; | ||
|
||
const dateTimeStr = dateTimeRaw | ||
.trim() | ||
.replace(/(\d{1,}\/\d{1,}\/\d{4})(\d{1,}:\d{1,}\s[AP]\.?M\.?)/, '$1 $2') // add a space between date and time | ||
.replaceAll('P.M.', 'PM') | ||
.replaceAll('A.M.', 'AM'); | ||
|
||
const pubDate = parseDate(dateTimeStr); | ||
|
||
const activityCellText = $(el) | ||
.find(`#stApp_milestoneActivityLocation${i}`) | ||
.text() | ||
.trim() | ||
.replaceAll(/\s*\n+\s*/g, '\n'); | ||
|
||
const lines = activityCellText | ||
.split('\n') | ||
.map((l) => l.trim()) | ||
.filter(Boolean); | ||
|
||
// Situation 0: There is text within the strong element | ||
// Example: ["Delivered", "DELIVERED", "REDMOND, WA, United States"] | ||
// Situation 1: strong is empty => the first line in lines is the status | ||
// Example: ["Departed from Facility", "Seattle, WA, United States"] | ||
const status = lines[0]; | ||
const location = lines.at(-1) || ''; | ||
|
||
items.push({ | ||
title: status, | ||
link: url, | ||
description: ` | ||
Status: ${status} <br> | ||
Location: ${location} <br> | ||
Date and Time: ${dateTimeStr} | ||
`, | ||
pubDate, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use Array.prototype.map() instead. |
||
}); | ||
|
||
return { | ||
title: `UPS Tracking - ${trackingNumber}`, | ||
link: url, | ||
item: items, | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use
RSSHub/lib/types.ts
Line 32 in 0a93a74