finishing with the finding and search file
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
module gitea.zacksolutions.dev/Zakaria/find
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type xkcd struct {
|
||||||
|
Num int `json:"num"`
|
||||||
|
Day string `json:"day"`
|
||||||
|
Month string `json:"month"`
|
||||||
|
Year string `json:"year"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Transcript string `json:"transcript"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
fmt.Fprintln(os.Stderr, "No file was given")
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
filename := os.Args[1]
|
||||||
|
if len(os.Args) < 3 {
|
||||||
|
fmt.Fprintln(os.Stderr, "No search tags were given")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
items []xkcd
|
||||||
|
terms []string
|
||||||
|
input io.ReadCloser
|
||||||
|
cnt int
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
// okay so let's decode the file, get search terms and search
|
||||||
|
|
||||||
|
if input, err = os.Open(filename); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Corrupt file: %s\n", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
if err = json.NewDecoder(input).Decode(&items); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Bad json Fromat: %s", err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "read %d comics\n", len(items))
|
||||||
|
for _, t := range os.Args[2:] {
|
||||||
|
terms = append(terms, strings.ToLower(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
outer:
|
||||||
|
|
||||||
|
for _, item := range items {
|
||||||
|
title := strings.ToLower(item.Title)
|
||||||
|
transcript := strings.ToLower(item.Transcript)
|
||||||
|
|
||||||
|
for _, term := range terms {
|
||||||
|
if !strings.Contains(title, term) && !strings.Contains(transcript, term) {
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("https://xkcd.com/%d/%s/%s/%s\n", item.Num, item.Day, item.Month, item.Year)
|
||||||
|
cnt++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(os.Stderr, "found %d comics\n", cnt)
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -51,6 +51,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
fmt.Fprint(output, "[")
|
fmt.Fprint(output, "[")
|
||||||
defer fmt.Fprint(output, "]")
|
defer fmt.Fprint(output, "]")
|
||||||
|
|
||||||
for i := 1; fails < 2; i++ {
|
for i := 1; fails < 2; i++ {
|
||||||
if data = getComic(i); data == nil {
|
if data = getComic(i); data == nil {
|
||||||
fails++
|
fails++
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user