Skip to content

Instantly share code, notes, and snippets.

@maikxchd
Last active January 12, 2023 05:30
Show Gist options
  • Save maikxchd/307265424d1c4e526a7338175197461e to your computer and use it in GitHub Desktop.
Save maikxchd/307265424d1c4e526a7338175197461e to your computer and use it in GitHub Desktop.
MBKM Check Script in Go

MBKM Checker

This tool is a system to check the MSIB site for updates on certain positions through scraping the website and searching for certain key words. It's dumb and very not accurate but i don't wanna work on it more than i need to. The terraform file deploys the code to AWS Lambda that runs the function periodically every 24 hours.

Remember recheck and refill the placeholder values in the Terraform file for a smooth deployment.

package main
import (
"fmt"
"net/http"
"time"
"golang.org/x/net/html"
)
//insert positions you wanna monitor here
func main() {
keywords := []string{
"gojek",
"tiket.com",
"traveloka",
"grab",
"cloud",
"infra",
"infrastructure",
"security",
"devops",
"devsecops"}
//insert website here
for {
resp, err := http.Get("https://kampusmerdeka.kemdikbud.go.id/program/magang/browse/")
if err != nil {
fmt.Println("Error:", err)
continue
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
fmt.Println("Error:", err)
continue
}
count := checkForKeywords(doc, keywords)
fmt.Printf("Found %d instances of keywords\n", count)
//frequency of detection, can be replaced with a lambda function if youre into that
// time.Sleep(24 * time.Hour)
}
}
func checkForKeywords(n *html.Node, keywords []string) int {
if n.Type == html.TextNode {
for _, keyword := range keywords {
if contains(n.Data, keyword) {
return 1
}
}
}
count := 0
for c := n.FirstChild; c != nil; c = c.NextSibling {
count += checkForKeywords(c, keywords)
}
return count
}
func contains(s, substr string) bool {
return strings.Index(s, substr) != -1
}
provider "aws" {
region = "us-east-1"
}
resource "aws_lambda_function" "example" {
function_name = "example"
handler = "main"
runtime = "go1.x"
role = aws_iam_role.iam_for_lambda.arn
s3_bucket = aws_s3_bucket.example_bucket.bucket
s3_key = "example.zip"
}
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy" "iam_for_lambda" {
name = "iam_for_lambda"
role = aws_iam_role.iam_for_lambda.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "logs:*",
"Resource": "arn:aws:logs:*:*:*"
}
]
}
EOF
}
resource "aws_s3_bucket" "example_bucket" {
bucket = "example-bucket"
acl = "private"
}
resource "aws_s3_bucket_object" "example_zip" {
bucket = aws_s3_bucket.example_bucket.bucket
key = "example.zip"
//put the go file here
source = "check.zip"
}
//increase or decrease frequency
resource "aws_cloudwatch_event_rule" "example_rule" {
name = "example_rule"
schedule_expression = "rate(24 hours)"
}
resource "aws_cloudwatch_event_target" "example_target" {
rule = aws_cloudwatch_event_rule.example_rule.name
target_id = "example_target"
arn = aws_lambda_function.example.arn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment