Hello, recently i decided i wanted to get all ads from a job website’s api, i get json in return. In the Json there is many ads for jobs, and also how many pages is available. I’m a trainee so this is good practise.
The problem i am having is that it is very slow having to loop through the json twice, first time to get the totalPages variable, and then secondly to store everything in structs. Secondly i am writing the same code twice, which is really annoying when i hate repeating myself. I am getting some very long run times that i would like to shorten, i wrote the same thing in python and it was actually quicker.
Thanks for any responds in advance! I’m here to learn and move on
Code:
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
type JsonData struct {
Content []JsonContent `json:"content"`
TotalElements int `json:"totalElements"`
PageNumber int `json:"pageNumber"`
PageSize int `json:"pageSize"`
TotalPages int `json:"totalPages"`
First bool `json:"first"`
Last bool `json:"last"`
Sort string `json:"sort"`
}
type JsonContent struct {
Uuid string `json:"uuid"`
Published string `json:"published"`
Expires string `json:"expires"`
Updated string `json:"updated"`
WorkLoc []WorkLocations `json:"workLocations"`
Title string `json:"title"`
Description string `json:"description"`
SourceUrl string `json:"sourceurl"`
Source string `json:"source"`
ApplicationDue string `json:"applicationDue"`
OccupationCat []OccupationCategories `json:"occupationCategories"`
JobTitle string `json:"jobtitle"`
Link string `json:"link"`
Employ Employer `json:"employer"`
EngagementType string `json:"engagementtype"`
Extent string `json:"extent"`
StartTime string `json:"starttime"`
PositionCount interface{} `json:"positioncount"`
Sector string `json:"sector"`
}
type WorkLocations struct {
Country string `json:"country"`
Address string `json:"address"`
City string `json:"city"`
PostalCode string `json:"postalCode"`
County string `json:"county"`
Municipal string `json:"municipal"`
}
type OccupationCategories struct {
Level1 string `json:"level1"`
Level2 string `json:"level2"`
}
type Employer struct {
Name string `json:"name"`
OrgNr string `json:"orgnr"`
Description string `json:"description"`
Homepage interface{} `json:"homepage"`
}
type TotalPageStruct struct {
TotalPages int `json:"totalPages"`
}
func main() {
fmt.Println("Starting..")
var totalPages TotalPageStruct
var datas JsonData
var keyWords = []string{"Python", "Golang", "Ansible", "Powershell", "Support", "Kundeservice", "Servicekonsulent", "kundebehandling", "Medarbeider"}
getTotalPages(&totalPages)
i := 1
for i < totalPages.TotalPages {
url := "https://arbeidsplassen.nav.no/public-feed/api/v1/ads?page=" + strconv.Itoa(i) + "&size=5000&published=%2A&county=Oslo"
// Create a Bearer string by appending string access token
var bearer = "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJwdWJsaWMudG9rZW4udjFAbmF2Lm5vIiwiYXVkIjoiZmVlZC1hcGktdjEiLCJpc3MiOiJuYXYubm8iLCJpYXQiOjE1NTc0NzM0MjJ9.jNGlLUF9HxoHo5JrQNMkweLj_91bgk97ZebLdfx3_UQ"
// Create a new request using http
req, err := http.NewRequest("GET", url, nil)
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &datas)
if err != nil {
log.Fatal(err)
}
dataMap := lookForWork(&datas, keyWords)
for k, v := range dataMap {
if v > 0 {
fmt.Println(k, v)
}
}
i++
}
fmt.Println("Ended..")
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func getTotalPages(totalPages *TotalPageStruct) {
// Getting the totalPages variable
url := "https://arbeidsplassen.nav.no/public-feed/api/v1/ads?page=1&size=5000&published=%2A&county=Oslo"
// Create a Bearer string by appending string access token
var bearer = "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJwdWJsaWMudG9rZW4udjFAbmF2Lm5vIiwiYXVkIjoiZmVlZC1hcGktdjEiLCJpc3MiOiJuYXYubm8iLCJpYXQiOjE1NTc0NzM0MjJ9.jNGlLUF9HxoHo5JrQNMkweLj_91bgk97ZebLdfx3_UQ"
// Create a new request using http
req, err := http.NewRequest("GET", url, nil)
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &totalPages)
if err != nil {
log.Fatal(err)
}
}
func lookForWork(data *JsonData, keyWords []string) map[string]int {
var dataMap = map[string]int{}
for _, v := range data.Content {
dataMap[v.Link] = 0
for _, keyWord := range keyWords {
if strings.Contains(strings.ToLower(v.Description), strings.ToLower(keyWord)) {
//fmt.Println(v.Link, keyWord)
dataMap[v.Link] += 1
}
}
}
return dataMap
}