Skip to content

Instantly share code, notes, and snippets.

@1player
Last active August 29, 2015 14:05
Show Gist options
  • Save 1player/c16261751cb9299981c7 to your computer and use it in GitHub Desktop.
Save 1player/c16261751cb9299981c7 to your computer and use it in GitHub Desktop.
counterstats
(ns counterstats-clj.core
(:gen-class)
(:require [clj-http.client :as client]
[cheshire.core :refer :all]))
(def steam-key "XXX")
(def csgo-appid "730")
(def steam-profile "76561198005221932")
(defn flatten-steam-stats [stats]
(into {}
(for [stat stats]
[(get stat "name") (get stat "value")])))
(defn steam-get-stats [apikey steamid appid]
(let [resp (client/get "https://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/"
{:query-params {:key apikey
:steamid steamid
:appid appid}})
json (parse-string (:body resp))
playerstats (json "playerstats")
stats (playerstats "stats")]
(flatten-steam-stats stats)))
(defn steam-get-my-stats []
(steam-get-stats steam-key steam-profile csgo-appid))
(defn -main
[& args]
(println "Total headshots:" ((steam-get-my-stats) "total_kills_headshot")))
#lang racket/base
(require net/url)
(require net/uri-codec)
(require json)
(require db)
(require racket/string)
(define steam-key "XXX")
(define csgo-appid "730")
(define steam-profile "76561198005221932")
(define steam-stats-url "https://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/")
(define (flatten-steam-stats stats)
(for/hash ([stat (in-list stats)])
(let ([stat-name (hash-ref stat 'name)]
[stat-value (hash-ref stat 'value)])
(values stat-name stat-value))))
(define (steam-get-stats-params key steamid appid)
(alist->form-urlencoded `((appid . ,appid) (key . ,key) (steamid . ,steamid))))
(define (steam-get-stats key steamid appid)
(let* ([params (steam-get-stats-params key steamid appid)]
[url (string->url (string-append steam-stats-url "?" params))]
[data (get-pure-port url)]
[json (read-json data)]
[playerstats (hash-ref json 'playerstats)]
[stats (hash-ref playerstats 'stats)])
(flatten-steam-stats stats)))
(define (steam-get-my-stats)
(steam-get-stats steam-key steam-profile csgo-appid))
(let ([my-stats (steam-get-my-stats)])
(printf "Total headshots: ~a\n" (hash-ref my-stats "total_kills_headshot")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment