Example #1
0
// DispatchJSON receives an extension json request
func DispatchJSON(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	// get namespace
	namespace := mux.Vars(r)["namespace"]
	if namespace == "" {
		startpage.Dispatch(w, r)
		return
	}

	shareURL := r.URL.Query().Get("url")

	if !govalidator.IsRequestURL(shareURL) {
		c.Errorf("Error at unmarshalling for share/json. Namespace: %v. Error: %v", namespace, shareURL)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	i := extract.ItemFromURL(shareURL, r)
	i.Namespace = namespace

	c.Infof("Item: %v", i)

	if err := data.StoreItem(c, i); err != nil {
		c.Errorf("Error at in StoreItem. Item: %v. Error: %v", i, err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	w.Write(statusOk)
}
Example #2
0
// DispatchEmail parses incoming emails
func DispatchEmail(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	defer r.Body.Close()

	msg, err := mail.ReadMessage(r.Body)
	if err != nil {
		c.Errorf("Error at mail.ReadMessage in DispatchEmail. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	c.Infof("header: %v", msg.Header)

	// get namespaces
	namespaces, err := getNamespaces(msg)
	if err != nil {
		c.Errorf("Error at parsing the receiver fields. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	c.Infof("Detected namespaces: %v", namespaces)

	// get body
	body, err := extractBody(c, msg.Header, msg.Body)
	if err != nil {
		c.Errorf("Error at parsing the body. Error: %v", err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	c.Infof("Received mail: %v", body)

	urls, err := parseBody(c, body)
	c.Infof("Found urls: %v", urls)

	for _, shareURL := range urls {
		if !govalidator.IsRequestURL(shareURL) {
			c.Errorf("Invalid URL. Error: %v", shareURL)
			continue
		}

		i := extract.ItemFromURL(shareURL, r)

		for _, namespace := range namespaces {
			i.Namespace = namespace
			c.Infof("Item: %v", i)

			if err := data.StoreItem(c, i); err != nil {
				c.Errorf("Error at in StoreItem. Item: %v. Error: %v", i, err)
				continue
			}
		}
	}

}
Example #3
0
func main() {
	webaddr := flag.String("url", "", "The URL to be parsed.")
	flag.Parse()

	// Check if URL was passed at command line
	if *webaddr == "" {
		flag.PrintDefaults()
		return
	}

	// Is it a valid URL?
	if !govalidator.IsRequestURL(*webaddr) {
		fmt.Println("Invalid URL")
		return
	}

	fmt.Print(extract.ItemFromURL(*webaddr, nil))
}