Compare commits

...

8 Commits
main ... main

Author SHA1 Message Date
3199099481 readme markdown 2024-06-08 18:24:54 +00:00
32c5fc7291 Update readme.md 2024-06-08 18:18:02 +00:00
9222b9915d added bash script to build and zip 2024-06-08 19:50:48 +02:00
186db79995 i alrady hate lambda 2024-06-08 19:35:01 +02:00
36a2f49dcf updated readme 2024-06-08 17:06:33 +02:00
5fbdd9706e set job scraping limit to 20 jobs 2024-06-08 17:04:46 +02:00
20511db91f added in memory caching for 5 minutes 2024-06-08 16:56:35 +02:00
c20690f323 added aws lambda integrations 2024-06-08 16:48:00 +02:00
6 changed files with 91 additions and 32 deletions

3
.gitignore vendored
View File

@ -1,2 +1 @@
/thehub_cache
/jobs.json
/lambda-package

31
build.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
# Set variables
PACKAGE_DIR="./lambda-package"
BUILD_FILE="bootstrap"
ZIP_FILE="lambda-deployment.zip"
SOURCE_FILE="main.go"
# Delete the content of the lambda-package directory
rm -rf $PACKAGE_DIR/*
echo "Deleted the content of $PACKAGE_DIR"
# Set environment variables and build the Go project
GOOS=linux GOARCH=arm64 go build -o $BUILD_FILE -tags lambda.norpc $SOURCE_FILE
echo "Built the Go project with GOOS=linux and GOARCH=arm64"
# Move the build file to the lambda-package directory
mv $BUILD_FILE $PACKAGE_DIR/
echo "Moved the build file to $PACKAGE_DIR"
# Change directory to lambda-package
cd $PACKAGE_DIR
# Zip the contents of lambda-package into lambda-deployment.zip
zip -r $ZIP_FILE *
echo "Zipped the contents of $PACKAGE_DIR into $ZIP_FILE"
# Return to the original directory
cd -
echo "Script completed successfully"

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/antchfx/htmlquery v1.3.1 // indirect
github.com/antchfx/xmlquery v1.4.0 // indirect
github.com/antchfx/xpath v1.3.0 // indirect
github.com/aws/aws-lambda-go v1.47.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gocolly/colly v1.2.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect

2
go.sum
View File

@ -8,6 +8,8 @@ github.com/antchfx/xmlquery v1.4.0 h1:xg2HkfcRK2TeTbdb0m1jxCYnvsPaGY/oeZWTGqX/0h
github.com/antchfx/xmlquery v1.4.0/go.mod h1:Ax2aeaeDjfIw3CwXKDQ0GkwZ6QlxoChlIBP+mGnDFjI=
github.com/antchfx/xpath v1.3.0 h1:nTMlzGAK3IJ0bPpME2urTuFL76o4A96iYvoKFHRXJgc=
github.com/antchfx/xpath v1.3.0/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=

View File

@ -1,12 +1,12 @@
package main
import (
"encoding/json"
"context"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/gocolly/colly"
)
@ -30,6 +30,13 @@ type skills struct {
Typescript bool `json:"typescript"`
}
var (
jobs []job
lastFetch time.Time
cacheTTL = time.Minute * 5
jobLimit = 20
)
func skillChecker(description string) skills {
return skills{
React: strings.Contains(description, "React"),
@ -41,34 +48,32 @@ func skillChecker(description string) skills {
}
}
// Slice to store job details
var jobs []job
func fetchData() error {
func main() {
fName := "jobs.json"
file, err := os.Create(fName)
if err != nil {
log.Fatalf("Cannot create file %q: %s", fName, err)
}
defer file.Close()
baseUrl := "https://thehub.io"
searchString := "https://thehub.io/jobs?roles=frontenddeveloper&roles=fullstackdeveloper&roles=backenddeveloper&roles=devops&paid=true&countryCode=DK&sorting=newJobs"
// Instantiate default collector
c := colly.NewCollector(
// visit only the hub
colly.AllowedDomains("www.thehub.io", "thehub.io"),
// Cache responses to prevent multiple requests
colly.CacheDir("./thehub_cache"),
colly.CacheDir("./tmp"),
)
// Slice of excluded words in the job titles
excluded := []string{"senior", "lead"}
// Instantiate a new collector to visit the job details page
detailsCollector := c.Clone()
// Limit the number of jobs to fetch
jobCount := 0
// On every <div> element with class "card__content attribute call callback
c.OnHTML("div[class=card__content]", func(e *colly.HTMLElement) {
// Return if the job limit has been reached
if jobCount >= jobLimit {
return
}
// Get the title and ensure it doesn't contain any excluded words
title := e.ChildText("span.card-job-find-list__position")
for _, excludedWord := range excluded {
@ -87,6 +92,9 @@ func main() {
})
detailsCollector.OnHTML("div.view-job-details", func(e *colly.HTMLElement) {
if jobCount >= jobLimit {
return
}
// Get logo and trim the url
logo := e.ChildAttr("div.media-item__image", "style")
@ -106,6 +114,7 @@ func main() {
Skills: skillChecker(e.ChildText("content.text-block__content > span")),
}
jobs = append(jobs, jobDetails)
jobCount++
})
// Handle pagination
c.OnHTML("a.page-link", func(e *colly.HTMLElement) {
@ -116,15 +125,32 @@ func main() {
e.Request.Visit(fullNextPage)
}
})
// Visit the initial URL to start scraping
err := c.Visit("https://thehub.io/jobs?roles=frontenddeveloper&roles=fullstackdeveloper&roles=backenddeveloper&search=developer&paid=true&countryCode=DK&sorting=newJobs")
if err != nil {
return err
}
return nil
}
c.Visit(searchString)
// Encode jobs slice to JSON
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ") // Pretty-print with indentation
if err := encoder.Encode(jobs); err != nil {
log.Fatalf("Cannot write to file %q: %s", fName, err)
func handler(ctx context.Context) ([]job, error) {
// Check if cache is valid
if time.Since(lastFetch) < cacheTTL && len(jobs) > 0 {
return jobs, nil
}
fmt.Println("Job details successfully written to", fName)
// Fetch new data
err := fetchData()
if err != nil {
return nil, err
}
// Update cache timestamp
lastFetch = time.Now()
return jobs, nil
}
func main() {
lambda.Start(handler)
}

View File

@ -1,13 +1,13 @@
# The Hub Scraper
This is a simple scraper that extracts job details from the [The Hub](https://thehub.io) website.
deprecated as lambda was a bad solution for this, without setting up dynamodb, api etc. This will go live in a ec2 so it can write to local storage instead of running on demand.
## Usage
Go is fast but free tier lambda is not and I am not yet a smart man.
To run the scraper, simply execute the following command:
<del>This is a simple scraper that extracts job details from the [The Hub](https://thehub.io) website.</del>
```bash
go run scraper.go
```
<del>It's a fork of the original [The Hub Scraper](https://gitea.rannes.dev/rannes.dev/sw-jobs-go) by [Rannes](https://gitea.rannes.dev/rannes.dev).</del>
The scraper will create a `jobs.json` file in the current directory, which contains a list of job details in JSON format.
<del>## Usage</del>
<del>To run the scraper zip it deploy it to AWS Lambda and then call the function. </del>