Created
February 27, 2024 02:37
-
-
Save dereckson/405a29164e4d9e5cf4ba3cc65879a153 to your computer and use it in GitHub Desktop.
Check if you're online on Idle RPG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env tclsh8.6 | |
# ------------------------------------------------------------- | |
# IdleRPG client | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
# Description: Allow to query XML API from IdleRPG | |
# License: BSD-2-Clause | |
# Usage: idlerpg-check <player name> | |
# ------------------------------------------------------------- | |
package require http | |
package require tls | |
package require xml | |
package require dom | |
set idlerpg_url "https://idlerpg.lolhosting.net/xml.php?player=" | |
http::register https 443 [list ::tls::socket -autoservername true] | |
# ------------------------------------------------------------- | |
# IdleRPG check | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
proc idlerpg_check_is_online {player} { | |
set is_online [idlerpg_is_online $player] | |
if {$is_online == ""} { | |
puts stderr "Can't find information about $player." | |
exit 2 | |
} | |
if {!$is_online} { | |
puts "⚠ You're offline on IdleRPG." | |
} | |
} | |
# ------------------------------------------------------------- | |
# API client | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
proc idlerpg_query {player} { | |
global idlerpg_url | |
set token [http::geturl $idlerpg_url$player] | |
set response [http::data $token] | |
http::cleanup $token | |
return $response | |
} | |
proc idlerpg_is_online {player} { | |
xpath_search [idlerpg_query $player] /player/online | |
} | |
# ------------------------------------------------------------- | |
# XML helpers | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
proc xpath_search {document expression} { | |
set xmlDoc [dom::parse $document] | |
dom::node stringValue [dom::selectNode $xmlDoc $expression] | |
} | |
# ------------------------------------------------------------- | |
# Entry point | |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
if {$argc < 1} { | |
puts stderr "Usage: [info script] <player name>" | |
exit 1 | |
} | |
idlerpg_check_is_online [lindex $argv 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment