-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgist.go
79 lines (64 loc) · 1.57 KB
/
gist.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
package main
import (
"encoding/json"
"log"
"time"
)
type GistFile struct {
Name string `json:"filename"`
Type string `json:"language"`
Url string `json:"raw_url"`
Size int `json:"size"`
Content string `json:"content"`
}
type Gist struct {
Url string
Date string `json:"updated_at"`
Key string `json:"id"`
User string
files []*GistFile
}
func (g *Gist) Download() {
_, exists := conf.keys[g.Key]
if exists {
return
}
var gist map[string]*json.RawMessage
data := getGithub(g.Url)
err := json.Unmarshal(data, &gist)
if err != nil {
log.Printf("[-] Could not parse gist %s: %s\n.", g.Key, err.Error())
return
}
// Decode each file object out of the Files map.
var files map[string]*GistFile
err = json.Unmarshal(*gist["files"], &files)
if err != nil {
log.Printf("[-] Could not parse gist file: %s\n", err.Error())
}
for k := range files {
g.files = append(g.files, files[k])
}
conf.keys[g.Key] = time.Now()
}
func scrapeGists(c chan<- *ProcessItem) {
var gists []*Gist
log.Println("[+] Checking for new gists.")
resp := getGithub("https://api.github.com/gists/public?per_page=100")
err := json.Unmarshal(resp, &gists)
if err != nil {
log.Println("[-] Could not parse list of gists.")
log.Printf("[-] %s.\n", err.Error())
log.Println(string(resp))
return
}
log.Printf("[+] Processing %d gists.\n", len(gists))
for i, _ := range gists {
g := gists[i]
g.Download()
for _, gist := range g.files {
item := &ProcessItem{Source: "Gist", Location: gist.Url, Key: g.Key, Content: gist.Content}
c <- item
}
}
}