/
main.go
163 lines (157 loc) · 3.75 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
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the AGPL v3. http://www.gnu.org/licenses/agpl-3.0.html
// A command line Sucks-Rules-O-Meter
package main
import (
"flag"
"fmt"
"github.com/darkhelmet/env"
"github.com/jmcvetta/srom/srom"
"github.com/msbranco/goconfig"
"labix.org/v2/mgo"
"log"
"os"
"os/user"
"path/filepath"
)
func setupMongoStorage() *srom.MongoStorage {
//
// Setup MongoDB
//
mongoUrl := env.StringDefault("MONGOLAB_URI", "localhost")
log.Println("Connecting to MongoDB on " + mongoUrl + "...")
session, err := mgo.Dial(mongoUrl)
if err != nil {
log.Panic(err)
}
defer session.Close()
db := session.DB("")
_, err = db.CollectionNames()
if err != nil {
log.Println("Setting db name to 'SROM'.")
db = session.DB("SROM")
}
collection := db.C("terms")
termIdx := mgo.Index{
Key: []string{"Term", "Timestamp"},
Unique: false,
Background: true,
}
log.Println("Ensuring Term, Timestamp index")
err = collection.EnsureIndex(termIdx)
if err != nil {
log.Panic(err)
}
ratioIdx := mgo.Index{
Key: []string{"Ratio", "Timestamp"},
Unique: false,
Background: true,
}
log.Println("Ensuring Ratio, Timestamp index")
err = collection.EnsureIndex(ratioIdx)
if err != nil {
log.Panic(err)
}
mongo := srom.MongoStorage{
Col: collection,
}
return &mongo
}
func getString(c *goconfig.ConfigFile, section, option string) string {
value, err := c.GetString(section, option)
if err != nil {
log.Println(section, ":", option)
log.Fatal(err)
}
if value == "" {
log.Fatalf("No value for option '%v' in section '%v'.", option, section)
}
return value
}
func main() {
log.SetFlags(log.Ltime | log.Lshortfile) // Ought to be controlled with a flag
//
// Config File
//
u, err := user.Current()
if err != nil {
log.Fatal(err)
}
confFolder := filepath.Join(u.HomeDir, ".config", "srom")
confFile := filepath.Join(confFolder, "srom.conf")
_, err = os.Stat(confFile)
if os.IsNotExist(err) {
os.MkdirAll(confFolder, 0700)
c := goconfig.NewConfigFile()
c.AddSection("google")
c.AddOption("google", "custom_search_id", "")
c.AddOption("google", "api_key", "")
c.AddSection("azure")
c.AddOption("azure", "customer_id", "")
c.AddOption("azure", "key", "")
header := "Sucks-Rules-O-Meter Configuration"
err = c.WriteConfigFile(confFile, 0600, header)
if err != nil {
log.Fatal(err)
}
log.Println("Created new config file at", confFile)
log.Println("You will need to populate it with your API credentials")
return
}
engines := []srom.SearchEngine{}
c, err := goconfig.ReadConfigFile(confFile)
if err != nil {
log.Fatal(err)
}
for _, section := range c.GetSections() {
//
// Google
//
if section == "google" {
google := srom.GoogleSearch{
ApiKey: getString(c, "google", "api_key"),
CustomSearchId: getString(c, "google", "custom_search_id"),
}
engines = append(engines, &google)
}
if section == "azure" {
bing := srom.BingSearch{
CustomerId: getString(c, "azure", "customer_id"),
Key: getString(c, "azure", "key"),
}
engines = append(engines, &bing)
}
}
//
// Parse Flags
//
flag.Parse()
if flag.NArg() != 1 {
log.Fatal("Must supply one and only one argument")
}
//
// Run Query
//
sr := srom.New(engines, &srom.NilOutput{})
sr.Run()
term := flag.Arg(0)
ratio, err := sr.Query(term)
if err != nil {
log.Fatal(err)
}
fmt.Printf("\nThe term '%v' has a sucks/rules ratio of %v\n", term, ratio)
msg := fmt.Sprintf("The internet thinks '%v' ", term)
switch {
case ratio > 1:
msg += "SUCKS"
case ratio == 1:
msg += "neither sucks nor rocks. Weird, eh?"
case ratio < 1:
msg += "ROCKS"
}
if ratio > 2 || ratio < 0.5 {
msg += " HARD"
}
msg += ".\n\n"
fmt.Printf(msg)
}