summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cmd/indus/main.go47
-rw-r--r--fetch/request.go41
-rw-r--r--fetch/wikipedia.go39
-rw-r--r--summarize/wikipedia.go27
4 files changed, 153 insertions, 1 deletions
diff --git a/cmd/indus/main.go b/cmd/indus/main.go
index 38dd16d..ce3aa91 100644
--- a/cmd/indus/main.go
+++ b/cmd/indus/main.go
@@ -1,3 +1,48 @@
 package main
 
-func main() {}
+import (
+	"fmt"
+	"log"
+
+	"framagit.org/andinus/indus/clipboard"
+	"framagit.org/andinus/indus/fetch"
+	"framagit.org/andinus/indus/summarize"
+)
+
+func main() {
+	// Get the primary clipboard selection.
+	sel, err := clipboard.GetSel()
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	// If the selection is an empty string then the program should
+	// exit because otherwise Notify function will fail with
+	// non-zero exit code.
+	if len(sel) == 0 {
+		err = fmt.Errorf("clipboard: primary clipboard empty")
+		log.Fatal(err)
+	}
+
+	notifyWiki(sel)
+}
+
+func notifyWiki(sel string) {
+	// Fetch the summary from wikipedia.
+	w, err := fetch.Wikipedia(sel)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	// Get notification information.
+	n, err := summarize.Wikipedia(w)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	// Send a desktop notification to the user.
+	err = n.Notify()
+	if err != nil {
+		log.Fatal(err)
+	}
+}
diff --git a/fetch/request.go b/fetch/request.go
new file mode 100644
index 0000000..a3c6f23
--- /dev/null
+++ b/fetch/request.go
@@ -0,0 +1,41 @@
+package fetch
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"time"
+)
+
+func getRes(api string) ([]byte, error) {
+	var body []byte
+
+	c := http.Client{
+		// TODO: timeout should be configurable by the user
+		Timeout: time.Second * 64,
+	}
+
+	req, err := http.NewRequest(http.MethodGet, api, nil)
+	if err != nil {
+		return body, err
+	}
+
+	req.Header.Set("User-Agent", "Andinus / Indus - framagit.org/andinus/indus")
+
+	res, err := c.Do(req)
+	if err != nil {
+		return body, err
+	}
+	defer res.Body.Close()
+
+	if res.StatusCode != 200 {
+		return body, fmt.Errorf("Unexpected response status code received: %d %s",
+			res.StatusCode, http.StatusText(res.StatusCode))
+	}
+
+	body, err = ioutil.ReadAll(res.Body)
+	if err != nil {
+		return body, err
+	}
+	return body, err
+}
diff --git a/fetch/wikipedia.go b/fetch/wikipedia.go
new file mode 100644
index 0000000..ee057d4
--- /dev/null
+++ b/fetch/wikipedia.go
@@ -0,0 +1,39 @@
+package fetch
+
+import (
+	"encoding/json"
+	"fmt"
+)
+
+// Wiki struct holds information retrieved from wikipedia api.
+type Wiki struct {
+	Type         string `json:"type"`
+	Title        string `json:"title"`
+	DisplayTitle string `json:"displaytitle"`
+	PageID       int    `json:"pageid"`
+	Description  string `json:"description"`
+	Language     string `json:"lang"`
+	Extract      string `json:"extract"`
+}
+
+// Wikipedia gets the summary from wikipedia api & returns it with an error (if
+// exists). It takes a string as input which will be the search query
+// sent to wikipedia.
+func Wikipedia(q string) (Wiki, error) {
+	w := Wiki{}
+	api := fmt.Sprintf("https://en.wikipedia.org/api/rest_v1/page/summary/%s", q)
+
+	body, err := getRes(api)
+	if err != nil {
+		return w, err
+	}
+
+	// Unmarshal json to w
+	err = json.Unmarshal([]byte(body), &w)
+	if err != nil {
+		err = fmt.Errorf("Unmarshalling Json failed\n%s", err.Error())
+		return w, err
+	}
+
+	return w, err
+}
diff --git a/summarize/wikipedia.go b/summarize/wikipedia.go
new file mode 100644
index 0000000..c4a5050
--- /dev/null
+++ b/summarize/wikipedia.go
@@ -0,0 +1,27 @@
+package summarize
+
+import (
+	"fmt"
+
+	"framagit.org/andinus/indus/fetch"
+	"framagit.org/andinus/indus/notification"
+)
+
+// Wikipedia returns struct notification.Notif with notification info
+// from the wikipedia summary.
+func Wikipedia(w fetch.Wiki) (notification.Notif, error) {
+	n := notification.Notif{}
+	var err error
+
+	// Continue only if the page's type is standard. TODO: Work
+	// with other page types to get summary.
+	switch w.Type {
+	case "standard":
+		n.Title = fmt.Sprintf("%s - Wikipedia", w.Title)
+		n.Message = w.Description
+	default:
+		err = fmt.Errorf("Summarizing wikipedia response failed")
+	}
+
+	return n, err
+}