Google Custom Search API - Inaccurate results - python

I'm trying to use the aforementioned API to get number of google search results for a query, however it's not giving me the correct results. I have my custom search engine configured to search the entire web. For example, if I search "hello" I get 113,000,000 results. However if I do that myself on Google I get 1,340,000,000. I'm quite new to all this stuff so feeling quite lost, can anybody please help? Thanks
from googleapiclient.discovery import build
import pprint
my_api_key = "API KEY"
my_cse_id = "CSE ID"
def main():
service = build("customsearch", "v1",
developerKey = my_api_key)
res = service.cse().list(q = 'hello', cx= my_cse_id,).execute()
pprint.pprint(res)
if __name__ == '__main__':
main()

Related

Enforce parameters into a google research (Python)

I created a python script that allows me to search on google on a terminal.
from googleapiclient.discovery import build
import pprint
searchSubject = input("Research: ")
#What I'm looking for like on a normal google research
api_key = "my_api_key"
cse_id = "my_cse_id"
def google_search(search_term, api_key, cse_id, **kwargs):
service = build("customsearch", "v1", developerKey=api_key)
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
results = google_search(str(searchSubject), api_key, cse_id, num=10)
for result in results:
pprint.pprint(result)
The thing is, I would like to enforce certain parameters into my research. I am looking for .onion websites. When I type that, I get salad recipes. I would like the research to include explicitly .onion (the . is the key word here). I tried to find proper documentations to enforce parameters but didn't find any.

Azure tranlation api don't deliver results while handover data from arangoDB

I struggle a little with getting an return on my azure translation api call.
My code is based on this code https://github.com/MicrosoftTranslator/PythonConsole and it work perfectly.
I furthermore have a arangoDB with some test data. Which does it work and give me this:Result on db test
However, if i combine both as follow:
from xml.etree import ElementTree
from auth import AzureAuthClient
from arango import ArangoClient
import requests
client = ArangoClient(
protocol='http',
host='localhost',
port=32768,
username='root',
password='password',
enable_logging=True
)
db = client.database('testdb')
test = db.collection('testcol')
def GetTextAndTranslate(finalToken):
fromLangCode = "en"
toLangCode = "de"
textToTranslate = " "
for t in test:
#text to translate
textToTranslate = t['name']
# Call to Microsoft Translator Service
headers = {"Authorization ": finalToken}
translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text={}&to={}".format(textToTranslate, toLangCode)
translationData = requests.get(translateUrl, headers = headers)
# parse xml return values
translation = ElementTree.fromstring(translationData.text.encode('utf-8'))
# display translation if needed
print (translation.text)
if __name__ == "__main__":
#Add your client secret in the next line
client_secret = 'azurepassword'
auth_client = AzureAuthClient(client_secret)
bearer_token = 'Bearer ' + auth_client.get_access_token()
I just get nothing. The console needs less then a second and then I can enter new command on the terminal. But no result displayed, also tried to put it into a file. Azure tell me that I called the API, but I can't see what was processed there.
Thanks for your help!
I tried to test your code for calling Azure Translator API, but I discovered the translator part of your code works fine and the Arango part also works fine. Under your code is not complete for me, the only issue I guess is that the function GetTextAndTranslate(finalToken) shoud be defined as GetTextAndTranslate(test, finalToken) which can be passed the argument test collection like below.
def GetTextAndTranslate(test, finalToken):
# Your code
........
if __name__ == "__main__":
client = ArangoClient(
protocol='http',
host='localhost',
port=32768,
username='root',
password='password',
enable_logging=True
)
db = client.database('testdb')
test = db.collection('testcol')
#Add your client secret in the next line
client_secret = 'azurepassword'
auth_client = AzureAuthClient(client_secret)
bearer_token = 'Bearer ' + auth_client.get_access_token()
GetTextAndTranslate(test, bearer_token)
Hope it helps. Any update, please feel free to let me know.

How to query an advanced search with google customsearch API?

How can I programmatically using the Google Python client library do an advanced search with Google custom search API search engine in order to return a list of first n links based in some terms and parameters of an advanced search I queried?.
I tried to check the documentation(I did not found any example), and this answer. However, the latter did not worked, since currently there is no support for the AJAX API. So far I tried this:
from googleapiclient.discovery import build
import pprint
my_cse_id = "test"
def google_search(search_term, api_key, cse_id, **kwargs):
service = build("customsearch", "v1",developerKey="<My developer key>")
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
results = google_search('dogs', my_api_key, my_cse_id, num=10)
for result in results:
pprint.pprint(result)
And this:
import pprint
from googleapiclient.discovery import build
def main():
service = build("customsearch", "v1",developerKey="<My developer key>")
res = service.cse().list(q='dogs').execute()
pprint.pprint(res)
if __name__ == '__main__':
main()
Thus, any idea of how to do and advanced search with google's search engine API?. This is how my credentials look at google console:
credentials
First you need to define a custom search as described here, then make sure your my_cse_id matches the google API custom search (cs) id, e.g.
cx='017576662512468239146:omuauf_lfve'
is a search engine which only searches for domains ending with .com.
Next we need our developerKey.
from googleapiclient.discovery import build
service = build("customsearch", "v1", developerKey=dev_key)
Now we can execute our search.
res = service.cse().list(q=search_term, cx=my_cse_id).execute()
We can add additional search parameters, like language or country by using the arguments described here, e.g.
res = service.cse().list(q="the best dog food", cx=my_cse_id, cr="countryUK", lr="lang_en").execute()
would serch for "the best dog food" in English and the site needs to be from the UK.
The following modified code worked for me. api_key was removed since it was never used.
from googleapiclient.discovery import build
my_cse_id = "012156694711735292392:rl7x1k3j0vy"
dev_key = "<Your developer key>"
def google_search(search_term, cse_id, **kwargs):
service = build("customsearch", "v1", developerKey=dev_key)
res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
return res['items']
results = google_search('boxer dogs', my_cse_id, num=10, cr="countryCA", lr="lang_en")
for result in results:
print(result.get('link'))
Output
http://www.aboxerworld.com/whiteboxerfaqs.htm
http://boxerrescueontario.com/?section=available_dogs
http://www.aboxerworld.com/abouttheboxerbreed.htm
http://m.huffpost.com/ca/entry/10992754
http://rawboxers.com/aboutraw.shtml
http://www.tanoakboxers.com/
http://www.mondlichtboxers.com/
http://www.tanoakboxers.com/puppies/
http://www.landosboxers.com/dogs/puppies/puppies.htm
http://www.boxerrescuequebec.com/
An alternative using the python requests library if you do not want to use the google discovery api:
import requests, pprint
q='italy'
api_key='AIzaSyCs.....................'
q = requests.get('https://content.googleapis.com/customsearch/v1',
params={ 'cx': '013027958806940070381:dazyknr8pvm', 'q': q, 'key': api_key} )
pprint.pprint(q.json())
This is late but hopefully it helps someone...
For advanced search use
response=service.cse().list(q="mysearchterm",
cx="017576662512468239146:omuauf_lfve", ).execute()
The list() method takes in more args to help advance your search... check args here:
https://developers.google.com/custom-search/json-api/v1/reference/cse/list

