refactored scrapers

This commit is contained in:
2025-02-16 15:07:59 -06:00
parent fc21549791
commit 283ed3eaea
13 changed files with 800 additions and 8 deletions
+31
View File
@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
package event
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}
+75
View File
@@ -0,0 +1,75 @@
package event
import (
"context"
"encoding/json"
)
type Fight struct {
Weight string `json:"weight"`
FighterA *Fighter `json:"fighter_a"`
FighterB *Fighter `json:"fighter_b"`
}
type Fighter struct {
Name string `json:"name"`
Image string `json:"image"`
Country string `json:"country"`
Link string `json:"link"`
}
func (e *Event) UnmarshalFights() []*Fight {
var fights []*Fight
_ = json.Unmarshal([]byte(e.Fights), &fights)
return fights
}
func (e *Event) MarshalFights(fights []*Fight) {
buf, _ := json.Marshal(&fights)
e.Fights = string(buf)
}
const upsertEvents = `-- name: UpsertEvents :exec
INSERT INTO
event (
url,
slug,
name,
location,
organization,
image,
date,
fights,
history
)
SELECT
json_extract (e.value, '$.url'),
json_extract (e.value, '$.slug'),
json_extract (e.value, '$.name'),
json_extract (e.value, '$.location'),
json_extract (e.value, '$.organization'),
json_extract (e.value, '$.image'),
json_extract (e.value, '$.date'),
json_extract (e.value, '$.fights'),
json_extract (e.value, '$.history')
FROM
json_each (?) AS e ON CONFLICT (url, slug) DO
UPDATE
SET
name = excluded.name,
location = excluded.location,
organization = excluded.organization,
image = excluded.image,
date = excluded.date,
fights = excluded.fights,
history = excluded.history
`
func (q *Queries) UpsertEvents(ctx context.Context, e []*Event) error {
b, err := json.Marshal(&e)
if err != nil {
return err
}
_, err = q.db.ExecContext(ctx, upsertEvents, string(b))
return err
}
+22
View File
@@ -0,0 +1,22 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
package event
type Event struct {
Url string `json:"url"`
Slug string `json:"slug"`
Name string `json:"name"`
Location string `json:"location"`
Organization string `json:"organization"`
Image string `json:"image"`
Date int64 `json:"date"`
Fights string `json:"fights"`
History string `json:"history"`
}
type Tapology struct {
Name string `json:"name"`
Url string `json:"url"`
}
+56
View File
@@ -0,0 +1,56 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.26.0
// source: query.sql
package event
import (
"context"
)
const getEvent = `-- name: GetEvent :one
SELECT
url, slug, name, location, organization, image, date, fights, history
FROM
event
WHERE
slug = ?
LIMIT
1
`
func (q *Queries) GetEvent(ctx context.Context, slug string) (Event, error) {
row := q.db.QueryRowContext(ctx, getEvent, slug)
var i Event
err := row.Scan(
&i.Url,
&i.Slug,
&i.Name,
&i.Location,
&i.Organization,
&i.Image,
&i.Date,
&i.Fights,
&i.History,
)
return i, err
}
const getTapology = `-- name: GetTapology :one
SELECT
name, url
FROM
tapology
WHERE
name = ?
LIMIT
1
`
func (q *Queries) GetTapology(ctx context.Context, name string) (Tapology, error) {
row := q.db.QueryRowContext(ctx, getTapology, name)
var i Tapology
err := row.Scan(&i.Name, &i.Url)
return i, err
}