Added fetch script for UK Gov FindATender service which is based on OCDS (with issues...)
This commit is contained in:
1 parent
ff33ef082a
commit
e43b7a8370
16 files changed
+744
-11
No files matched your search
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
Copyright © 2026 Craig Williams <craig@avsdev.uk>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/cli"
|
||||
"git.avsdev.uk/Go/job-search/internal/findATender"
|
||||
"git.avsdev.uk/Go/job-search/internal/findATender/ocds"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func filter(reqs []ocds.Release, tag string) []ocds.Release {
|
||||
var resp []ocds.Release
|
||||
for _, req := range reqs {
|
||||
if slices.Contains(req.Tag, tag) {
|
||||
resp = append(resp, req)
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func commandOnce(opts *cli.Options) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "once",
|
||||
Short: "Fetch current opportunities - single shot",
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
slog.Debug("fetch.once.PersistentPreRun")
|
||||
},
|
||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||
slog.Debug("fetch.once.PersistentPostRun")
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
slog.Debug("fetch.once.Run")
|
||||
|
||||
conf, err := opts.RunSetup(cmd, true, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("no configuration loaded (%w)", err)
|
||||
}
|
||||
|
||||
since, sinceErr := cmd.Flags().GetTime("since")
|
||||
if sinceErr != nil {
|
||||
return sinceErr
|
||||
}
|
||||
|
||||
ft := findATender.New(conf, conf.Sources.FindATender.APIRoot)
|
||||
defer ft.Shutdown()
|
||||
|
||||
ignoreRateLimit, _ := cmd.Flags().GetBool("ignore-rate-limit")
|
||||
|
||||
results, err := ft.FetchAll(since, !ignoreRateLimit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
planning, _ := cmd.Flags().GetBool("planning")
|
||||
opportunities, _ := cmd.Flags().GetBool("opportunities")
|
||||
awards, _ := cmd.Flags().GetBool("awards")
|
||||
|
||||
if (planning && (opportunities || awards)) ||
|
||||
(opportunities && (planning || awards)) ||
|
||||
(awards && (planning || opportunities)) {
|
||||
return fmt.Errorf(
|
||||
"--planning, --opportunities and --awards are exclusive, only one can be provided",
|
||||
)
|
||||
}
|
||||
|
||||
filterTag := ""
|
||||
if planning {
|
||||
filterTag = "planning"
|
||||
}
|
||||
if opportunities {
|
||||
filterTag = "opportunity"
|
||||
}
|
||||
if awards {
|
||||
filterTag = "award"
|
||||
}
|
||||
|
||||
if len(filterTag) > 0 {
|
||||
results = filter(results, filterTag)
|
||||
}
|
||||
|
||||
jbytes, err := json.MarshalIndent(results, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jbytes))
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
cmd.Flags().Time(
|
||||
"since",
|
||||
time.Time{},
|
||||
[]string{
|
||||
time.DateOnly, time.DateTime,
|
||||
},
|
||||
"contract updates published since YYYY-MM-DD[ hh:mm:ss]",
|
||||
)
|
||||
cmd.Flags().Bool(
|
||||
"planning", false, "only return planning (i.e. exclude opportunities and awards)",
|
||||
)
|
||||
cmd.Flags().Bool(
|
||||
"opportunities", false, "only return opportunities (i.e. exclude awards and planning)",
|
||||
)
|
||||
cmd.Flags().Bool(
|
||||
"awards", false, "only return awards (i.e. exclude planning and opportunities)",
|
||||
)
|
||||
cmd.Flags().Bool(
|
||||
"ignore-rate-limit", false, "ignore the rate limit and fail if exceeded",
|
||||
)
|
||||
return cmd
|
||||
}
|
||||
Reference in new issue
Block a user