func urlShortenerMain(client *http.Client, argv []string) {
	if len(argv) != 1 {
		fmt.Fprintf(os.Stderr, "Usage: urlshortener http://goo.gl/xxxxx     (to look up details)\n")
		fmt.Fprintf(os.Stderr, "       urlshortener http://example.com/long (to shorten)\n")
		return
	}

	svc, _ := urlshortener.New(client)
	urlstr := argv[0]

	// short -> long
	if strings.HasPrefix(urlstr, "http://goo.gl/") || strings.HasPrefix(urlstr, "https://goo.gl/") {
		url, err := svc.Url.Get(urlstr).Do()
		if err != nil {
			log.Fatalf("URL Get: %v", err)
		}
		fmt.Printf("Lookup of %s: %s\n", urlstr, url.LongUrl)
		return
	}

	// long -> short
	url, err := svc.Url.Insert(&urlshortener.Url{
		Kind:    "urlshortener#url", // Not really needed
		LongUrl: urlstr,
	}).Do()
	if err != nil {
		log.Fatalf("URL Insert: %v", err)
	}
	fmt.Printf("Shortened %s => %s\n", urlstr, url.Id)
}
Exemple #2
0
func long(w http.ResponseWriter, r *http.Request) {
	shortUrl := "http://goo.gl/" + r.FormValue("shortUrl")
	urlshortenerSvc, _ := urlshortener.New(http.DefaultClient)
	url, err := urlshortenerSvc.Url.Get(shortUrl).Do()
	if err != nil {
		fmt.Println("error: %v", err)
		return
	}
	rootHtmlTmpl.Execute(w, fmt.Sprintf("Longer version of %s is : %s", shortUrl, url.LongUrl))
}
Exemple #3
0
func (u *UrlShortenerMod) Init(b *Bot, conn irc.SafeConn) (err error) {
	u.svc, err = urlshortener.New(http.DefaultClient)
	if err != nil {
		return
	}

	b.Hook("short", func(b *Bot, sender, cmd string, args ...string) error {
		if len(args) < 1 {
			return fmt.Errorf("missing argument")
		}
		short, err := u.shorten(args[0])
		if err != nil {
			return err
		}

		b.Conn.Privmsg(sender, short)
		return nil
	})

	log.Printf("urlshortener module initialized")

	return nil
}
Exemple #4
0
func short(w http.ResponseWriter, r *http.Request) {
	longUrl := r.FormValue("longUrl")
	urlshortenerSvc, _ := urlshortener.New(http.DefaultClient)
	url, _ := urlshortenerSvc.Url.Insert(&urlshortener.Url{LongUrl: longUrl}).Do()
	rootHtmlTmpl.Execute(w, fmt.Sprintf("Shortened version of %s is : %s", longUrl, url.Id))
}