2013-02-17 03:57:54 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type youTubeVideo struct {
|
|
|
|
Entry struct {
|
|
|
|
Info struct {
|
|
|
|
Title struct {
|
|
|
|
Text string `json:"$t"`
|
|
|
|
} `json:"media$title"`
|
|
|
|
Description struct {
|
|
|
|
Text string `json:"$t"`
|
|
|
|
} `json:"media$description"`
|
|
|
|
} `json:"media$group"`
|
|
|
|
Rating struct {
|
|
|
|
Likes string `json:"numLikes"`
|
|
|
|
Dislikes string `json:"numDislikes"`
|
|
|
|
} `json:"yt$rating"`
|
|
|
|
Statistics struct {
|
|
|
|
Views string `json:"viewCount"`
|
|
|
|
} `json:"yt$statistics"`
|
|
|
|
} `json:entry`
|
|
|
|
}
|
|
|
|
|
2013-02-17 07:50:42 +00:00
|
|
|
var UrlRegex string = `(\s|^)(http://|https://)(.*?)(\s|$)`
|
|
|
|
|
2013-02-17 03:57:54 +00:00
|
|
|
func UrlFunc(conn *Conn, line *Line) {
|
|
|
|
text := line.Message()
|
2013-02-17 07:50:42 +00:00
|
|
|
if regex, err := regexp.Compile(UrlRegex); err == nil {
|
2013-02-17 03:57:54 +00:00
|
|
|
url := strings.TrimSpace(regex.FindString(text))
|
|
|
|
if url != "" {
|
|
|
|
if resp, err := http.Get(url); err == nil {
|
|
|
|
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/html") {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if content, err := ioutil.ReadAll(resp.Body); err == nil {
|
|
|
|
if regex, err := regexp.Compile(`<title>(.*?)</title>`); err == nil {
|
|
|
|
if regex.Match([]byte(content)) {
|
|
|
|
conn.Privmsg(line.Target(), strings.TrimSpace(regex.FindStringSubmatch(string(content))[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-17 07:50:42 +00:00
|
|
|
var YouTubeRegex string = `(\s|^)(http://|https://)?(www.)?(youtube.com/watch\?v=|youtu.be/)(.*?)(\s|$|\&|#)`
|
|
|
|
|
2013-02-17 03:57:54 +00:00
|
|
|
func YouTubeFunc(conn *Conn, line *Line) {
|
|
|
|
text := line.Message()
|
2013-02-17 07:50:42 +00:00
|
|
|
if regex, err := regexp.Compile(YouTubeRegex); err == nil {
|
2013-02-17 03:57:54 +00:00
|
|
|
if regex.Match([]byte(text)) {
|
|
|
|
matches := regex.FindStringSubmatch(text)
|
|
|
|
id := matches[len(matches)-2]
|
|
|
|
url := fmt.Sprintf("https://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json", id)
|
|
|
|
if resp, err := http.Get(url); err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if contents, err := ioutil.ReadAll(resp.Body); err == nil {
|
|
|
|
var data youTubeVideo
|
|
|
|
if err := json.Unmarshal(contents, &data); err == nil {
|
|
|
|
conn.Privmsg(line.Target(), fmt.Sprintf("%s - %s views (%s likes, %s dislikes)", data.Entry.Info.Title.Text, data.Entry.Statistics.Views, data.Entry.Rating.Likes, data.Entry.Rating.Dislikes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|