youtube API V3 Search Python

I am Trying to use the youtube-api V3 search. I have a flask web application and am trying to output the results into rows on the webpage.
I started with the Google developers pages (works perfect in terminal) then found this great sounding example which would output thumbnails and hyper links from the results. However it ran on Google app engine and I am trying to create a Flask application.
So with my very limited knowledge I have tried to merge them to fit my needs.
#!/usr/bin/python
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser
import sys
import os
import urllib
# Set API_KEY to the "API key" value from the "Access" tab of the
# Google APIs Console http://code.google.com/apis/console#access
# Please ensure that you have enabled the YouTube Data API and Freebase API
# for your project.
API_KEY = "REPLACE ME" #Yes I did replace this with my API KEY
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
QUERY_TERM = "dog"
def search_by_keyword():
youtube = build(
YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
developerKey=API_KEY
)
search_response = youtube.search().list(
q=QUERY_TERM,
part="id,snippet",
maxResults=25
).execute()
videos = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["videoId"]))
print "Videos:\n", "\n".join(videos), "\n"
if __name__ == "__main__":
try:
youtube_search()
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
What I actually want it to do is return results to my webpage rather than just print but thought it was best to take one step at a time. If I can make it work in terminal I could then sort it on a webpage, but not as simple as I thought.
Currently I receive error;
Traceback (most recent call last):
File "search.py", line 38, in <module>
print "Videos:\n", "\n".join(videos), "\n"
NameError: name 'videos' is not defined
Which is my first main hurdle as I was expecting it to work as it did in both examples I took it from.
My second problem is how do I go about converting results (assuming I could fix above) into a table on the webpage?
I plan to have
#app.route('/search')
include the above code and then;
return render_template ('search.html')
But unsure how to pass the results back onto it, do I still need the print for this or is it another term I should be using? I want to be able to use the VideoID returned from each to sort thumbnails and links.
your list of videos is a local variable and doesn't exist outside your function "search_by_keyword()" to access the videos variable, you could return that variable like so:
def search_by_keyword():
youtube = build(
YOUTUBE_API_SERVICE_NAME,
YOUTUBE_API_VERSION,
developerKey=API_KEY
)
search_response = youtube.search().list(
q=QUERY_TERM,
part="id,snippet",
maxResults=25
).execute()
videos = []
for search_result in search_response.get("items", []):
if search_result["id"]["kind"] == "youtube#video":
videos.append("%s (%s)" % (search_result["snippet"]["title"],
search_result["id"]["videoId"]))
return videos
in your main function you could use something like:
videos = search_by_keyword()
in that way you could print the videos to the console or send it to a template with Flask.

Facebook API Public Friends of A USERID

I am making a web service using django python. I am able to get my friends information when I logged in. I want to know the steps or an API in which if I pass a userId it will give me list of his/her public friends
You need a valid access_token and a Graph API call to /me/friends
The API documentation for this is listed at https://developers.facebook.com/docs/reference/api/user/#friends
Not sure which Django module you are using or whether there is a stable one out there but the following is in facepy github.com/jgorset/facepy
import web
from facepy import GraphAPI
from urlparse import parse_qs
url = ('/', 'index')
app_id = "YOUR_APP_ID"
app_secret = "APP_SECRET"
post_login_url = "http://0.0.0.0:8080/"
class index:
def GET(self):
user_data = web.input(code=None)
code = user_data.code
if not user_data.code:
dialog_url = ( "http://www.facebook.com/dialog/oauth?" +
"client_id=" + app_id +
"&redirect_uri=" + post_login_url )
return "<script>top.location.href='" + dialog_url + "'</script>"
else:
graph = GraphAPI()
response = graph.get(
path='oauth/access_token',
client_id=app_id,
client_secret=app_secret,
redirect_uri=post_login_url,
code=code
)
data = parse_qs(response)
graph = GraphAPI(data['access_token'][0])
friends_data = graph.get('me/friends')
if __name__ == "__main__":
app = web.application(url, globals())
app.run()
Have you checked out https://pypi.python.org/pypi/facebook-sdk ?
Seems like it might do what you are looking for.

Categories

Resources