Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Codeforces and Atcoder Profiles #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/homeController.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ func Logout(c *fiber.Ctx) error {

func NotFound(c *fiber.Ctx) error {
return c.Render("home/404", fiber.Map{})
}
}
190 changes: 188 additions & 2 deletions controllers/profileController.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@ import (
"io"
"net/http"
"time"

"html/template"
"github.com/gofiber/fiber/v2"
"github.com/dhanrajchaurasia/CP-GRIND/initializers"
"github.com/dhanrajchaurasia/CP-GRIND/models"
"github.com/enescakir/emoji"
)

// func UserProfile(c *fiber.Ctx) error {
// return c.Render("profile/index", fiber.Map{})
// }

func UserProfile(c *fiber.Ctx) error {
return c.Render("profile/index", fiber.Map{})
msg := c.Cookies("message")
cfProfile := template.HTML(c.Cookies("cfProfile"))
c.ClearCookie("message")
c.ClearCookie("cfProfile")
data := fiber.Map{
"User": c.Cookies("username"),
"CF_Profile": cfProfile,
"Message": msg,
}
return c.Render("profile/index", data)
}

func GetCFProfile(c *fiber.Ctx) error {
Expand Down Expand Up @@ -58,3 +74,173 @@ func GetCFProfile(c *fiber.Ctx) error {
})
return c.Redirect("/")
}


func GetCodeforces(c *fiber.Ctx) error {
IfPresent := initializers.IsProfilePresent(c.FormValue("cf-handle"))
// var info models.Profile
// var err
if IfPresent > 0 {
info := initializers.CFProfile(c.FormValue("cf-handle"))
Handle := info.Handle
rank := info.Rank
rating := info.Rating
maxRank := info.MaxRank
maxRating := info.MaxRating
htmlBody := fmt.Sprintf("%v<br>%v CF Rank: %v<br>%v CF Rating: %v<br>%v Max Rating: %v<br>%v Max Rank: %v", Handle, emoji.ManTechnologist, rank, emoji.SmilingFaceWithSunglasses, int(rating), emoji.SportsMedal, int(maxRating), emoji.Trophy, maxRank)
c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})

}
if IfPresent == 0 {
url := fmt.Sprintf("https://codeforces.com/api/user.info?handles=%v", c.FormValue("cf-handle"))
response, err := http.Get(url)
htmlBody := "Could not get the data!"
c.Cookie(&fiber.Cookie{Name: "message", Value: "", Expires: time.Now()})
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})
c.Cookie(&fiber.Cookie{
Name: "message",
Value: err.Error(),
})
return c.Redirect("/profile")
}
defer response.Body.Close()
jsonData, _ := io.ReadAll(response.Body)
var data map[string]interface{}
err = json.Unmarshal(jsonData, &data)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})
c.Cookie(&fiber.Cookie{
Name: "message",
Value: err.Error(),
})
return c.Redirect("/profile")
}
result := data["result"].([]interface{})
if len(result) > 0 {
user := result[0].(map[string]interface{})
rank := user["rank"].(string)
Handle := user["handle"].(string)
rating := user["rating"].(float64)
contributions := user["contribution"].(float64)
lastOnline := user["lastOnlineTimeSeconds"].(float64)
friendCount := user["friendOfCount"].(float64)
maxRating := user["maxRating"].(float64)
maxRank := user["maxRank"].(string)
htmlBody = fmt.Sprintf("%v<br>%v CF Rank: %v<br>%v CF Rating: %v<br>%v Contributions: %v<br>Last online: %v<br>%v Friends: %v<br>%v Max Rating: %v<br>%v Max Rank: %v", Handle, emoji.ManTechnologist, rank, emoji.SmilingFaceWithSunglasses, int(rating), emoji.Star, int(contributions), int(lastOnline/31536000),emoji.GlowingStar, int(friendCount),emoji.SportsMedal, int(maxRating), emoji.Trophy, maxRank)

//database
profile := models.Profile{
Handle: Handle,
Rank: rank,
Rating: rating,
MaxRank: maxRank,
MaxRating: maxRating,
Email: c.FormValue("cf-email"),
}
// c.Cookie(&fiber.Cookie{
// Name: "cfProfile",
// Value: htmlBody,
// })
initializers.CreateNewProfile(profile)
// if err != nil {
// return c.Render("profile/index", fiber.Map{
// "Message": err.Error(),
// })
// }
}


c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})

}
return c.Redirect("/profile")
}

//atcoder


func GetAtcoderProfile(c *fiber.Ctx) error {
IfPresent := initializers.IsAtProfilePresent(c.FormValue("cf-handle"))
// var info models.Profile
// var err
if IfPresent > 0 {
info := initializers.ATProfile(c.FormValue("cf-handle"))
Handle := info.Handle
rank := info.Rank
Sumbissions := info.Sumbissions
htmlBody := fmt.Sprintf("%v<br>%v AT Rank: %v<br>%v Sumbissions: %v",Handle, emoji.Trophy, int(rank),emoji.Star, int(Sumbissions))
c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})

}
if IfPresent == 0 {
url := fmt.Sprintf("https://kenkoooo.com/atcoder/atcoder-api/v3/user/ac_rank?user=%v", c.FormValue("cf-handle"))
response, err := http.Get(url)
htmlBody := "Could not get the data!"
c.Cookie(&fiber.Cookie{Name: "message", Value: "", Expires: time.Now()})
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})
c.Cookie(&fiber.Cookie{
Name: "message",
Value: err.Error(),
})
return c.Redirect("/profile")
}
defer response.Body.Close()
jsonData, _ := io.ReadAll(response.Body)
var data map[string]interface{}
err = json.Unmarshal(jsonData, &data)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})
c.Cookie(&fiber.Cookie{
Name: "message",
Value: err.Error(),
})
return c.Redirect("/profile")
}
// result := data["result"].([]interface{})
if len(data) > 0 {
rank := data["rank"].(float64)
Handle := c.FormValue("cf-handle")
count := data["count"].(float64)
htmlBody = fmt.Sprintf("%v<br>%v AT Rank: %v<br>%v Sumbissions: %v",Handle, emoji.Trophy, int(rank),emoji.Star, int(count))

//database
atprofile := models.ATProfile{
Handle: Handle,
Rank: rank,
Sumbissions: count,
Email: c.FormValue("at-email"),
}
initializers.AddATProfile(atprofile)
}


c.Cookie(&fiber.Cookie{
Name: "cfProfile",
Value: htmlBody,
})
}
return c.Redirect("/profile")
}
23 changes: 4 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@ require (
)

require (
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bep/godartsass v1.2.0 // indirect
github.com/bep/godartsass/v2 v2.0.0 // indirect
github.com/bep/golibsass v1.1.1 // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/cosmtrek/air v1.44.0 // indirect
github.com/creack/pty v1.1.18 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/enescakir/emoji v1.0.0 // indirect
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/gohugoio/hugo v0.116.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gravityblast/fresh v0.0.0-20190826141211-0fa698148017 // indirect
github.com/howeyc/fsnotify v0.9.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.1 // indirect
Expand All @@ -37,23 +28,17 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
github.com/pilu/fresh v0.0.0-20190826141211-0fa698148017 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tdewolff/parse/v2 v2.6.7 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.48.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
Loading