Created
June 21, 2023 08:14
-
-
Save y0d3n/5623bb3e4e05bb34619603bea987dee3 to your computer and use it in GitHub Desktop.
main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"net/url" | |
"os" | |
"strings" | |
) | |
func main() { | |
fp, err := os.Open("url.lst") | |
if err != nil { | |
log.Fatalf("os.Open failed: %s", err) | |
} | |
defer fp.Close() | |
flag.Parse() | |
arg := flag.Args()[0] | |
switch arg { | |
case "dup": | |
dupCheck(fp) | |
case "node": | |
parseNodes(fp) | |
} | |
} | |
// --------- | |
func dupCheck(fp *os.File) { | |
scanner := bufio.NewScanner(fp) | |
var s string | |
urls := make(map[string]string) | |
for i := 1; scanner.Scan(); i++ { | |
s = scanner.Text() | |
fmt.Printf("%d %v", i, s) | |
if _, ok := urls[s]; ok { | |
fmt.Print("\x1b[31m\t(dup)\x1b[0m") | |
} | |
urls[s] = s | |
fmt.Println() | |
} | |
if err := scanner.Err(); err != nil { | |
log.Fatalf("scanner.Scan failed: %s", err) | |
} | |
} | |
// --------- | |
func parseNodes(fp *os.File) { | |
var node Node | |
node = Node{ | |
Path: "/", | |
} | |
var host string | |
scanner := bufio.NewScanner(fp) | |
for i := 0; scanner.Scan(); i++ { | |
u, err := url.Parse(strings.Split(scanner.Text(), "\t")[0]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if i == 0 { | |
host = u.Host | |
} | |
if host == u.Host { | |
addPath(&node, u.Path) | |
} | |
} | |
if err := scanner.Err(); err != nil { | |
log.Fatalf("scanner.Scan failed: %s", err) | |
} | |
printMap(node, 0) | |
} | |
type Node struct { | |
Parent *Node | |
Path string | |
Children *[]Node | |
} | |
func addChild(node *Node, parsedPath []string) { | |
if len(parsedPath) > 0 { | |
childIdx := getChildIdx(node, parsedPath[0]) | |
child := &Node{ | |
Parent: node, | |
Path: parsedPath[0], | |
} | |
if childIdx == -1 { | |
// pathがchildrenにない | |
*node.Children = append(*node.Children, *child) | |
childIdx = len(*node.Children) - 1 | |
} else if childIdx == -2 { | |
// children == nil | |
children := make([]Node, 0) | |
node.Children = &children | |
*node.Children = append(*node.Children, *child) | |
childIdx = 0 | |
} | |
if len(parsedPath) > 1 { | |
addChild(&(*node.Children)[childIdx], parsedPath[1:]) | |
} | |
} | |
} | |
func addPath(node *Node, fullPath string) { | |
parsedPath := strings.Split(fullPath, "/") | |
parsedPath = removeSpace(parsedPath) | |
addChild(node, parsedPath) | |
} | |
func getChildIdx(node *Node, path string) int { | |
if (*node).Children != nil { | |
for i, child := range *node.Children { | |
if child.Path == path { | |
return i | |
} | |
} | |
return -1 | |
} | |
return -2 | |
} | |
func removeSpace(parsedPath []string) []string { | |
paths := make([]string, 0) | |
for _, v := range parsedPath { | |
if v == "" { | |
continue | |
} | |
paths = append(paths, v) | |
} | |
return paths | |
} | |
func printMap(node Node, indent int) { | |
for i := 0; i < indent; i++ { | |
fmt.Printf("\t") | |
} | |
fmt.Println(node.Path) | |
if node.Children != nil { | |
indent++ | |
for _, v := range *node.Children { | |
printMap(v, indent) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment