Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit 14bfe0c

Browse files
committedJun 15, 2018
Update curse dl checker to use CAV
1 parent 10158da commit 14bfe0c

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
commands.txt
1010
curse.json.bz2
1111
discordBot
12-
discordLog.txt
12+
discordLog.txt
13+
auth_config.json

‎Main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strings"
66
"time"
77
"github.com/bwmarrin/discordgo"
8-
"github.com/modmuss50/discordBot/minecraft"
8+
"github.com/TechReborn/DiscordBot/curse"
99
"github.com/modmuss50/MCP-Diff/mcpDiff"
1010
"strconv"
1111
"github.com/modmuss50/MCP-Diff/utils"
@@ -14,7 +14,7 @@ import (
1414
"bytes"
1515
"io/ioutil"
1616
"github.com/modmuss50/goutils"
17-
"github.com/modmuss50/discordBot/curse"
17+
"github.com/TechReborn/DiscordBot/minecraft"
1818
)
1919

2020
var (

‎build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ golang {
1919
build 'github.com/patrickmn/go-cache'
2020
build 'github.com/modmuss50/goutils'
2121
build 'github.com/dustin/go-humanize'
22+
build 'github.com/modmuss50/CAV2'
2223
forceUpdate = true
2324
}
2425
platforms = System.getProperty("platforms", "linux-amd64,windows-amd64,darwin-amd64")

‎curse/Curse.go

+46-23
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"github.com/modmuss50/goutils"
88
"encoding/json"
99
"strings"
10-
"strconv"
1110
"github.com/dustin/go-humanize"
1211
"github.com/bwmarrin/discordgo"
1312
"sort"
1413
"github.com/patrickmn/go-cache"
1514
"time"
1615
"compress/bzip2"
16+
"github.com/modmuss50/CAV2"
17+
"strconv"
1718
)
1819

1920
var (
@@ -23,6 +24,12 @@ var (
2324

2425
func Load(){
2526
Cache = cache.New(90*time.Minute, 1*time.Minute)
27+
28+
//logs with with cav
29+
err := cav2.SetupDefaultConfig()
30+
if err != nil {
31+
panic(err)
32+
}
2633
}
2734

2835
func HandleCurseMessage(s *discordgo.Session, m *discordgo.MessageCreate) bool {
@@ -37,7 +44,8 @@ func HandleCurseMessage(s *discordgo.Session, m *discordgo.MessageCreate) bool {
3744

3845
data, found := Cache.Get("data")
3946
if !found {
40-
s.ChannelMessageSend(m.ChannelID, "Curse Cache Expired, re-downloading")
47+
m, merr := s.ChannelMessageSend(m.ChannelID, "Curse Cache Expired, give me a second ...")
48+
s.ChannelTyping(m.ChannelID)
4149
reader, dwerr := goutils.Download("http://clientupdate-v6.cursecdn.com/feed/addons/432/v10/complete.json.bz2")
4250
if dwerr != nil{
4351
s.ChannelMessageSend(m.ChannelID, "failed to download curse data")
@@ -55,62 +63,77 @@ func HandleCurseMessage(s *discordgo.Session, m *discordgo.MessageCreate) bool {
5563
data = database
5664

5765
Cache.Set("data", database, cache.DefaultExpiration)
66+
67+
if merr == nil { //Remove that message
68+
s.ChannelMessageDelete(m.ChannelID, m.ID)
69+
}
5870
}
5971

6072
var database = data.(AddonDatabase)
6173

62-
var downloads int64 = 0
6374

64-
var addons []Addon
75+
76+
var addons []int
6577

6678
for _,addon := range database.Addons {
6779
for _,author := range addon.Authors {
6880
if strings.EqualFold(author.Name, username){
69-
addons = append(addons, addon)
70-
71-
downloads = downloads + getDownloadCount(addon.DownloadCount)
81+
addons = append(addons, addon.Id)
7282
}
7383
}
7484
}
7585

86+
if len(addons) == 0 {
87+
s.ChannelMessageSend(m.ChannelID, "No addons found for " + username)
88+
return true
89+
}
90+
91+
92+
var downloads float64 = 0
93+
addonInfo, err := cav2.GetAddons(addons)
94+
if err != nil {
95+
s.ChannelMessageSend(m.ChannelID, "Something bad happened when loading detailed addon data from curse")
96+
return true
97+
}
98+
99+
for _, addon := range addonInfo {
100+
downloads += addon.DownloadCount
101+
}
102+
76103
if downloads == 0 {
77104
s.ChannelMessageSend(m.ChannelID, "No downloads found for " + username)
78105
return true
79106
}
80107

81-
sort.Sort(SortAddon(addons))
108+
sort.Sort(SortAddon(addonInfo))
82109
projects := ""
83-
for i,addon := range addons {
84-
if i < 10 {
85-
projects = projects + addon.Name + " : `" + humanize.Comma(getDownloadCount(addon.DownloadCount)) + "`\n"
86-
87-
}
110+
for _,addon := range addonInfo {
111+
projects = projects + addon.Name + " : `" + humanize.Comma(round(addon.DownloadCount)) + "`\n"
88112
}
113+
89114
s.ChannelMessageSend(m.ChannelID, projects)
90115

91-
fmt.Println(humanize.Comma(downloads))
92-
s.ChannelMessageSend(m.ChannelID, username + " has `" + humanize.Comma(downloads) + "` total downloads")
116+
fmt.Println(humanize.Comma(round(downloads)))
117+
s.ChannelMessageSend(m.ChannelID, username + " has `" + humanize.Comma(round(downloads)) + "` total downloads over `" + strconv.Itoa(len(addons)) + "` projects")
93118
}
94119
return false
95120
}
96121

97-
type SortAddon []Addon
122+
type SortAddon []cav2.Addon
98123

99124
func (c SortAddon) Len() int { return len(c) }
100125
func (c SortAddon) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
101-
func (c SortAddon) Less(i, j int) bool { return getDownloadCount(c[i].DownloadCount ) > getDownloadCount(c[j].DownloadCount ) }
102-
103-
func getDownloadCount(number json.Number) int64 {
104-
intstr := strings.Split(number.String(), ".")[0]
105-
int, _ := strconv.ParseInt(intstr, 10, 64)
106-
return int
107-
}
126+
func (c SortAddon) Less(i, j int) bool { return c[i].DownloadCount > c[j].DownloadCount }
108127

109128
func LoadFromBz2(byteArray []byte) string {
110129
reader := bzip2.NewReader(bytes.NewReader(byteArray))
111130
buf := new(bytes.Buffer)
112131
buf.ReadFrom(reader)
113132
s := buf.String()
114133
return s
134+
}
115135

136+
func round(val float64) int64 {
137+
if val < 0 { return int64(val-0.5) }
138+
return int64(val+0.5)
116139
}

‎curse/CurseStructs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import "encoding/json"
44

55
type AddonDatabase struct {
66
TimeStamp int64 `json:"timestamp"`
7-
Addons []Addon `json:"data"`
7+
Addons []AddonDB `json:"data"`
88
}
99

10-
type Addon struct {
10+
type AddonDB struct {
1111
Id int `json:"id"`
1212
Name string `json:"name"`
1313
Authors []Author `json:"authors"`

0 commit comments

Comments
 (0)
This repository has been archived.