gempir

Untitled

Aug 5th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.17 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "encoding/json"
  6.     "fmt"
  7.     "log"
  8.     "net"
  9.     "net/http"
  10.     "net/textproto"
  11.     "strconv"
  12.     "strings"
  13.     "time"
  14. )
  15.  
  16. // Bot 123
  17. type Bot struct {
  18.     login  string
  19.     host   string
  20.     nick   string
  21.     conn   net.Conn
  22.     parser parse
  23.     epm    map[string]EPMEmote
  24. }
  25.  
  26. var (
  27.     globalTwitchEmotes []int
  28. )
  29.  
  30. // EPMEmote struct for single emoteEPM data
  31. type EPMEmote struct {
  32.     Channel string
  33.     EmoteID string
  34.     EPM     int
  35.     Emotes  int
  36. }
  37.  
  38. // NewBot xD
  39. func NewBot() *Bot {
  40.     return &Bot{
  41.         login:  "spamchamp",
  42.         host:   cfg.IrcHost,
  43.         nick:   "justinfan123321",
  44.         conn:   nil,
  45.         parser: *new(parse),
  46.         epm:    make(map[string]EPMEmote),
  47.     }
  48. }
  49.  
  50. // Connect asd
  51. func (bot *Bot) Connect() (conn net.Conn, err error) {
  52.     log.Println(bot.host)
  53.     conn, err = net.Dial("tcp", bot.host)
  54.     if err != nil {
  55.         Log.Fatal("unable to connect to IRC server ", err)
  56.     }
  57.     bot.conn = conn
  58.     log.Printf("Connected to IRC server %s (%s)\n", bot.host, bot.conn.RemoteAddr())
  59.     return bot.conn, nil
  60. }
  61.  
  62. func (bot *Bot) startBot() {
  63.     conn, _ := bot.Connect()
  64.  
  65.     fmt.Fprintf(conn, "LOGIN %s\r\n", bot.login)
  66.     fmt.Fprintf(conn, "NICK %s\r\n", bot.nick)
  67.     bot.joinChannel("#gempir")
  68.  
  69.     defer conn.Close()
  70.  
  71.     //go bot.joinTopChannels()
  72.  
  73.     go func() {
  74.         t := time.NewTicker(time.Second)
  75.         for {
  76.             bot.calculateEPM()
  77.             bot.syncEPM()
  78.             <-t.C
  79.         }
  80.     }()
  81.  
  82.     reader := bufio.NewReader(conn)
  83.     tp := textproto.NewReader(reader)
  84.     for {
  85.         line, err := tp.ReadLine()
  86.         if err != nil {
  87.             log.Println(err)
  88.             break // break loop on errors
  89.         }
  90.         bot.handleMessage(line)
  91.     }
  92. }
  93.  
  94. func (bot *Bot) handleMessage(message string) {
  95.     pmsg := bot.parser.Parse(message)
  96.     if len(pmsg.Emotes) == 0 ||
  97.         strings.Contains(pmsg.Emotes[0].Name, ")") ||
  98.         strings.Contains(pmsg.Emotes[0].Name, "(") ||
  99.         strings.Contains(pmsg.Emotes[0].Name, "<") ||
  100.         strings.Contains(pmsg.Emotes[0].Name, ">") ||
  101.         strings.Contains(pmsg.Emotes[0].Name, "/") ||
  102.         strings.Contains(pmsg.Emotes[0].Name, "\\") ||
  103.         strings.Contains(pmsg.Emotes[0].Name, ";") ||
  104.         strings.Contains(pmsg.Emotes[0].Name, ":") {
  105.         return
  106.     }
  107.     id, err := strconv.Atoi(pmsg.Emotes[0].ID)
  108.     if err != nil {
  109.         log.Println(err)
  110.     }
  111.     if !stringInSlice(id, globalTwitchEmotes) {
  112.         return
  113.     }
  114.     e, exists := bot.epm[pmsg.Emotes[0].Name]
  115.     if exists {
  116.         e.Emotes++
  117.         bot.epm[pmsg.Emotes[0].Name] = e
  118.     } else {
  119.         bot.epm[pmsg.Emotes[0].Name] = EPMEmote{
  120.             Channel: pmsg.Channel,
  121.             EmoteID: pmsg.Emotes[0].ID,
  122.             EPM:     0,
  123.             Emotes:  1,
  124.         }
  125.     }
  126.     e, _ = bot.epm[pmsg.Emotes[0].Name]
  127.     bot.updateRedisScore(pmsg.Channel, pmsg.Emotes[0].Name, pmsg.Emotes[0].ID, e.EPM)
  128. }
  129.  
  130. func (bot *Bot) calculateEPM() {
  131.     for _, emote := range bot.epm {
  132.         // log.Println(emote.EmoteID)
  133.         // log.Println("inert", emote.EPM)
  134.         // emote.EPM = (emote.EPM + (emote.Emotes * 60)) / 2
  135.         // emote.Emotes = 0
  136.         // log.Println(emote.EPM)
  137.         log.Println(emote.EPM)
  138.         emote.EPM = 123
  139.         log.Println(emote.EPM)
  140.     }
  141. }
  142.  
  143. func (bot *Bot) joinChannel(channel string) {
  144.     log.Println("joining", channel)
  145.     fmt.Fprintf(bot.conn, "JOIN %s\r\n", channel)
  146. }
  147.  
  148. // TopChannels top channels on twitch
  149. type TopChannels struct {
  150.     Streams []Stream `json:"streams"`
  151. }
  152.  
  153. // Stream single stream object
  154. type Stream struct {
  155.     Channel struct {
  156.         Name string `json:"name"`
  157.     } `json:"channel"`
  158. }
  159.  
  160. func (bot *Bot) joinTopChannels() {
  161.     res, err := httpRequest("https://api.twitch.tv/kraken/streams?limit=100&offset=0")
  162.     if err != nil {
  163.         log.Panic(err)
  164.     }
  165.     var topChannels TopChannels
  166.     json.Unmarshal(res, &topChannels)
  167.     log.Println(topChannels.Streams[0].Channel.Name)
  168.     for _, channel := range topChannels.Streams {
  169.         go bot.joinChannel("#" + channel.Channel.Name)
  170.     }
  171. }
  172.  
  173. func (bot *Bot) syncEPM() {
  174.     //log.Println("syncing EPM")
  175.     for index, emote := range bot.epm {
  176.         if emote.EPM < 3 {
  177.             return
  178.         }
  179.         data := new(emoteData)
  180.         data.Channel = emote.Channel
  181.         data.Epm = emote.EPM
  182.         data.Emote.Bttv = false
  183.         data.Emote.EmoteCode = index
  184.         var err error
  185.         data.Emote.EmoteID, err = strconv.Atoi(emote.EmoteID)
  186.         if err != nil {
  187.             log.Println(err)
  188.             continue
  189.         }
  190.         emitEmote(data)
  191.     }
  192. }
  193.  
  194. func (bot *Bot) updateRedisScore(channel string, emote string, emoteID string, score int) {
  195.     res, err := rclient.HGet("spamchamp:records", emoteID).Result()
  196.     if err != nil {
  197.         rclient.HSet("spamchamp:records", emoteID, emote+"[:]"+channel+"[:]"+strconv.Itoa(score))
  198.     } else {
  199.         rscore, err := strconv.Atoi(strings.Split(res, "[:]")[2])
  200.         if err != nil {
  201.             log.Println(err)
  202.             return
  203.         }
  204.         if score > rscore {
  205.             rclient.HSet("spamchamp:records", emoteID, emote+"[:]"+channel+"[:]"+strconv.Itoa(score))
  206.         }
  207.     }
  208. }
  209.  
  210. func serveRecords(w http.ResponseWriter, r *http.Request) {
  211.     var records []emoteData
  212.     res, err := rclient.HGetAll("spamchamp:records").Result()
  213.     if err != nil {
  214.         log.Println(err)
  215.         return
  216.     }
  217.     for index, element := range res {
  218.         split := strings.Split(element, "[:]")
  219.         data := new(emoteData)
  220.         data.Channel = split[1]
  221.         data.Epm, err = strconv.Atoi(split[2])
  222.         if err != nil {
  223.             log.Println(err)
  224.             continue
  225.         }
  226.         data.Emote.Bttv = false
  227.         data.Emote.EmoteCode = split[0]
  228.         var err error
  229.         data.Emote.EmoteID, err = strconv.Atoi(index)
  230.         if err != nil {
  231.             log.Println(err)
  232.             continue
  233.         }
  234.         records = append(records, *data)
  235.     }
  236.     json.NewEncoder(w).Encode(records)
  237. }
  238.  
  239. // GlobalTwitchEmotes struct for twitchemotes data
  240. type GlobalTwitchEmotes struct {
  241.     Emotes map[string]TwitchEmotesEmote `json:"emotes"`
  242. }
  243.  
  244. // TwitchEmotesEmote single emote from twitch emotes
  245. type TwitchEmotesEmote struct {
  246.     ImageID int `json:"image_id"`
  247. }
  248.  
  249. func loadTwitchEmotes() {
  250.     res, err := httpRequest("https://twitchemotes.com/api_cache/v2/global.json")
  251.     if err != nil {
  252.         log.Panic(err)
  253.     }
  254.     var globalEmotes GlobalTwitchEmotes
  255.     json.Unmarshal(res, &globalEmotes)
  256.     for _, emoteID := range globalEmotes.Emotes {
  257.         globalTwitchEmotes = append(globalTwitchEmotes, emoteID.ImageID)
  258.     }
  259. }
  260.  
  261. func stringInSlice(a int, list []int) bool {
  262.     for _, b := range list {
  263.         if b == a {
  264.             return true
  265.         }
  266.     }
  267.     return false
  268. }
  269.  
  270. func round(v float64, decimals int) float64 {
  271.     var pow float64 = 1
  272.     for i := 0; i < decimals; i++ {
  273.         pow *= 10
  274.     }
  275.     return float64(int((v*pow)+0.5)) / pow
  276. }
Advertisement
Add Comment
Please, Sign In to add comment