-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (80 loc) · 2.9 KB
/
Copy pathmain.go
File metadata and controls
98 lines (80 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"log"
"net/url"
"os"
"sort"
"strconv"
"time"
)
// PageCount represents a URL and its link count
type PageCount struct {
URL string
Count int
}
func main() {
if len(os.Args) < 4 {
fmt.Println("Usage: ./crawler <baseURL> <maxConcurrency> <maxPages>")
os.Exit(1)
}
baseURL, err := url.Parse(os.Args[1])
if err != nil {
log.Fatalf("Invalid base URL %q: %v\n", os.Args[1], err)
}
maxConcurrency, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatalf("Invalid max concurrency: %v\n", err)
}
maxPages, err := strconv.Atoi(os.Args[3])
if err != nil {
log.Fatalf("Invalid max pages: %v\n", err)
}
startTime := time.Now()
// Initialize crawler
crawler := &Crawler{
pageCounts: make(map[string]int),
baseURL: baseURL,
concurrencyLimiter: make(chan struct{}, maxConcurrency),
maxPages: maxPages,
}
// Start crawling
crawler.wg.Add(1)
go crawler.crawlPage(baseURL.String())
crawler.wg.Wait()
// Convert map to slice for sorting
var results []PageCount
for url, count := range crawler.pageCounts {
results = append(results, PageCount{URL: url, Count: count})
}
// Sort by descending link count
sort.Slice(results, func(i, j int) bool {
return results[i].Count > results[j].Count
})
// Compute total links
var totalLinks int
for _, r := range results {
totalLinks += r.Count
}
duration := time.Since(startTime)
printReport(baseURL.String(), results, totalLinks, duration)
}
// printReport outputs crawl statistics in a formatted way
func printReport(baseURL string, results []PageCount, totalLinks int, duration time.Duration) {
fmt.Println("╔══════════════════════════════════════════════════════════════════════╗")
fmt.Println("║ CRAWL REPORT ║")
fmt.Println("╚══════════════════════════════════════════════════════════════════════╝")
fmt.Printf("%-30s : %s\n", "Base URL", baseURL)
fmt.Printf("%-30s : %d\n", "Unique Pages", len(results))
fmt.Printf("%-30s : %d\n", "Total Links", totalLinks)
fmt.Printf("%-30s : %s\n", "Crawl Duration", duration.Round(time.Millisecond))
fmt.Printf("%-30s : %.2f\n", "Pages/Second", float64(len(results))/duration.Seconds())
if len(results) > 0 {
fmt.Printf("%-30s : %s (%d links)\n", "Most Linked Page", results[0].URL, results[0].Count)
fmt.Printf("%-30s : %s (%d links)\n", "Least Linked Page", results[len(results)-1].URL, results[len(results)-1].Count)
}
fmt.Println("\nTop 5 Most Linked Pages:")
for i := 0; i < 5 && i < len(results); i++ {
fmt.Printf(" %-2d. %-50s - %d links\n", i+1, results[i].URL, results[i].Count)
}
}