From cbf640710ca9db7246379fa1597070045a9c29b3 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 2 Dec 2017 21:53:01 +0100 Subject: [PATCH] Added companies sorted by popularity --- pages/companies/companies.go | 4 +-- pages/companies/companies.pixy | 29 ++++++++++------- pages/companies/popular.go | 57 ++++++++++++++++++++++++++++++++++ pages/companies/popular.pixy | 9 ++++++ pages/index.go | 3 +- styles/typography.scarlet | 4 +++ 6 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 pages/companies/popular.go create mode 100644 pages/companies/popular.pixy diff --git a/pages/companies/companies.go b/pages/companies/companies.go index 9461b96f..5b379e22 100644 --- a/pages/companies/companies.go +++ b/pages/companies/companies.go @@ -10,8 +10,8 @@ import ( "github.com/animenotifier/notify.moe/utils" ) -// Get renders the companies page. -func Get(ctx *aero.Context) string { +// All renders the companies page. +func All(ctx *aero.Context) string { user := utils.GetUser(ctx) companies := arn.FilterCompanies(func(company *arn.Company) bool { diff --git a/pages/companies/companies.pixy b/pages/companies/companies.pixy index 4f3ba796..127f16d2 100644 --- a/pages/companies/companies.pixy +++ b/pages/companies/companies.pixy @@ -1,6 +1,21 @@ component Companies(groups [][]*arn.Company, user *arn.User) - h1 Companies + CompaniesTabs(user) + .companies + each group in groups + .companies-group + h3= group[0].Name.English[:1] + + ul + each company in group + li.mountable + a.ajax(href=company.Link())= company.Name.English + +component CompaniesTabs(user *arn.User) + .tabs + Tab("All", "font", "/companies") + Tab("Popular", "globe", "/companies/popular") + .corner-buttons if user != nil if user.DraftIndex().CompanyID == "" @@ -10,14 +25,4 @@ component Companies(groups [][]*arn.Company, user *arn.User) else a.button.ajax(href="/company/" + user.DraftIndex().CompanyID + "/edit") Icon("pencil") - span Edit draft - - .companies - each group in groups - .companies-group - h3= group[0].Name.English[:1] - - ul - each company in group - li - a.ajax(href=company.Link())= company.Name.English \ No newline at end of file + span Edit draft \ No newline at end of file diff --git a/pages/companies/popular.go b/pages/companies/popular.go new file mode 100644 index 00000000..a989e42e --- /dev/null +++ b/pages/companies/popular.go @@ -0,0 +1,57 @@ +package companies + +import ( + "sort" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" +) + +const maxPopularCompanies = 100 + +// Popular renders the companies sorted by popularity. +func Popular(ctx *aero.Context) string { + user := utils.GetUser(ctx) + companies := []*arn.Company{} + + // ID to popularity + popularity := map[string]int{} + + for anime := range arn.StreamAnime() { + for _, studio := range anime.Studios() { + popularity[studio.ID] += anime.Popularity.Watching + anime.Popularity.Completed + } + } + + for companyID := range popularity { + company, err := arn.GetCompany(companyID) + + if err != nil { + continue + } + + companies = append(companies, company) + } + + sort.Slice(companies, func(i, j int) bool { + a := companies[i] + b := companies[j] + + aPopularity := popularity[a.ID] + bPopularity := popularity[b.ID] + + if aPopularity == bPopularity { + return a.Name.English < b.Name.English + } + + return aPopularity > bPopularity + }) + + if len(companies) > maxPopularCompanies { + companies = companies[:maxPopularCompanies] + } + + return ctx.HTML(components.PopularCompanies(companies, popularity, user)) +} diff --git a/pages/companies/popular.pixy b/pages/companies/popular.pixy new file mode 100644 index 00000000..1c12aa9b --- /dev/null +++ b/pages/companies/popular.pixy @@ -0,0 +1,9 @@ +component PopularCompanies(companies []*arn.Company, popularity map[string]int, user *arn.User) + CompaniesTabs(user) + + .companies + ol + each company in companies + li.mountable + a.ajax(href=company.Link())= company.Name.English + span= " (" + strconv.Itoa(popularity[company.ID]) + ")" \ No newline at end of file diff --git a/pages/index.go b/pages/index.go index 532da9f4..4a8c8372 100644 --- a/pages/index.go +++ b/pages/index.go @@ -105,7 +105,8 @@ func Configure(app *aero.Application) { // Companies l.Page("/company/:id", company.Get) l.Page("/company/:id/edit", company.Edit) - l.Page("/companies", companies.Get) + l.Page("/companies", companies.All) + l.Page("/companies/popular", companies.Popular) // Settings l.Page("/settings", settings.Get(components.SettingsPersonal)) diff --git a/styles/typography.scarlet b/styles/typography.scarlet index 2b546a2d..fd2fd5a2 100644 --- a/styles/typography.scarlet +++ b/styles/typography.scarlet @@ -17,6 +17,10 @@ strong em font-style italic +ol + li + list-style-type decimal + hr border none border-bottom 1px solid text-color