Powershell and API's

Powershell and API's

in API Development

Posted by: Makani Eldrinn.5638

Makani Eldrinn.5638

So first of all I should probably say that I suck at web development and have no desire to build any web tools with the api’s. I did notice though that Powershell has the ability to read the api’s with the Invoke-RestMethod cmdlet. I figured I could put this to use for myself. I’ve been trying to get my last few poi’s and vistas in wvw for a while now, but I’m not in wvw constantly so I miss opportunity’s to get to those poi’s. I know mos.millenum.org has live maps now but i’m not looking at those all the time either. What I have done is made a powershell script that will automatically poll the api on a regular interval and notify me with a popup when the objective has changed to my server’s color, here’s the script:
$url = “https://api.guildwars2.com/v1/wvw/match_details.json?match_id=1-5
$team = “Green”

do {

$result = Invoke-RestMethod $url
$objectives = $result.maps.objectives

$objective1 = $objectives.item("6")[0].owner
“Longview is $objective1”
sleep 300
}
while ( $objective1 -ne $team )

[System.Windows.Forms.MessageBox]::Show(“Longview is Green”,“Move it”,“OK”,“Hand”)

In the example I was monitoring the Longview tower on the red boarderlands. I know it’s not much but hopefully someone else can use this information for other useful things in the future.

Powershell and API's

in API Development

Posted by: poke.3712

poke.3712

TIL about Invoke-RestMethod. That’s pretty cool, thanks for sharing!

@poke // GW2W //

Powershell and API's

in API Development

Posted by: Doenerbude.9451

Doenerbude.9451

I have made similar.
Since I have no idea of Java/PHP in conjunction with json, I let PowerShell call the API and write anything to the database. A small table simply shows the contents of the database. Is not live, but better than nothing: D

Script below
Let someone find something useful

##MySQL
$db_host = ‘host’
$db_name = ‘db-name’
$db_user = ‘db-user’
$db_pass = ‘db-pass’
$db_table = ‘db-table’

##Vars
$lang = ‘de’

##MySQL-Connect
[void][system.reflection.Assembly]::LoadFrom(“C:\Program Files (x86)\MySQL\MySQL Connector Net 6.5.4\Assemblies\v4.0\MySQL.Data.dll”)
$dbconnect = New-Object MySql.Data.MySqlClient.MySqlConnection
$dbconnect.ConnectionString = “server=$db_host;user id=$db_user;password=$db_pass;database=$db_name;pooling=false”
$dbconnect.Open()

$sql = New-Object MySql.Data.MySqlClient.MySqlCommand
$sql.Connection = $dbconnect

##World Events for 2206
$get = Invoke-WebRequest “https://api.guildwars2.com/v1/events.json?world_id=2206
$get = $get | ConvertFrom-Json

for($i = 0; $i -lt $get.events.count;$i++) {
$map_id = $get.events.map_id[$i]
$event_id = $get.events.event_id[$i]
$status = $get.events.state[$i]
$sql.CommandText = “INSERT INTO $db_table (map_id, event_id, status) values (‘$map_id’, ‘$event_id’, ‘$status’) ON DUPLICATE KEY UPDATE status=‘$status’”
$sql.ExecuteNonQuery()
}

##Event-Names (You have to do this once!)
$get = Invoke-WebRequest “https://api.guildwars2.com/v1/event_names.json?lang=$lang
$get = $get.Content | ConvertFrom-Json

for($i = 0; $i -lt $get.events.count;$i++) {
$event_id = $get.id[$i]
$event_name = $get.name[$i]
$sql.CommandText = “UPDATE $db_table SET `event_name` = ‘$event_name’ WHERE `event_id` = ‘$event_id’”
$sql.ExecuteNonQuery()
}

##Map-Names (You have to do this once!)
$get = Invoke-WebRequest “https://api.guildwars2.com/v1/map_names.json?lang=$lang
$get = $get.Content | ConvertFrom-Json

for($i = 0; $i -lt $get.events.count;$i++) {
$map_id = $get.id[$i]
$map_name = $get.name[$i]
$sql.CommandText = “UPDATE $db_table SET `map_name` = ‘$map_name’ WHERE `map_id` = ‘$map_id’”
$sql.ExecuteNonQuery()
}

$dbconnect.Close()