Documentation
¶
Overview ¶
Package crawler contains the parsing and download rules used while importing external pages.
Keep framework and browser quirks here. The main application should provide callbacks for effects such as downloading resources, deciding whether to persist a resource, and mapping imported documents to local paths. This keeps new parsing rules testable without a database, HTTP server, or admin UI.
Index ¶
- Variables
- func CanonicalWholeSitePagePath(rawPath string) string
- func CleanPath(rawPath string) string
- func CloneURL(sourceURL *url.URL) *url.URL
- func CurrentWholeSiteImportURL(pageQueue []WholeSitePageJob) string
- func DownloadHTML(client *http.Client, pageURL *url.URL, applyHeaders func(*http.Request)) (string, bool, error)
- func DownloadHTMLContext(ctx context.Context, client *http.Client, pageURL *url.URL, ...) (string, bool, error)
- func EffectiveResourceContentType(resourceURL, contentType string) string
- func ErrorReason(err error) string
- func ExtractPageLinks(htmlSource string, baseURL, siteURL *url.URL) []*url.URL
- func FirstPathSegment(rawPath string) string
- func HasAllowedResourceExtension(resourceURL string) bool
- func IsPageURL(pageURL *url.URL) bool
- func IsSuspiciousReference(rawRef string) bool
- func IsWholeSitePageURL(pageURL *url.URL) bool
- func IsWholeSitePageURLString(rawURL string) bool
- func NewSessionClient(timeout time.Duration, transport http.RoundTripper) *http.Client
- func NormalizeMirroredAssetReference(assetPath string) string
- func NormalizeURL(rawRef string, baseURL *url.URL, referenceContext ReferenceContext) (string, bool)
- func NormalizedResourceContentType(contentTypeHeader string) string
- func OriginRootURL(baseURL *url.URL) *url.URL
- func PreviewResourceKind(tagName, attributeName, rawRef string) string
- func ResourceExtension(rawRef string) string
- func ResourceExtensionFromContentType(contentType string) string
- func ResourceKindFromContentType(contentType string) string
- func ResourceKindFromURL(resourceURL string) string
- func RewriteCSSImportReferences(source string, rewriteSingle func(string) string) string
- func RewriteCSSURLReferences(source string, rewriteSingle func(string) string) string
- func RewriteDocumentResourceReferences(source string, rewriteSingle func(string) string) string
- func RewriteSrcSetReferences(source string, rewriteSingle func(string) string) string
- func SameHost(leftURL, rightURL *url.URL) bool
- func ShouldRewriteJSResourceReference(rawReference string) bool
- func SplitDataURL(rawRef string) (string, string, bool)
- func SplitStaticResourceURLTrailingText(rawURL string) (string, string)
- func WholeSiteLocalLink(basePath string, startURL, pageURL *url.URL) string
- func WholeSiteLocalPath(basePath string, startURL, pageURL *url.URL) string
- func WholeSitePageKey(pageURL *url.URL) string
- type DecodeResult
- type HTMLDownloadResult
- type HTMLDownloadRetryOptions
- type ImportRequest
- type ImportResult
- type ImportedPage
- type Parser
- type ProgressEvent
- type ProgressTracker
- type ReferenceContext
- type ResourcePreview
- type SourceOptions
- type WholeSitePageJob
Constants ¶
This section is empty.
Variables ¶
var KnownResourceKindsByExtension = map[string]string{}/* 142 elements not displayed */
Functions ¶
func CurrentWholeSiteImportURL ¶
func CurrentWholeSiteImportURL(pageQueue []WholeSitePageJob) string
func DownloadHTML ¶
func DownloadHTMLContext ¶
func ErrorReason ¶
func ExtractPageLinks ¶
func FirstPathSegment ¶
func IsSuspiciousReference ¶
func IsWholeSitePageURL ¶
func NewSessionClient ¶
func NormalizeURL ¶
func PreviewResourceKind ¶
func ResourceExtension ¶
func ResourceKindFromURL ¶
func RewriteCSSURLReferences ¶
func RewriteSrcSetReferences ¶
func WholeSiteLocalLink ¶
func WholeSiteLocalPath ¶
func WholeSitePageKey ¶
Types ¶
type DecodeResult ¶
func DecodeHTML ¶
func DecodeHTML(htmlBytes []byte, contentType string) DecodeResult
func DecodeText ¶
func DecodeText(textBytes []byte, contentType string) DecodeResult
type HTMLDownloadResult ¶
type HTMLDownloadResult struct {
HTML string
IsHTML bool
ResolvedURL *url.URL
Status string
StatusCode int
Encoding string
EncodingSource string
EncodingCertain bool
}
HTMLDownloadResult carries the document and transport metadata together so callers can keep HTTP details out of application-level download code.
func DownloadHTMLPageContext ¶
type ImportRequest ¶
type ImportRequest struct {
Domain string
PagePath string
SourceURL string
RemoteSourceURL *url.URL
HTML string
Context context.Context
ProgressToken string
DownloadTotal int
DownloadTotalBytes int64
SelectedResourceURLs map[string]struct{}
SourceOptions SourceOptions
}
ImportRequest is the application-facing input for single page and whole site imports.
type ImportResult ¶
type ImportResult struct {
RedirectPath string
FailedTotal int
FailedURLs []string
FailedReasons map[string]string
}
ImportResult is returned after the importer has persisted everything it could fetch.
type ImportedPage ¶
ImportedPage is a crawled remote document mapped to a local SiteBrush page.
type Parser ¶
type Parser struct {
NormalizeURL func(rawRef string, baseURL *url.URL, referenceContext ReferenceContext) (string, bool)
RewriteResourceReference func(rawRef string, baseURL *url.URL, depth int, referenceContext ReferenceContext) string
RewriteDocumentResourceReference func(rawRef string, baseURL *url.URL, depth int) string
DocumentURLRewriter func(normalizedURL string) (string, bool)
ShouldBlankEmbeddedDocumentReference func(tagName, normalizedURL string) bool
ShouldRewriteImageAltResource func(rawRef string, baseURL *url.URL) bool
}
Parser rewrites references found in external pages.
The package owns parsing rules. The application owns side effects: fetching, quota decisions, asset persistence, whole-site page routing, and progress. Add new site-specific or framework-specific parsing rules here by extending the relevant Rewrite* method or adding a narrow helper next to the rule it supports.
func (Parser) RewriteJavaScriptReferences ¶
func (Parser) RewriteStaticURLTextReferences ¶
type ProgressEvent ¶
type ProgressEvent struct {
Token string `json:"token"`
Stage string `json:"stage"`
FoundTotal int `json:"found_total"`
DownloadedTotal int `json:"downloaded_total"`
DownloadTotal int `json:"download_total,omitempty"`
DownloadedBytes int64 `json:"downloaded_bytes,omitempty"`
DownloadTotalBytes int64 `json:"download_total_bytes,omitempty"`
FailedTotal int `json:"failed_total"`
FailedURLs []string `json:"failed_urls,omitempty"`
FailedReasons map[string]string `json:"failed_reasons,omitempty"`
RetryAttempt int `json:"retry_attempt,omitempty"`
RetryTotal int `json:"retry_total,omitempty"`
RetryDelaySeconds int `json:"retry_delay_seconds,omitempty"`
CurrentURL string `json:"current_url"`
CurrentError string `json:"current_error,omitempty"`
CurrentPercent int `json:"current_percent"`
CurrentDownloadedBytes int64 `json:"current_downloaded_bytes"`
CurrentSizeBytes int64 `json:"current_size_bytes"`
CompletedPercent int `json:"completed_percent"`
Message string `json:"message"`
}
ProgressEvent is emitted by page and site import crawlers.
type ProgressTracker ¶
type ProgressTracker struct {
// contains filtered or unexported fields
}
ProgressTracker routes progress events by token using a channel actor.
func NewProgressTracker ¶
func NewProgressTracker() *ProgressTracker
func (*ProgressTracker) Publish ¶
func (tracker *ProgressTracker) Publish(event ProgressEvent)
func (*ProgressTracker) Subscribe ¶
func (tracker *ProgressTracker) Subscribe(token string) chan ProgressEvent
func (*ProgressTracker) Unsubscribe ¶
func (tracker *ProgressTracker) Unsubscribe(token string, stream chan ProgressEvent)
type ReferenceContext ¶
type ReferenceContext int
ReferenceContext tells parser rules where a reference was found. Browsers do not resolve every string literal the same way: HTML attributes, CSS url(...) values, and JavaScript module manifests each have their own conventions. Keeping the context explicit makes new crawler quirks local to this package instead of spreading conditional URL handling through the app.
const ( ReferenceDocument ReferenceContext = iota ReferenceJavaScript )
type ResourcePreview ¶
type ResourcePreview struct {
URL string `json:"url"`
Kind string `json:"kind"`
SizeBytes int64 `json:"size_bytes"`
}
ResourcePreview describes one downloadable remote asset before import confirmation.
type SourceOptions ¶
SourceOptions carry transport-level source overrides shared by previews and imports.
type WholeSitePageJob ¶
WholeSitePageJob is the queue entry used while crawling a whole remote site.