Created
December 15, 2019 03:46
-
-
Save Mukundan314/7acd4e0d826f791a9176cab5a768e646 to your computer and use it in GitHub Desktop.
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
import argparse | |
import json | |
import sys | |
import urllib.parse | |
import urllib.request | |
GOOGLE_API_KEY = "AIzaSyDlTbpjDV_Zh1twcJAP5wqA5fufFd11o5o" | |
# GOOGLE_API_URL = "https://www.googleapis.com/customsearch/v1/siterestrict" | |
GOOGLE_API_URL = "https://www.googleapis.com/customsearch/v1" | |
GOOGLE_SEARCH_ENGINE_ID = "009028027573556014028:bsh42hbg5d2" | |
BING_API_KEY = "226819c937f84d1f91ceffdf6b34fa1e" | |
BING_API_URL = "https://api.cognitive.microsoft.com/bing/v7.0/search" | |
def main(argv): | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"-n", | |
"--num", | |
type=int, | |
default=10, | |
help="maximum number of urls to show, should be less than 10 for google", | |
) | |
parser.add_argument( | |
"-e", | |
"--engine", | |
type=str, | |
choices=["google", "bing"], | |
default="google", | |
help="Search engine to use (default: google)", | |
) | |
parser.add_argument("query", type=str) | |
args = parser.parse_args(argv[1:]) | |
if args.engine == "google": | |
data = urllib.parse.urlencode({ | |
"num": args.num, | |
"key": GOOGLE_API_KEY, | |
"cx": GOOGLE_SEARCH_ENGINE_ID, | |
"q": args.query, | |
}) | |
url = "{}?{}".format(GOOGLE_API_URL, data) | |
with urllib.request.urlopen(url) as res: | |
response = json.load(res) | |
results = map(lambda x: x["link"], response["items"] if "items" in response else []) | |
else: | |
data = urllib.parse.urlencode({"q": args.query, "count": args.num}) | |
url = "{}?{}".format(BING_API_URL, data) | |
req = urllib.request.Request(url, headers={"Ocp-Apim-Subscription-Key": BING_API_KEY}) | |
with urllib.request.urlopen(req) as res: | |
response = json.load(res) | |
results = (map(lambda x: x["url"], response["webPages"]["value"]) | |
if "webPages" in response else []) | |
print(*results, sep="\n") | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment