2016-01-20 06:40:13 +00:00
|
|
|
// vi:ts=4:sts=4:sw=4:noet:tw=72
|
2016-01-10 17:45:21 +00:00
|
|
|
|
2016-02-15 23:17:42 +00:00
|
|
|
package modules
|
2016-01-10 17:45:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-01-21 21:10:04 +00:00
|
|
|
"html"
|
2016-01-10 17:45:21 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-03-12 12:09:25 +00:00
|
|
|
"code.dnix.de/an/xlog"
|
|
|
|
|
2016-01-10 17:45:21 +00:00
|
|
|
"flokatirc/util"
|
|
|
|
|
|
|
|
"github.com/sorcix/irc"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
API_URL = "https://robertsspaceindustries.com/api"
|
|
|
|
API_METHOD = "/stats/getCrowdfundStats"
|
|
|
|
QUERY_CIT_URL = "https://robertsspaceindustries.com/citizens/"
|
|
|
|
QUERY_ORG_URL = "https://robertsspaceindustries.com/orgs/"
|
|
|
|
RE_CIT_NAME = `<div class="info">\s*<p class="entry">\s*<strong class="value">(.*)</strong>\s*</p>\s*<p class="entry">`
|
|
|
|
RE_CIT_RECORD = `<p class="entry citizen-record">\s*<span class="label">UEE Citizen Record</span>\s*<strong class="value">#(.*)</strong>`
|
|
|
|
RE_CIT_HANDLE = `<p class="entry">\s*<span class="label">Handle name</span>\s*<strong class="value">(.*)</strong>\s*</p>`
|
|
|
|
RE_CIT_ORG = `<a href=.*orgs.*class=.*style=.*background-position.*px center.*>(.*)</a>`
|
|
|
|
RE_CIT_SID = `Spectrum Identification \(SID\)</span>\s*<strong class="value data.*">([A-Z0-9]*)</strong>`
|
|
|
|
RE_CIT_RANK = `Organization rank</span>\s*<strong class="value data.*">(.*)</strong>`
|
|
|
|
RE_ORG_NAME = `<h1>(.*) ?/ ?<span class="symbol">`
|
|
|
|
RE_ORG_COUNT = `<span class="count">(.*) member`
|
|
|
|
RE_ORG_MODEL = `<li class="model">(.*)</li>`
|
|
|
|
RE_ORG_FOCUS = `<ul class="focus clearfix">\s*<li class="primary tooltip-wrap">\s*<img src=".*" alt=".*" />\s*<div class="rsi-tooltip">\s*<div class="content">(.*)</div>\s*<span class="bottom"></span>\s*</div>\s*</li>\s*<li class="secondary tooltip-wrap">\s*<img src=".*" alt=".*" />\s*<div class="rsi-tooltip">\s*<div class="content">(.*)</div>\s*<span class="bottom"></span>\s*</div>\s*</li>\s*</ul>`
|
|
|
|
RE_ORG_COMM = `<li class="commitment">(.*)</li>`
|
|
|
|
FANS_INT = 1000
|
|
|
|
FLEET_INT = 1000
|
2017-01-21 16:04:24 +00:00
|
|
|
FUNDS_INT = 50000
|
2016-01-10 17:45:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
fans = 0
|
|
|
|
fleet = 0
|
|
|
|
funds = 0
|
|
|
|
)
|
|
|
|
|
2016-02-15 23:17:42 +00:00
|
|
|
func init() {
|
2016-03-19 22:49:41 +00:00
|
|
|
MsgFuncs["sc"] = scHandleMessage
|
|
|
|
RunFuncs["sc"] = scScrapeLoop
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 23:17:42 +00:00
|
|
|
func scHandleMessage(m *irc.Message) {
|
2016-01-10 17:45:21 +00:00
|
|
|
tok := strings.Split(m.Trailing, " ")
|
|
|
|
if len(tok) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch tok[0] {
|
|
|
|
case "!scstats":
|
|
|
|
showScStats()
|
2016-01-11 06:18:52 +00:00
|
|
|
case "!sccit":
|
2016-01-10 17:45:21 +00:00
|
|
|
if len(tok) > 1 {
|
|
|
|
showCitizen(tok[1])
|
|
|
|
}
|
2016-01-11 06:18:52 +00:00
|
|
|
case "!scorg":
|
2016-01-10 17:45:21 +00:00
|
|
|
if len(tok) > 1 {
|
|
|
|
showOrganization(tok[1])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 23:17:42 +00:00
|
|
|
func scScrapeLoop() {
|
2016-01-10 17:45:21 +00:00
|
|
|
for {
|
2016-02-15 23:17:42 +00:00
|
|
|
scScraper()
|
2016-01-10 17:45:21 +00:00
|
|
|
time.Sleep(1 * time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-15 23:17:42 +00:00
|
|
|
func scScraper() {
|
2016-01-10 17:45:21 +00:00
|
|
|
var data interface{}
|
|
|
|
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Scraping SC stats")
|
2016-01-10 17:45:21 +00:00
|
|
|
resp, err := http.PostForm(API_URL+API_METHOD,
|
|
|
|
url.Values{"fans": {"true"}, "fleet": {"true"}, "funds": {"true"}})
|
|
|
|
if err != nil {
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Error: %v", err)
|
2016-01-10 17:45:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Error: %v", err)
|
2016-01-10 17:45:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &data)
|
|
|
|
if err != nil {
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Error: %v", err)
|
2016-01-10 17:45:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
stats := data.(map[string]interface{})["data"].(map[string]interface{})
|
|
|
|
//fmt.Println(stats["fans"].(type))
|
2016-02-15 23:17:42 +00:00
|
|
|
curFans := util.ToInt(stats["fans"])
|
|
|
|
curFleet := util.ToInt(stats["fleet"])
|
|
|
|
curFunds := util.ToInt(stats["funds"]) / 100
|
2016-01-10 17:45:21 +00:00
|
|
|
|
|
|
|
nextFans := ((fans / FANS_INT) * FANS_INT) + FANS_INT
|
|
|
|
nextFleet := ((fleet / FLEET_INT) * FLEET_INT) + FLEET_INT
|
|
|
|
nextFunds := ((funds / FUNDS_INT) * FUNDS_INT) + FUNDS_INT
|
|
|
|
|
|
|
|
if curFans >= nextFans {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n[SC] Star Citizens: " + util.NumberToString(curFans, '.')
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
if curFleet >= nextFleet {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n[SC] The UEE Fleet: " + util.NumberToString(curFleet, '.')
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
if curFunds >= nextFunds {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n[SC] Funds raised: " + util.NumberToString(curFunds, '.')
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fans = curFans
|
|
|
|
fleet = curFleet
|
|
|
|
funds = curFunds
|
|
|
|
}
|
|
|
|
|
|
|
|
func showScStats() {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n*** SC User and Funding Stats ***"
|
|
|
|
SayCh <- fmt.Sprintf("*\nFans: %s", util.NumberToString(fans, '.'))
|
|
|
|
SayCh <- fmt.Sprintf("*\nFleet: %s", util.NumberToString(fleet, '.'))
|
|
|
|
SayCh <- fmt.Sprintf("*\nFunds: $ %s", util.NumberToString(funds, '.'))
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func showCitizen(handle string) {
|
|
|
|
resp, err := http.Get(QUERY_CIT_URL + handle)
|
|
|
|
if err != nil {
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Error: %v", err)
|
2016-01-10 17:45:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Error: %v", err)
|
2016-01-10 17:45:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
reName := regexp.MustCompile(RE_CIT_NAME)
|
|
|
|
reRecord := regexp.MustCompile(RE_CIT_RECORD)
|
|
|
|
reHandle := regexp.MustCompile(RE_CIT_HANDLE)
|
|
|
|
reOrg := regexp.MustCompile(RE_CIT_ORG)
|
|
|
|
reSid := regexp.MustCompile(RE_CIT_SID)
|
|
|
|
reRank := regexp.MustCompile(RE_CIT_RANK)
|
|
|
|
name := reName.FindStringSubmatch(string(body))
|
|
|
|
record := reRecord.FindStringSubmatch(string(body))
|
|
|
|
handle_ := reHandle.FindStringSubmatch(string(body))
|
|
|
|
org := reOrg.FindStringSubmatch(string(body))
|
|
|
|
sid := reSid.FindStringSubmatch(string(body))
|
|
|
|
rank := reRank.FindStringSubmatch(string(body))
|
|
|
|
if len(name) > 1 {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n*** Citizen Info ***"
|
|
|
|
SayCh <- "*\n" + "Name: " + html.UnescapeString(string(name[1])) + " [" + string(handle_[1]) + "]"
|
|
|
|
SayCh <- "*\n" + "URL: " + QUERY_CIT_URL + string(handle_[1])
|
|
|
|
SayCh <- "*\n" + "UEE #: " + string(record[1])
|
2016-01-10 17:45:21 +00:00
|
|
|
if len(org) > 1 {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n" + "Organization: " + html.UnescapeString(string(org[1])) + " [" + string(sid[1]) + "]"
|
|
|
|
SayCh <- "*\n" + "Rank: " + html.UnescapeString(string(rank[1]))
|
2016-01-10 17:45:21 +00:00
|
|
|
} else {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n" + "Organization: <none>"
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n" + "*** No Such Citizen ***"
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showOrganization(handle string) {
|
|
|
|
resp, err := http.Get(QUERY_ORG_URL + handle)
|
|
|
|
if err != nil {
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Error: %v", err)
|
2016-01-10 17:45:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-03-12 12:09:25 +00:00
|
|
|
xlog.Info("Error: %v", err)
|
2016-01-10 17:45:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
reName := regexp.MustCompile(RE_ORG_NAME)
|
|
|
|
reCount := regexp.MustCompile(RE_ORG_COUNT)
|
|
|
|
reModel := regexp.MustCompile(RE_ORG_MODEL)
|
|
|
|
reFocus := regexp.MustCompile(RE_ORG_FOCUS)
|
|
|
|
reComm := regexp.MustCompile(RE_ORG_COMM)
|
|
|
|
name := reName.FindStringSubmatch(string(body))
|
|
|
|
count := reCount.FindStringSubmatch(string(body))
|
|
|
|
model := reModel.FindStringSubmatch(string(body))
|
|
|
|
focus := reFocus.FindStringSubmatch(string(body))
|
|
|
|
comm := reComm.FindStringSubmatch(string(body))
|
|
|
|
if len(name) > 1 {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n*** Organization Info ***"
|
|
|
|
SayCh <- "*\nName: " + string(name[1]) + " [" + strings.ToUpper(handle) + "]"
|
|
|
|
SayCh <- "*\nURL: " + QUERY_ORG_URL + strings.ToUpper(handle)
|
|
|
|
SayCh <- "*\nMembers: " + string(count[1])
|
|
|
|
SayCh <- "*\nModel: " + string(model[1])
|
|
|
|
SayCh <- "*\nCommitment: " + string(comm[1])
|
|
|
|
SayCh <- "*\nFocus: " + string(focus[1]) + ", " + string(focus[2])
|
2016-01-10 17:45:21 +00:00
|
|
|
} else {
|
2016-02-16 06:57:14 +00:00
|
|
|
SayCh <- "*\n*** No Such Organization ***"
|
2016-01-10 17:45:21 +00:00
|
|
|
}
|
|
|
|
}
|