-
Notifications
You must be signed in to change notification settings - Fork 12
Sites 32286 1 #797
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
Open
anagarwa
wants to merge
2
commits into
main
Choose a base branch
from
SITES-32286_1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sites 32286 1 #797
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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
This file contains hidden or 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,120 @@ | ||
import { PutObjectCommand } from '@aws-sdk/client-s3'; | ||
import { hasText } from './functions.js'; | ||
|
||
/** | ||
* Generates storage path for scraped content that matches run-sqs.js expectations | ||
*/ | ||
export function getScrapedContentPath(siteId, url, prefix = 'scrapes') { | ||
const urlObj = new URL(url); | ||
const urlPath = urlObj.pathname.replace(/\/$/, '') || '/'; | ||
return `${prefix}/${siteId}${urlPath}/scrape.json`; | ||
} | ||
|
||
/** | ||
* Stores scraped content in S3 in the format expected by run-sqs.js | ||
*/ | ||
export async function storeScrapedContent(s3Client, bucketName, siteId, url, content, options = {}) { | ||
const { prefix = 'scrapes' } = options; | ||
|
||
const filePath = getScrapedContentPath(siteId, url, prefix); | ||
|
||
const command = new PutObjectCommand({ | ||
Bucket: bucketName, | ||
Key: filePath, | ||
Body: JSON.stringify(content), | ||
ContentType: 'application/json', | ||
}); | ||
|
||
await s3Client.send(command); | ||
console.log(`Successfully stored scraped content at: ${filePath}`); | ||
|
||
return filePath; | ||
} | ||
|
||
/** | ||
* Simple web scraper function | ||
*/ | ||
export async function scrapeUrl(url, options = {}) { | ||
const { | ||
customHeaders = {}, | ||
timeout = 15000, | ||
userAgent = 'SpaceCat-Scraper/1.0' | ||
} = options; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
headers: { | ||
'User-Agent': userAgent, | ||
...customHeaders, | ||
}, | ||
signal: AbortSignal.timeout(timeout), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP ${response.status}: ${response.statusText}`); | ||
} | ||
|
||
const rawBody = await response.text(); | ||
const finalUrl = response.url; | ||
|
||
return { | ||
finalUrl, | ||
status: response.status, | ||
headers: Object.fromEntries(response.headers.entries()), | ||
rawBody, | ||
scrapeTime: Date.now(), | ||
scrapedAt: new Date().toISOString(), | ||
}; | ||
} catch (error) { | ||
throw new Error(`Failed to scrape ${url}: ${error.message}`); | ||
} | ||
} | ||
|
||
/** | ||
* Batch scrape multiple URLs and store them in S3 | ||
*/ | ||
export async function scrapeAndStoreUrls(s3Client, bucketName, siteId, urls, options = {}) { | ||
const results = []; | ||
|
||
for (const url of urls) { | ||
try { | ||
console.log(`Scraping: ${url}`); | ||
const scrapeResult = await scrapeUrl(url, options); | ||
|
||
const contentToStore = { | ||
finalUrl: scrapeResult.finalUrl, | ||
scrapeResult, | ||
userAgent: options.userAgent || 'SpaceCat-Scraper/1.0', | ||
scrapeTime: scrapeResult.scrapeTime, | ||
scrapedAt: scrapeResult.scrapedAt, | ||
}; | ||
|
||
const storagePath = await storeScrapedContent( | ||
s3Client, | ||
bucketName, | ||
siteId, | ||
url, | ||
contentToStore, | ||
options | ||
); | ||
|
||
results.push({ | ||
url, | ||
finalUrl: scrapeResult.finalUrl, | ||
status: 'COMPLETE', | ||
location: storagePath, | ||
scrapeResult, | ||
}); | ||
|
||
} catch (error) { | ||
console.error(`Failed to scrape ${url}:`, error); | ||
results.push({ | ||
url, | ||
status: 'FAILED', | ||
error: error.message, | ||
}); | ||
} | ||
} | ||
|
||
return results; | ||
} |
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.
Check notice
Code scanning / CodeQL
Unused variable, import, function or class Note
Copilot Autofix
AI 4 months ago
To fix the problem, the unused import
hasText
should be removed from the file. This will eliminate the unnecessary clutter and improve code readability. The change is straightforward and involves deleting the import statement forhasText
.