-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (145 loc) · 3.56 KB
/
main.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*Program to remove junk data in mp3 files*/
package main
import (
"bufio"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/gorilla/websocket"
"github.com/skratchdot/open-golang/open"
)
//ResponseMsg is the format in which server sends websocket to clients
type ResponseMsg struct {
Count int
Context string
}
var (
musicList []string
junkList []string
upgrader = websocket.Upgrader{}
)
//LinesFromFile reads string from path and return array of string line by line
func LinesFromFile(path string) ([]string, error) {
var arr []string
file, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0666)
if err != nil {
return arr, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
arr = append(arr, strings.TrimSpace(scanner.Text()))
}
if scanner.Err() != nil {
return arr, scanner.Err()
}
return arr, nil
}
//BrowseXFiles reads all x types of files from root path & return filepaths in an array of string
func BrowseXFiles(x string, root string) ([]string, error) {
var arr []string
err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
if strings.HasSuffix(f.Name(), x) { //.mp3
arr = append(arr, path)
}
}
return nil
})
if err != nil {
return arr, err
}
return arr, nil
}
func main() {
//Load local junk list
l, err := LinesFromFile("./junk.txt")
if err != nil {
log.Fatal("Error loading in junklist", err)
}
junkList = l
// musicList = []string{}
musicList, err = BrowseXFiles(".mp3", ".")
if err != nil {
log.Fatal("Error in walking over files", err)
}
http.HandleFunc("/", home)
http.HandleFunc("/ws", compute)
log.Println("Running on http://localhost:7899/")
open.Run("http://localhost:7899/")
err = http.ListenAndServe(":7899", nil)
if err != nil {
log.Fatal("listenAndServe", err)
}
}
func home(rw http.ResponseWriter, req *http.Request) {
// fmt.Println("Client connected", req.RemoteAddr)
var v = struct {
Host string
Count int
}{
req.Host,
len(musicList),
}
t := template.Must(template.ParseFiles("./ui.tmpl"))
t.Execute(rw, &v)
}
func compute(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade websocket error:", err)
return
}
// defer c.Close()
//Regex for websites junk (possible junk)
webRex := "(www.|)[a-zA-Z0-9_\\-]+\\.[a-zA-Z]{2,4}"
rx, _ := regexp.Compile(webRex)
var iter = 0
var cleanedFi string
//Scan music list
for _, fi := range musicList {
iter = iter + 1
//Only base names, mp3 extension exclude
cleanedFi = filepath.Base(strings.TrimSuffix(fi, ".mp3"))
//Possible junk with websites name, other exclude
if rx.MatchString(cleanedFi) {
//junk List Match
junk := stringInSlice(cleanedFi, junkList)
if junk == "" { //junk not found
c.WriteJSON(&ResponseMsg{iter, cleanedFi})
var v = struct{ junk string }{}
c.ReadJSON(&v)
junk = v.junk
//New junk from user added to local junk list
junkList = append(junkList, junk)
appendTojunkDB(junk)
}
// os.Rename(fi, strings.Replace(fi, junk, "", 1))
}
}
c.WriteJSON(&ResponseMsg{iter, cleanedFi})
c.Close()
}
func appendTojunkDB(sp string) {
if sp != "" {
file, _ := os.OpenFile("junk.txt", os.O_RDWR|os.O_APPEND, 0666)
defer file.Close()
b := make([]byte, 1000) //this can be efficient
file.Read(b)
if !strings.Contains(string(b), sp) {
file.WriteString(sp + "\n")
}
}
}
func stringInSlice(a string, b []string) string {
for _, el := range b {
if strings.Contains(a, el) {
return el
}
}
return ""
}