Skip to content

Instantly share code, notes, and snippets.

@mabry1985
Created April 30, 2023 06:21
Show Gist options
  • Save mabry1985/35095144967f28a07849cd6bbfc2246e to your computer and use it in GitHub Desktop.
Save mabry1985/35095144967f28a07849cd6bbfc2246e to your computer and use it in GitHub Desktop.
/*
# Google Search
Example of leveraging Google's Custom Search Engine API to search the web
*/
// Name: Google Search
// Description: Leverage Google's Custom Search Engine API to search the web
// Author: Josh Mabry
// Twitter: @AI_Citizen
import "@johnlindquist/kit";
let GOOGLE_API_KEY = await env("GOOGLE_API_KEY", {
shortcuts: [
{
name: "Google API Key",
key: `${cmd}+o`,
bar: "right",
onPress: () => {
open("https://developers.google.com/custom-search/v1/introduction");
},
},
],
ignoreBlur: true,
secret: true,
height: PROMPT.HEIGHT.INPUT_ONLY,
});
let GOOGLE_CSE_KEY = await env("GOOGLE_CSE_KEY", {
shortcuts: [
{
name: "Google Custom Search Engine Key",
key: `${cmd}+o`,
bar: "right",
onPress: () => {
open("https://programmablesearchengine.google.com/");
},
},
],
ignoreBlur: true,
secret: true,
height: PROMPT.HEIGHT.INPUT_ONLY,
});
let query = await arg(
{
placeholder: "Search Query",
strict: false,
},
[
{
name: "Send a search query to Google",
info: "always",
},
]
);
let search = (q) =>
`https://www.googleapis.com/customsearch/v1?key=${GOOGLE_API_KEY}&cx=${GOOGLE_CSE_KEY}&q=${q}&sort=date`;
let response = await get(search(query));
let items = response?.data?.items;
if (items) {
let choices = items.map((item) => ({
name: item.title,
value: item.link,
}));
let link = await arg("Choose a link to view", choices);
open(link);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment