Jump to content

JSON queries for Google API


iamtheky
 Share

Recommended Posts

The following is an example that fetches name/address/phone number for places of your criteria. If you needed to poll many different locations and aggregate the data, a solution as such may prove useful.

As always my hopes is that people see this pile i left, fix the inefficiencies, and the result weeks from now is far finer than this initial offering.

You need an API key from google (which is free), in your api console make sure you kick on both maps and places

and JSMN from

#Include "JSMN.au3"
#Include <Array.au3>
#Include <File.au3>

Global $aFarray
Global $aDarray

$input1 = inputbox("What Place?" , "Enter the Place Name or Type of Place" , "University")
$input2 = inputbox("Where At?" , "Enter the Zip or City" , "76504")
$radius = inputbox("Prioritize Returns" , "Priority is given to places within this many meters" , "10000")
$sInputs = $input1 & " " & $input2
$location = stringreplace($sInputs , " " , "+")

Local $Json = BinaryToString(InetRead("https://maps.googleapis.com/maps/api/place/textsearch/json?query=" & $location & "&radius=" & $radius & "&sensor=false&key=YourKeyHere"), 4)
Local $Obj = Jsmn_Decode($Json)

$file = fileopen(@ScriptDir & "\JSON_RADIUS_OUTPUT.txt" , 2)
filewrite($file , Jsmn_Encode($Obj, $JSMN_PRETTY_PRINT))
fileclose($file)

_FileReadToArray(@ScriptDir & "\JSON_RADIUS_OUTPUT.txt" , $aFarray)

;~ _ArrayDisplay($aFarray)

For $i = $aFarray[0] to 1 step -1
If stringinstr($aFarray[$i] , '"formatted_address": ') or stringinstr($aFarray[$i] , '"name": ') or stringinstr($aFarray[$i] , '"reference": ') Then
ContinueLoop
Else
_ArrayDelete($aFarray , $i)
Endif
next

_ArrayDelete($aFarray , 0)

For $i = 2 to ubound($aFarray) - 1 step 3
$aFarray[$i] = stringtrimright(stringtrimleft($aFarray[$i], 17),2)
Local $Json2 = BinaryToString(InetRead("https://maps.googleapis.com/maps/api/place/details/json?reference="& $aFarray[$i] &"&sensor=false&key=YourKeyHere"), 4)
Local $Obj2 = Jsmn_Decode($Json2)

$file = fileopen(@ScriptDir & "\JSON_Details_OUTPUT.txt" , 2)
filewrite($file , Jsmn_Encode($Obj2, $JSMN_PRETTY_PRINT))
fileclose($file)

_FileReadToArray(@ScriptDir & "\JSON_Details_OUTPUT.txt" , $aDarray)

For $j = 1 to $aDarray[0]
If stringinstr($aDarray[$j] , '"formatted_phone_number": ') Then
$aFarray[$i] = stringtrimright(stringtrimleft($aDArray[$j], 29),2)
Endif
next

Next

$results = Fileopen(@ScriptDir & "\results.txt" , 2)

for $k = 0 to ubound($aFArray) - 1 step 3
filewrite($results , stringtrimright(stringtrimleft($aFArray[$k+1], 12),2))
filewrite($results , @CRLF)
filewrite($results , stringtrimright(stringtrimleft($aFArray[$k], 25),2))
filewrite($results , @CRLF)
filewrite($results , $aFArray[$k+2])
filewrite($results , @CRLF)
filewrite($results , @CRLF)
Next

FileClose ($results)
shellexecute(@ScriptDir & "\results.txt")
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...