Google SafeSearch API, Google Locations, and more.

A short post this week. I have been messing with the google API and Powershell. The google APIs tend to follow a pattern so if nothing else I hope these functions work as a solid example. That said I like to make sure stuff is at least a little useful outside of examples.

My favorite is the safe browsing api. Google constantly monitors sites for Malware, Social Engineering and other attacks. This is the same system that warns Google users before clicking a link on a search. Some uses for the function would be to watch domains you own being flagged either correctly or a incorrectly. I was also kicking around the idea of doing an Active directory DNS log parser that caches the queries and compares them to the safe browsing database. This might catch users going with unsafe behavior and the like in a corporate environment.

You can find the function on pastebin here or all the scripts are on my github

Safe browsing function:
#Always fails safe search: malware.testing.google.test/testing/malware/
function get-BrowseSafe() {
Param(
[Parameter(Mandatory = $true)][string]$search,
[AllowEmptyString()]$api_key=""
)
#build the json object to send to google:
$json = '{
"client": {
"clientId": "BrowseSafe_monitor",
"clientVersion": "1"
},
"threatInfo": {
"threatTypes": ["MALWARE", "SOCIAL_ENGINEERING"],
"platformTypes": ["ANY_PLATFORM" ],
"threatEntryTypes": ["URL"],
"threatEntries": [

#loop through multiple semi-colon delimited urls
$search_arr = $search -split “;”
$count_max = $search_arr.count
$count = 1
foreach($item in $search_arr) {
if($count -eq $count_max) {
$json = $json + “{“”url””: “”$item””}”
} else {
$json = $json + “{“”url””: “”$item””},”
}
$count++
}
#close Json
$json = $json + ‘]}}’

#build url with correct api_key
$url = “https://safebrowsing.googleapis.com/v4/threatMatches:find?key=” + $api_key
try {
$result = Invoke-RestMethod “$url” -Method POST -Body $json -ContentType ‘application/json’
$result = $result.matches
} catch {
echo $_
}

if($($result.count) -eq 0) {
return $false
} else {
return $result
}
}

Search-custom uses the google custom search API. Which allows you to setup your own set of sites and query from googles search database.  You have to build a list of sites and base the search themes off of those. Each search list has its own id that can be updated. So the function provided can be used as more of a template for search sites you come up with. Be sure to get the customsearch_id provided by google after you configure your projects search list.

You can find the function on pastebin here or all the scripts are on my github

function search-custom() {
Param(
[Parameter(Mandatory = $true)][string]$search,
[AllowEmptyString()] $customsearch_id = "<provide your own search id with google>",
[AllowEmptyString()]$api_key="<your key>"
)

try {
$Search_results = invoke-restmethod “https://www.googleapis.com/customsearch/v1?q=$search&cr=us&cx=$customsearch_id&key=$api_key”
$search_results = ($search_results.items) | select title, snippet,link
return $search_results
} catch {
return $false
}
}

Search-nearby utilizes the google locations API. This is what recommends restaurants and the like near a location. I wanted this in my powershell profile so I ended up writing a quick wrapper.

You can find the function on pastebin here or all the scripts are on my github

function search-nearby() {
Param(
[Parameter(Mandatory = $true)][string]$search,
[AllowEmptyString()]$api_key="<your key>"
)

try {
$Search_results = invoke-restmethod “https://maps.googleapis.com/maps/api/place/textsearch/json?query=$search&key=$api_key”
$search_results = ($search_results.results) | select Name,types,Formatted_address,price_level,Rating
return $search_results
} catch {
return $false
}

}

The other 2 functions for this post are youtube based. It is worth checking out the youtube api directions. I found the youtube API the most confusing, and I still havn’t spent the time to figure out how to post videos. At least these will get you started with exploring youtube.

You can find the function on pastebin here or all the scripts are on my github


function Get-youtubesearch() {
Param(
[Parameter(Mandatory = $true)][string]$search,
[AllowEmptyString()]$max_page = 5,
[AllowEmptyString()]$copyright = "any",
[AllowEmptyString()]$youtube_key=""
)

$Search_results = invoke-restmethod “https://www.googleapis.com/youtube/v3/search?part=snippet&q=$search&type=video&videoLicense=$copyright&key=$youtube_key”
$page_count = 1
$video_list = @()

while(($Search_results.nextPageToken) -and ($page_count -le $max_page)) {
$next_page=$Search_results.nextPageToken

foreach($video_info in $search_results.items) {
$video_id = $video_info.id.videoid
$video_stats = invoke-restmethod “https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$video_id&key=$youtube_key”
[int]$views = $video_stats.items.statistics.viewcount
[int]$likes = $video_stats.items.statistics.likecount
[int]$dislikes = $video_stats.items.statistics.dislikeCount
$title = $video_info.snippet.title
$link = “https://youtube.com/watch?v=$video_id”

$video_list += new-object psobject -Property @{
title = “$title”;
video_id = “$video_id”;
likes = $likes;
dislikes = $dislikes;
views = “$views”;
link = “$link”;
}

}

$Search_results = invoke-restmethod “https://www.googleapis.com/youtube/v3/search?part=snippet&pageToken=$next_page&type=video&q=$search&videoLicense=$copyright&key=$youtube_key”
$page_count++
}

return $video_list
}

function get-youtubepopular() {
Param(
[AllowEmptyString()]$max_page = 5,
[AllowEmptyString()]$copyright = “any”,
[AllowEmptyString()]$youtube_key=””
)

$Search_results = invoke-restmethod “https://www.googleapis.com/youtube/v3/videos?chart=mostPopular&key=$youtube_key&part=snippet”
$page_count = 1
$video_list = @()

while(($Search_results.nextPageToken) -and ($page_count -le $max_page)) {
$next_page=$Search_results.nextPageToken

foreach($video_info in $search_results.items) {
$video_id = $video_info.id
echo “second search”
$video_stats = invoke-restmethod “https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$video_id&key=$youtube_key”
[int]$views = $video_stats.items.statistics.viewcount
[int]$likes = $video_stats.items.statistics.likecount
[int]$dislikes = $video_stats.items.statistics.dislikeCount
$title = $video_info.snippet.title
$link = “https://youtube.com/watch?v=$video_id”

$video_list += new-object psobject -Property @{
title = “$title”;
video_id = “$video_id”;
likes = $likes;
dislikes = $dislikes;
views = $views;
link = “$link”;
}

}

$Search_results = invoke-restmethod “https://www.googleapis.com/youtube/v3/videos?chart=mostPopular&pageToken=$next_page&key=$youtube_key”
$page_count++
}

return $video_list
}

Sharing is caring!

Leave your comment

four − 2 =