Skip to content

Instantly share code, notes, and snippets.

@pgaskin
Created June 15, 2019 18:26
Show Gist options
  • Select an option

  • Save pgaskin/1012a3f5feaae93de2f34d235feddadb to your computer and use it in GitHub Desktop.

Select an option

Save pgaskin/1012a3f5feaae93de2f34d235feddadb to your computer and use it in GitHub Desktop.
package main
import (
"archive/zip"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
rc, err := zip.OpenReader(os.Args[1])
if err != nil {
panic(err)
}
var isbn []string
seen := map[string]bool{}
for _, f := range rc.File {
if e := findISBN13(f.Name); len(e) == 1 {
isbn = append(isbn, e[0])
seen[e[0]] = true
}
if ext := filepath.Ext(f.Name); ext != ".html" && ext != ".xhtml" {
continue
}
r, err := f.Open()
if err != nil {
panic(err)
}
buf, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
for _, v := range findISBN13(string(buf)) {
if !seen[v] {
isbn = append(isbn, v)
seen[v] = true
}
}
}
fmt.Println(isbn)
}
var isbnRe = regexp.MustCompile(`9(?:[ -]?)(?:78|79)(?:[ -]?[0-9]){10}`)
func findISBN13(txt string) []string {
isbn := isbnRe.FindAllString(txt, -1)
r := strings.NewReplacer(" ", "", "-", "")
for i := range isbn {
isbn[i] = r.Replace(isbn[i])
}
return isbn
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment