Skip to content

Commit

Permalink
Merge branch 'open-dev' into open
Browse files Browse the repository at this point in the history
  • Loading branch information
StJudeWasHere committed Nov 26, 2024
2 parents 3592cee + f426a13 commit 1ec42cb
Show file tree
Hide file tree
Showing 20 changed files with 882 additions and 346 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ require (
github.com/spf13/viper v1.19.0
github.com/temoto/robotstxt v1.1.2
github.com/turk/go-sitemap v0.0.0-20210912154218-82ad01095e30
golang.org/x/crypto v0.28.0
golang.org/x/net v0.30.0
golang.org/x/text v0.19.0
golang.org/x/crypto v0.29.0
golang.org/x/net v0.31.0
golang.org/x/text v0.20.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -44,7 +44,7 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/sys v0.27.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,17 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -156,17 +156,17 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
172 changes: 172 additions & 0 deletions internal/archiver/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package archiver

import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
"slices"
"sort"
"strings"

"github.com/slyrz/warc"
)

type Reader struct {
waczPath string
}

func NewReader(waczPath string) *Reader {
return &Reader{
waczPath: waczPath,
}
}

// ReadArchive reads the archive and returns the contents of the warc record for
// the specified URL as a string.
func (s *Reader) ReadArchive(urlStr string) (content string) {
wacz, err := zip.OpenReader(s.waczPath)
if err != nil {
log.Printf("failed to open reader %v", err)
return ""
}
defer wacz.Close()

record, err := s.getCDXEntry(wacz, urlStr)
if err != nil {
return ""
}

file, err := s.getZipFile(wacz, "data/data.warc")
if err != nil {
return ""
}

zipoffset, err := file.DataOffset()
if err != nil {
return ""
}

f, err := os.OpenFile(s.waczPath, os.O_RDWR, 0644)
if err != nil {
log.Println(err)
return
}
defer f.Close()

buffer := make([]byte, record.Length)

_, err = f.ReadAt(buffer, zipoffset+int64(record.Offset))
if err != nil && err.Error() != "EOF" {
log.Println(err)
}
wr, _ := warc.NewReader(bytes.NewReader(buffer))
r, err := wr.ReadRecord()
if err != nil {
log.Println(err)
}

c, _ := io.ReadAll(r.Content)
return string(c)
}

// getCDXEntry Looks for the specified URL in the index file and returns an IndexEntry if found,
// otherwise it returns an error.
func (s *Reader) getCDXEntry(wacz *zip.ReadCloser, urlStr string) (*IndexEntry, error) {
file, err := s.getZipFile(wacz, "indexes/index.cdx")
if err != nil {
return nil, err
}
offset, err := file.DataOffset()
if err != nil {
return nil, err
}
size := file.FileInfo().Size()

parsedURL, err := url.Parse(urlStr)
if err != nil {
return nil, err
}

domainParts := strings.Split(parsedURL.Hostname(), ".")
slices.Reverse(domainParts)
searchableURL := strings.Join(domainParts, ",")
searchableURL = searchableURL + ")" + parsedURL.RequestURI()

line, err := s.searchFileSegment(offset, size, searchableURL)
if err != nil {
return nil, err
}

var record IndexEntry

jsonStart := strings.Index(line, "{")
if jsonStart == -1 {
fmt.Println("JSON data not found in line:", line)
return nil, fmt.Errorf("invalid IndexEntry %s", line)
}

// Extract the JSON substring
jsonData := line[jsonStart:]

err = json.Unmarshal([]byte(jsonData), &record)
if err != nil {
return nil, fmt.Errorf("error parsing JSON: %w", err)
}

return &record, nil
}

// getZipFile returns a *zip.File from a wacz file. If not found it returns an error.
func (s *Reader) getZipFile(wacz *zip.ReadCloser, waczFile string) (*zip.File, error) {
for _, file := range wacz.File {
if file.Name == waczFile {
return file, nil
}
}

return nil, errors.New("warc file file not found")
}

// searchFileSegment searches the target string in WACZ file index using bynary search.
// It loads the index contents in memory.
func (s *Reader) searchFileSegment(offset, length int64, target string) (string, error) {
file, err := os.Open(s.waczPath)
if err != nil {
return "", fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()

// Seek to the specified offset
_, err = file.Seek(offset, 0)
if err != nil {
return "", fmt.Errorf("failed to seek to offset: %v", err)
}

// Read the specified length of bytes
buffer := make([]byte, length)
_, err = file.Read(buffer)
if err != nil {
return "", fmt.Errorf("failed to read segment: %v", err)
}

// Split the buffer into lines
lines := strings.Split(string(buffer), "\n")

// Perform binary search on lines in memory using sort.Search
index := sort.Search(len(lines), func(i int) bool {
return lines[i] >= target
})

// Check if the found line starts with the target prefix
if index < len(lines) && strings.HasPrefix(lines[index], target) {
return lines[index], nil // Found the line
}

return "", fmt.Errorf("no line starting with '%s' found", target)
}
Loading

0 comments on commit 1ec42cb

Please sign in to comment.