/
runner.go
73 lines (64 loc) · 1.14 KB
/
runner.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
package main
import (
"fmt"
"os"
"goof/plugs"
"github.com/codegangsta/cli"
"goof/util"
)
func main() {
var (
plug string
debug bool
page int
)
app := cli.NewApp()
app.Name = "goof"
app.Usage = "Extracts blogs from top web magazines"
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "page, p",
Value: 1,
Usage: "Tells how many pages need to be extracted",
Destination: &page,
},
cli.StringFlag{
Name: "plug, u",
Value: "",
Usage: "Tells which plug needs to be invoked",
Destination: &plug,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Turns on the debug mode",
Destination: &debug,
},
}
app.Action = func(c *cli.Context) {
if debug {
os.Setenv("DEBUG", "*")
}
db := util.NewDb()
defer db.Close()
Posts := db.Posts()
switch plug {
case "tech-crunch":
t := plugs.NewTechCrunch()
for p := 1; p <= page; p++ {
posts := t.Next()
for i := range posts {
err := Posts.Insert(&posts[i])
if err != nil {
panic(err)
} else {
fmt.Printf("%s\n", posts[i].Json())
}
}
}
break
default:
panic("invalid plug name")
}
}
app.Run(os.Args)
}