summaryrefslogtreecommitdiff
path: root/steam.lisp
blob: 8d6151406263c73a28b2ba3fd75b5339b2882eec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(defvar steam-host "api.steampowered.com")

(defun set-steam-key (key)
  (setf (steam-key *config*) key))

(defun set-steam-id (id)
  (setf (steam-user-id *config*) id))

;; JSON
;; * The API returns an object containing the named object with the result data.
;; * Arrays are represented as an array with the name of the type of the objects
;;   in the array (ie. an object named "items" containing an array of objects
;;  of type "item" would be represented as an object named "items" containing
;;  an array named "item" containing several objects following the "item" structure).
;; * Null is represented as JSON's null.


(defun steam-games-uri ()
  (quri:render-uri
   (quri:make-uri-http
    :host steam-host
    :path "IPlayerService/GetOwnedGames/v0001/"
    :query (quri:url-encode-params `(("key" . ,(steam-key *config*))
				     ("steamid" . ,(steam-user-id *config*))
				     ("include_appinfo" . "true"))))))

(defun fetch-steam-games ()
  (derrida:with-keypaths ((games :|response| :|games|))
      (json:parse
       (flexi-streams:octets-to-string
	(drakma:http-request (steam-games-uri))))
    games))

(defun make-steam-game (json)
  (with-plist ((id :|appid|) (playtime :|playtime_forever|) (name :|name|)
	       (icon-url :|img_icon_url|) (last-played :|rtime_last_played|))
      json
    (let ((game (make-instance 'steam-game :name name :appid id)))
      (setf (playtime game) playtime
	    (icon-url game) icon-url
	    (last-played game) last-played)
      game)))

(defun steam-achievements-uri (game)
  (quri:render-uri
   (quri:make-uri-http
    :host steam-host
    :path "ISteamUserStats/GetPlayerAchievements/v0001/"
    :query (quri:url-encode-params `(("key" . ,(steam-key *config*))
				     ("steamid" . ,(steam-user-id *config*))
				     ("appid" . ,(appid game))
				     ("l" . "en"))))))

(defun fetch-steam-achievements (steam-game)
  (derrida:with-keypaths ((success :|playerstats| :|success|)
			  (achievements :|playerstats| :|achievements|))
      (json:parse
       (flexi-streams:octets-to-string
	(drakma:http-request (steam-achievements-uri steam-game))))
    (when success achievements)))

(defun steam-game-schema-uri (game)
  "Returns URI and query params to get detailed info about a game. This is where
the image links for achievements can be found."
  (quri:render-uri
   (quri:make-uri-http
    :host steam-host
    :path "ISteamUserStats/GetSchemaForGame/v2/"
    :query (quri:url-encode-params `(("key" . ,(steam-key *config*))
				     ("appid" . ,(appid game))
				     ("l" . "en"))))))