Skip to content

Commit 1f1f305

Browse files
Add support for storing location with content
1 parent 4d961c5 commit 1f1f305

File tree

7 files changed

+66
-26
lines changed

7 files changed

+66
-26
lines changed

files.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func scrapeFiles(c chan<- *ProcessItem) {
4343
continue
4444
}
4545

46-
item := &ProcessItem{Source: "Local", Key: file.Name(), Content: string(content)}
46+
item := &ProcessItem{Source: "Local", Location: path, Key: file.Name(), Content: string(content)}
4747
c <- item
4848

4949
os.Remove(path)

gist.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func scrapeGists(c chan<- *ProcessItem) {
7272
g.Download()
7373

7474
for _, gist := range g.files {
75-
item := &ProcessItem{Source: "Gist", Key: g.Key, Content: gist.Content}
75+
item := &ProcessItem{Source: "Gist", Location: gist.Url, Key: g.Key, Content: gist.Content}
7676
c <- item
7777
}
7878
}

github.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
type PushEventCommit struct {
1010
Key string `json:"sha"`
1111
Url string
12-
Files []string
12+
Files []GithubCommitFile
1313
}
1414

1515
type PushEvent struct {
@@ -26,6 +26,7 @@ type GithubEvent struct {
2626

2727
type GithubCommitFile struct {
2828
Url string `json:"raw_url"`
29+
Content string
2930
}
3031

3132
type GithubCommit struct {
@@ -48,9 +49,11 @@ func (p *PushEvent) Download() {
4849
continue
4950
}
5051

51-
for _, f := range ghc.Files {
52-
file := getGithub(f.Url)
53-
p.Commits[i].Files = append(p.Commits[i].Files, string(file))
52+
for j := range ghc.Files {
53+
f := ghc.Files[j]
54+
55+
f.Content = string(get(f.Url))
56+
p.Commits[i].Files = append(p.Commits[i].Files, f)
5457
}
5558
}
5659

@@ -87,8 +90,10 @@ func scrapeGithubEvents(c chan<- *ProcessItem) {
8790

8891
pe.Download()
8992
for i := range pe.Commits {
90-
for _, f := range pe.Commits[i].Files {
91-
item := &ProcessItem{Source: "GithubCommit", Key: string(pe.Key), Content: f}
93+
for j := range pe.Commits[i].Files {
94+
f := pe.Commits[i].Files[j]
95+
96+
item := &ProcessItem{Source: "GithubCommit", Location: f.Url, Key: string(pe.Key), Content: f.Content}
9297
c <- item
9398
}
9499
}

paste.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func scrapePastes(c chan<- *ProcessItem) {
4646
p := pastes[i]
4747
p.Download()
4848

49-
item := &ProcessItem{Source: "Pastebin", Key: p.Key, Content: p.Content}
49+
item := &ProcessItem{Source: "Pastebin", Location: p.Url, Key: p.Key, Content: p.Content}
5050
c <- item
5151
}
5252
}

process.go

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
package main
22

33
import (
4-
"fmt"
5-
//"log"
4+
"encoding/json"
5+
"log"
66
"strings"
77
)
88

9+
type SaveItem struct {
10+
Location string
11+
Content string
12+
}
13+
14+
func (s *SaveItem) Json() []byte {
15+
data, err := json.Marshal(s)
16+
if err != nil {
17+
log.Printf("[-] Could not create JSON for %s", s.Location)
18+
return nil
19+
}
20+
21+
return data
22+
}
23+
924
type ProcessItem struct {
1025
Source string
1126
Key string
27+
Location string
1228
Content string
1329
Save bool
1430
}
@@ -22,11 +38,13 @@ func (p *ProcessItem) Write() {
2238
return
2339
}
2440

25-
db.Write("pastes", p.Key, []byte(p.Content))
41+
s := SaveItem{Location: p.Location, Content: p.Content}
42+
43+
db.Write("pastes", p.Key, s.Json())
2644
}
2745

2846
func (p *ProcessItem) Regexes() {
29-
for i, _ := range conf.Regexes {
47+
for i := range conf.Regexes {
3048
r := conf.Regexes[i]
3149

3250
switch r.Match {
@@ -35,18 +53,24 @@ func (p *ProcessItem) Regexes() {
3553

3654
if items != nil {
3755
p.Save = true
38-
}
56+
data := strings.Join(items, "\n")
57+
s := SaveItem{Location: p.Location, Content: data}
3958

40-
for k := range items {
41-
rKey := fmt.Sprintf("%s-%d", p.Key, k)
42-
db.Write(r.Bucket, rKey, []byte(items[k]))
59+
db.Write(r.Bucket, p.Key, s.Json())
4360
}
61+
62+
// for k := range items {
63+
// rKey := fmt.Sprintf("%s-%d", p.Source, k)
64+
// db.Write(r.Bucket, rKey, []byte(items[k]))
65+
// }
4466
case "one":
4567
match := r.compiled.FindString(p.Content)
4668

4769
if match != "" {
4870
p.Save = true
49-
db.Write(r.Bucket, p.Key, []byte(match))
71+
s := SaveItem{Location: p.Location, Content: match}
72+
73+
db.Write(r.Bucket, p.Key, s.Json())
5074
}
5175
default:
5276
}
@@ -59,7 +83,7 @@ func (p *ProcessItem) Keywords() {
5983

6084
if strings.Contains(strings.ToLower(p.Content), strings.ToLower(kwd.Keyword)) {
6185
p.Save = true
62-
db.Write(kwd.Bucket, p.Key, nil)
86+
db.Write(kwd.Bucket, p.Location, nil)
6387
}
6488
}
6589
}

web.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"html/template"
56
"log"
67
"net/http"
@@ -14,17 +15,21 @@ import (
1415
type Value struct {
1516
Bucket string
1617
Key string
17-
Value string
18+
Location string
19+
Content string
1820
}
1921

2022
func NewValue(bucket, key string) *Value {
21-
v := new(Value)
23+
var s SaveItem
2224

23-
v.Bucket = bucket
24-
v.Key = key
25-
v.Value = db.Read(bucket, key)
25+
data := db.Read(bucket, key)
26+
err := json.Unmarshal([]byte(data), &s)
27+
if err != nil {
28+
log.Printf("[-] Could not unmarshal %s", key)
29+
return &Value{Bucket: bucket, Key: key}
30+
}
2631

27-
return v
32+
return &Value{Bucket: bucket, Key: key, Location: s.Location, Content: s.Content}
2833
}
2934

3035
type DataSet struct {

web/templates/read.html

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ <h2>{{ .Bucket }} - {{ .Key }}</h2>
55
<input type="text" id="search" name="{{ .Bucket }}" placeholder="Search term." />
66
<input type="submit" value="Search" onclick="search();" />
77
</section>
8-
<pre>{{ .Value }}</pre>
8+
<section>
9+
<h3>Location</h3>
10+
<p><a href="{{ .Location }}">{{ .Location }}</a></p>
11+
12+
<h3>Content</h3>
13+
<pre>{{ .Content }}</pre>
14+
</section>
915

1016
{{ end }}

0 commit comments

Comments
 (0)