Permalink
Browse files

Add bitly shortener

  • Loading branch information...
k4k committed Sep 15, 2017
1 parent db68366 commit ac6b72a75a11766b35a1076164f74ea9bec2af64
Showing with 22 additions and 3 deletions.
  1. +7 −1 Gopkg.lock
  2. +15 −2 main.go

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
17 main.go
@@ -9,6 +9,7 @@ import (
"strings"
"github.com/thoj/go-ircevent"
"github.com/zpnk/go-bitly"
"golang.org/x/net/html"
)
@@ -20,6 +21,7 @@ const channel = "#myircchannel" // IRC Channel
const nick = "webby-urlbot" // IRC Nick
const username = "webby-urlbot" // IRC Username
const realname = "Webby Urlbot" // IRC Real Name
const token = "<token>" // bitly OAuth Token
// IRC Functions
func ircConnect(srv string, port int, ch string) {
@@ -34,18 +36,29 @@ func ircConnect(srv string, port int, ch string) {
URL, urlstring := isURL(e.Message())
if URL {
title := getTitle(urlstring)
irccon.Privmsg(channel, title)
shortURL := shortenURL(urlstring)
irccon.Privmsg(channel, fmt.Sprintf("%s -- %s", shortURL, title))
}
})
//irccon.AddCallback("336", func(e *irc.Event) {})
err := irccon.Connect(fmt.Sprintf("%s:%d", server, port))
if err != nil {
fmt.Printf("Err %s", err)
fmt.Errorf("Err %s", err)
}
irccon.Loop()
}
// Bitly Functions
func shortenURL(longurl string) string {
b := bitly.New(token)
shortURL, shortErr := b.Links.Shorten(longurl)
if shortErr != nil {
fmt.Errorf("Error: %s", shortErr)
}
return shortURL.URL
}
// HTML Functions
func isTitleElement(n *html.Node) bool {
return n.Type == html.ElementNode && n.Data == "title"

0 comments on commit ac6b72a

Please sign in to comment.