func youtube(command *bot.Cmd, matches []string) (msg string, err error) { search := &youtubeSearch{} err = web.GetJSON(fmt.Sprintf(youtubeSearchURL, url.QueryEscape(matches[1]), bot.Config.Youtube), search) if err != nil { return fmt.Sprintf("YouTube | Could not find video for: %s", matches[1]), nil } if search.Pageinfo.Totalresults == 0 { return fmt.Sprintf("YouTube | Could not find video for: %s", matches[1]), nil } id := search.Items[0].ID.Videoid video := &youtubeVideo{} err = web.GetJSON(fmt.Sprintf(youtubeVideoURL, id, bot.Config.Youtube), video) if err != nil { return fmt.Sprintf("YouTube | Could not find video for: %s", matches[1]), nil } reg := regexp.MustCompile("\\s+") title := video.Items[0].Snippet.Title title = reg.ReplaceAllString(title, " ") // Strip excessive spaces duration := ParseDuration(video.Items[0].Contentdetails.Duration) output := fmt.Sprintf("YouTube | %s | %s | https://youtu.be/%s", title, duration, id) return output, nil }
func whosPlaying(command *bot.Cmd, matches []string) (msg string, err error) { users := bot.GetNames(strings.ToLower(command.Channel)) var playing []string for _, user := range users { if bot.GetUserKey(user, "lastfm") != "" { playing = append(playing, user) } } for _, user := range playing { username := checkLastfm(user, user) data := &NowPlaying{} err = web.GetJSON(fmt.Sprintf(NowPlayingURL, username, bot.Config.Lastfm), data) if err != nil || data.Error > 0 { continue } if data.Recenttracks.Attr.Total == "0" { continue } if data.Recenttracks.Track[0].Attr.Nowplaying != "true" { continue } var fmttags string tags := &ArtistTags{} err = web.GetJSON(fmt.Sprintf(ArtistTagsURL, url.QueryEscape(data.Recenttracks.Track[0].Artist.Text), bot.Config.Lastfm), tags) if err != nil { continue } var trunc int = 4 if len(tags.Toptags.Tag) < trunc { trunc = len(tags.Toptags.Tag) } for i := range tags.Toptags.Tag[:trunc] { fmttags += fmt.Sprintf("%s, ", tags.Toptags.Tag[i].Name) } fmttags = strings.TrimSuffix(fmttags, ", ") bot.Conn.Privmsg(command.Channel, fmt.Sprintf("%s (%s) | “%s” by %s | %s", user, username, data.Recenttracks.Track[0].Name, data.Recenttracks.Track[0].Artist.Text, fmttags)) time.Sleep(500 * time.Millisecond) } return "", nil }
func nowPlaying(command *bot.Cmd, matches []string) (msg string, err error) { username := checkLastfm(command.Nick, matches[1]) if username == "" { return "Lastfm not provided, nor on file. Use `-set lastfm <lastfm>` to save", nil } data := &NowPlaying{} err = web.GetJSON(fmt.Sprintf(NowPlayingURL, username, bot.Config.Lastfm), data) if err != nil || data.Error > 0 { return fmt.Sprintf("Could not get scrobbles for %s", username), nil } if data.Recenttracks.Attr.Total == "0" { return fmt.Sprintf("Could not get scrobbles for %s", username), nil } album := "" if data.Recenttracks.Track[0].Album.Text != "" { album = fmt.Sprintf(" from %s", data.Recenttracks.Track[0].Album.Text) } var fmttags string tags := &ArtistTags{} err = web.GetJSON(fmt.Sprintf(ArtistTagsURL, url.QueryEscape(data.Recenttracks.Track[0].Artist.Text), bot.Config.Lastfm), tags) if err != nil { return fmt.Sprintf("Could not get scrobbles for %s", username), nil } var trunc int = 4 if len(tags.Toptags.Tag) < trunc { trunc = len(tags.Toptags.Tag) } for i := range tags.Toptags.Tag[:trunc] { fmttags += fmt.Sprintf("%s, ", tags.Toptags.Tag[i].Name) } fmttags = strings.TrimSuffix(fmttags, ", ") state := "last played" if data.Recenttracks.Track[0].Attr.Nowplaying == "true" { state = "is playing" } output := fmt.Sprintf("Last.fm | %s %s: “%s” by %s%s | %s", username, state, data.Recenttracks.Track[0].Name, data.Recenttracks.Track[0].Artist.Text, album, fmttags) return output, nil }
func urban(command *bot.Cmd, matches []string) (msg string, err error) { var i int64 = 0 if matches[1] != "" { i, _ = strconv.ParseInt(matches[1], 10, 64) i = i - 1 } results := &DefinitionResults{} err = web.GetJSON(fmt.Sprintf(urbanURL, url.QueryEscape(matches[2])), results) if err != nil { return fmt.Sprintf("Urban Dictionary | %s | (No definition found)", matches[2]), nil } if results.ResultType == "no_results" { return fmt.Sprintf("Urban Dictionary | %s | (No definition found)", matches[2]), nil } if int(i+1) > len(results.List) { return fmt.Sprintf("Urban Dictionary | %s | (No definition found)", matches[2]), nil } word := results.List[i].Word definition := results.List[i].Definition permalink := results.List[i].Permalink short := web.ShortenURL(permalink) reg := regexp.MustCompile("\\s+") definition = reg.ReplaceAllString(definition, " ") // Strip tabs and newlines if len(definition) > 180 { definition = fmt.Sprintf("%s...", definition[0:180]) } output := fmt.Sprintf("Urban Dictionary | %s | %s | %s", word, short, definition) return output, nil }
func charts(command *bot.Cmd, matches []string) (msg string, err error) { username := checkLastfm(command.Nick, matches[1]) if username == "" { return "Lastfm not provided, nor on file. Use `-set lastfm <lastfm>` to save", nil } data := &WeeklyCharts{} err = web.GetJSON(fmt.Sprintf(ChartsURL, username, bot.Config.Lastfm), data) if err != nil || data.Error > 0 { return fmt.Sprintf("Could not get charts for %s", username), nil } if data.Topartists.Attr.Total == "0" { return fmt.Sprintf("Could not get charts for %s", username), nil } var fmtcharts string var trunc int = 5 if len(data.Topartists.Artist) < trunc { trunc = len(data.Topartists.Artist) } for i := range data.Topartists.Artist[:trunc] { fmtcharts += fmt.Sprintf("%s (%s), ", data.Topartists.Artist[i].Name, data.Topartists.Artist[i].Playcount) } fmtcharts = strings.TrimSuffix(fmtcharts, ", ") output := fmt.Sprintf("Last.fm | Top 5 Weekly artists for %s | %s", username, fmtcharts) return output, nil }
func getCoords(location string) string { var err error geo := &Geocode{} err = web.GetJSON(fmt.Sprintf(GeocodeURL, url.QueryEscape(location), bot.Config.Geocode), geo) if err != nil || geo.Status != "OK" { return "" } return fmt.Sprintf("%v,%v", geo.Results[0].Geometry.Location.Lat, geo.Results[0].Geometry.Location.Lng) }
func weather(command *bot.Cmd, matches []string) (msg string, err error) { var location string = matches[1] var coords string if location == "" { location, coords = checkLocation(command.Nick) if coords == "" { return "Location not provided, nor on file. Use `-set location <location>` to save", nil } } else { coords = getCoords(location) if coords == "" { return fmt.Sprintf("Could not find %s", location), nil } } data := &Forecast{} err = web.GetJSON(fmt.Sprintf(DarkSkyURL, bot.Config.Weather, coords), data) if err != nil { return fmt.Sprintf("Could not get weather for: %s", location), nil } units := "°C" windspeed := "m/s" if data.Flags.Units == "us" { units = "°F" windspeed = "mph" } else if data.Flags.Units == "ca" { windspeed = "km/h" } else if data.Flags.Units == "uk2" { windspeed = "mph" } return fmt.Sprintf("Weather | %s | Now: %s %s %v%s. Today: %s %v%s/%v%s. Humidity: %v%%. Wind: %v%s. Precipitation: %v%%.", location, data.Currently.Summary, Emoji(data.Currently.Icon), Round(data.Currently.Temperature), units, Emoji(data.Daily.Data[0].Icon), Round(data.Daily.Data[0].TemperatureMax), units, Round(data.Daily.Data[0].TemperatureMin), units, int(data.Daily.Data[0].Humidity*100), data.Daily.Data[0].WindSpeed, windspeed, int(data.Daily.Data[0].PrecipProbability*100)), nil }
func gif(command *bot.Cmd, matches []string) (msg string, err error) { data := &giphy{} err = web.GetJSON(fmt.Sprintf(giphyURL, url.QueryEscape(matches[1]), bot.Config.Giphy), data) if err != nil { return "", err } if len(data.Data) == 0 { return "No gifs found.", nil } index := rand.Intn(len(data.Data)) return fmt.Sprintf(data.Data[index].Images.FixedHeight.Url), nil }
func forecast(command *bot.Cmd, matches []string) (msg string, err error) { var location string = matches[1] var coords string if location == "" { location, coords = checkLocation(command.Nick) if coords == "" { return "Location not provided, nor on file. Use `-set location <location>` to save", nil } } else { coords = getCoords(location) if coords == "" { return fmt.Sprintf("Could not find %s", location), nil } } data := &Forecast{} err = web.GetJSON(fmt.Sprintf(DarkSkyURL, bot.Config.Weather, coords), data) if err != nil { return fmt.Sprintf("Could not get weather for: %s", location), nil } units := "°C" if data.Flags.Units == "us" { units = "°F" } output := fmt.Sprintf("Forecast | %s ", location) for i := range data.Daily.Data[0:4] { tm := time.Unix(data.Daily.Data[i].Time, 0) loc, _ := time.LoadLocation(data.Timezone) day := tm.In(loc).Weekday() output += fmt.Sprintf("| %s: %s %v%s/%v%s ", day, Emoji(data.Daily.Data[i].Icon), Round(data.Daily.Data[i].TemperatureMax), units, Round(data.Daily.Data[i].TemperatureMin), units, ) } return output, nil }
func setLocation(command *bot.Cmd, matches []string) (msg string, err error) { geo := &Geocode{} err = web.GetJSON(fmt.Sprintf(GeocodeURL, url.QueryEscape(strings.TrimSpace(matches[1])), bot.Config.Geocode), geo) if err != nil { return fmt.Sprintf("Could not find %s", strings.TrimSpace(matches[1])), nil } if geo.Status != "OK" { return fmt.Sprintf("Could not find %s", strings.TrimSpace(matches[1])), nil } coords := fmt.Sprintf("%v,%v", geo.Results[0].Geometry.Location.Lat, geo.Results[0].Geometry.Location.Lng) location := geo.Results[0].AddressComponents[0].ShortName if len(geo.Results[0].AddressComponents) > 1 { if geo.Results[0].AddressComponents[1].Types[0] == "locality" { location = geo.Results[0].AddressComponents[1].ShortName } } bot.SetUserKey(command.Nick, "location", location) bot.SetUserKey(command.Nick, "coords", coords) return fmt.Sprintf("%s: Location updated to: %s", command.Nick, location), nil }