~xenrox/faceit_checker

0d28c56c826d6a676fd45ae3ddf07c01bde44d62 — Thorben Günther 9 months ago bb32a47
getSteamProfile: Various improvements

Receive context, check if the received link looks like a Steam profile
URL, use HTTP client from receiver.
1 files changed, 16 insertions(+), 6 deletions(-)

M faceit.go
M faceit.go => faceit.go +16 -6
@@ 267,17 267,27 @@ func (fc *faceitChecker) lastMatch(ctx context.Context, faceitID string) (time.T
}

// getSteamProfile returns a steamID64 and the nickname from a profile URL
func getSteamProfile(url string) (*xmlSteam, error) {
func (fc *faceitChecker) getSteamProfile(ctx context.Context, url string) (*xmlSteam, error) {
	url += "?xml=1"

	i := strings.Index(url, "steamcommunity")
	if i == -1 {
		return nil, errors.New("this does not look like a valid Steam profile link")
	}

	// support steam links without leading https://
	if strings.HasPrefix(url, "steamcommunity") {
	if i == 0 {
		url = "https://" + url
	}

	resp, err := http.Get(url)
	req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
	if err != nil {
		return nil, errors.New("wrong steamlink")
		return nil, err
	}

	resp, err := fc.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()



@@ 392,7 402,7 @@ func (fc *faceitChecker) faceitHandler(w http.ResponseWriter, r *http.Request) {
	} else {
		steamLinks := strings.Fields(input)
		for _, steamLink := range steamLinks {
			steamProfile, err := getSteamProfile(steamLink)
			steamProfile, err := fc.getSteamProfile(r.Context(), steamLink)
			if err != nil {
				if err := fc.templates.ExecuteTemplate(w, "error.html", err); err != nil {
					logger.Error("Failed to execute template",


@@ 547,7 557,7 @@ func (fc *faceitChecker) getElo(w http.ResponseWriter, r *http.Request) {
		// Validate steamid; there are two possibilies: User entered correct steamID
		// but has no Faceit account -> give him default elo or user entered wrong steamID
		url := "https://steamcommunity.com/profiles/" + steamID64
		steamProfile, errSteam := getSteamProfile(url)
		steamProfile, errSteam := fc.getSteamProfile(r.Context(), url)
		if errSteam == nil && steamProfile.ID64 == steamID64 {
			if err == ErrNoCSGOFaceit {
				// Set default elo