diff options
Diffstat (limited to 'js')
| -rw-r--r-- | js/embed.go | 3 | ||||
| -rw-r--r-- | js/template.js | 27 |
2 files changed, 30 insertions, 0 deletions
diff --git a/js/embed.go b/js/embed.go index 5413e77..dcc8d93 100644 --- a/js/embed.go +++ b/js/embed.go @@ -6,3 +6,6 @@ import _ "embed" //go:embed flyscrape_bundle.js var Flyscrape string + +//go:embed template.js +var Template []byte diff --git a/js/template.js b/js/template.js new file mode 100644 index 0000000..d33adc5 --- /dev/null +++ b/js/template.js @@ -0,0 +1,27 @@ +import { parse } from "flyscrape"; + +export const options = { + url: "https://news.ycombinator.com/", // Specify the URL to start scraping from. + depth: 1, // Specify how deep links should be followed (0 = no follow). + allowedDomains: ["news.ycombinator.com"], // Specify the allowed domains to follow. +} + +export default function({ html, url }) { + const $ = parse(html); + + return { + title: $('title').text(), + entries: $('.athing').toArray().map(entry => { + const link = $(entry).find('.titleline > a'); + const rank = $(entry).find('.rank'); + const points = $(entry).next().find('.score'); + + return { + title: link.text(), // Extract the title text. + url: link.attr('href'), // Extract the link href. + rank: parseInt(rank.text().slice(0, -1)), // Extract and cleanup the rank. + points: parseInt(points.text().replace(' points', '')), // Extract and cleanup the points. + } + }), + }; +} |