I'm trying to write a python script that retrieves information about publications from ISI Web of Science. I found domoritz's python script wos.py on GitHub. It uses Suds to connect to the ISI Web of Science web service. I've imported it into my python script and I tried this code, following the very brief instructions in the comments:
from wos import *
soap = WokmwsSoapClient()
results = soap.search('Hallam')
I then get an error:
suds.WebFault: Server raised fault: 'line 1:1: unexpected token: Hallam'
I looked through the code in wos.py. Here is the search function:
def search(self, query):
qparams = {
'databaseID' : 'WOS',
'userQuery' : query,
'queryLanguage' : 'en',
'editions' : [{
'collection' : 'WOS',
'edition' : 'SCI',
},{
'collection' : 'WOS',
'edition' : 'SSCI',
}]
}
rparams = {
'count' : 5, # 1-100
'firstRecord' : 1,
'fields' : [{
'name' : 'Relevance',
'sort' : 'D',
}],
}
return self.client['search'].service.search(qparams, rparams)
I thought maybe query can't be just a plain python string, as I saw in the WSDL page that userQuery is actually of type xs:string. But this page says that userQuery "Must be a valid WOKQL query statement. This requirement is enforced internally", which makes it seem like I don't have to pass in a special type. Anyway, I tried appending 'xs:string' to the beginning of query but I got the same error.
Does anybody know the proper way to use this method?
You could try to use the Wos Python Client that can be install with:
pip install wos
And then you can use it like this:
from wos import WosClient
import wos.utils
with WosClient('JohnDoe', '12345') as client:
print(wos.utils.query(client, 'AU=Knuth Donald'))
You will also have a CLI tool to be used like:
wos -u 'JohnDoe' -p '12345' query 'AU=Knuth Donald'
*DISCLAIMER: I don't work for Web of Science but I am the author of the client. You need to have web services access (which is a paid service in addition to the normal WOS access) since Web of Science does not allow web services requests coming from normal users. You should ask your university to provide you with the username and password that WOS gave them. This is not just for my client but for anything that uses WOS web service. *
So apparently passing in a python string was fine, but I needed a string that was more like a search query. I found this example on the website I mentioned before:
<soap:Body>
<woksearch:search xmlns:woksearch="http://woksearch.v3.wokmws.thomsonreuters.com">
<!-- this request has the minimum required elements,
but contains all valid retrieve options
for this operation and databaseId -->
<queryParameters>
<databaseId>WOK</databaseId>
<userQuery>AU=Arce, G*</userQuery>
<queryLanguage>en</queryLanguage>
</queryParameters>
....
So I tried using results = soap.search('AU=Hallam') and that worked. I can now do things like print results.recordsFound and I get correct answers.
Related
I'm using this package to run Google Places API on Python. I'm running the following code:
from googleplaces import GooglePlaces, types, lang
key='XXXXXXX'
google_places = GooglePlaces(key)
query_result = google_places.nearby_search(
location='London, England', keyword='Fish and Chips',
radius=20000, types=[types.TYPE_FOOD])
print(query_result)
However when I do that, I get this error:
{
"error_message" : "This API project is not authorized to use this API. Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/geocoding_backend?project=_",
"results" : [],
"status" : "REQUEST_DENIED"
}
However my API is enabled and everything seems cool, as you can see below:
Can someone help here?
I am trying to use the PyBingSearch module from https://github.com/tristantao/py-bing-search to use the Bing API to return search results. In the github page they have an example of how to use it that goes.
from py_bing_search import PyBingWebSearch
search_term = "Python Software Foundation"
bing_web = PyBingWebSearch('Your-Api-Key-Here', search_term, web_only=False) # web_only is optional, but should be true to use your web only quota instead of your all purpose quota
first_fifty_result= bing_web.search(limit=50, format='json') #1-50
When I try and run the line that declares first_fifity_result I get an error stating Request returned with code 401, error msg: The authorization type you provided is not supported. Only Basic and OAuth are supported
Why do I get this error? In my code I made sure to change Your-Api-Key-Here to my API key.
Did you swap your API key? Where is says "Your-API-Key-Here"
bing_web = PyBingWebSearch('Your-Api-Key-Here', search_term, web_only=False) # web_only is optional, but should be true to use your web only quota instead of your all purpose quota
I'm working on a Rails 4 / mongoid application which needs to expose APIs for other applications and scripts. I need to be able to update documents in one of the models through an API with Python 3 script. I'm a bit fresh with Python hence asking for help here.
I already found out how to query Rails APIs with Python 3 and urllib but struggling with updates. I was trying to go through Python 3.5 docs for urllib2 but struggling to apply this to my script.
What goes to data and how to add authentication token to headers, which in curl would look something like this
-H 'Authorization:Token token="xxxxxxxxxxxxxxxx"'
-X POST -d "name=A60E001&status=changed"
I would greatly appreciate if somebody explained how to, for example, update status based on name (name is not unique yet but will be). My biggest challenge is the Python side. Once I have the data in params on Rails side I think I can handle it. I think.
I included my model and update action from the controller below.
app/models/experiment.rb
class Experiment
include Mongoid::Document
include Mongoid::Timestamps
field :name, type: String
field :status, type:String
end
app/controllers/api/v1/experiments_controller.rb
module Api
module V1
class ExperimentsController < ActionController::Base
before_filter :restrict_access
...
def update
respond_to do |format|
if #expt_proc.update(expt_proc_params)
format.json { render :show, status: :ok, location: #expt_proc }
else
format.json { render json: #expt_proc.errors, status: :unprocessable_entity }
end
end
end
...
private
def restrict_access
authenticate_or_request_with_http_token do |token, options|
ApiKey.where(access_token: token).exists?
end
end
...
I figured out who to send a PATCH request with Python 3 and update the the record's status by name.
Thanks to this post found out about requests module. I used requests.patch to update the record and it works great.
python code
import requests
import json
url = 'http://0.0.0.0:3000/api/v1/update_expt_queue.json'
payload = {'expt_name' : 'myExperiment1', 'status' : 'finished' }
r = requests.patch(url, payload)
There are two problems remaining:
How to add headers to this request which would allow me to go through token based authentication. At the moment it's disabled. request.patch only takes 2 parameters, it doesn't take headers parameter.
How to access the JSON which is sent in response by Rails API.
I'm writing an application that utilizes Paypal's permissions API. I'm currently working on the sandbox. I get the verification code correctly but when I try to GetAccessToken, I get the error:
{"responseEnvelope":{"timestamp":"2013-09-03T08:32:16.580-07:00","ack":"Failure","correlationId":"3527b7033f20f","build":"2210301"},"error":[{"errorId":"560022","domain":"PLATFORM","subdomain":"Application","severity":"Error","category":"Application","message":"The X-PAYPAL-APPLICATION-ID header contains an invalid value","parameter":["X-PAYPAL-APPLICATION-ID"]}]}
I'm using the sandbox APP_ID and all the Verification code is also gotten dynamically. Here is my code fragment.
token = "AAAAAAAYaraTSVjvkUBT"
verification = "mgnnWDVfFmgAES0q371Hug"
headers2 = {
"X-PAYPAL-SECURITY-USERID": settings.USERNAME,
"X-PAYPAL-SECURITY-PASSWORD": settings.PASSWORD,
"X-PAYPAL-SECURITY-SIGNATURE": settings.SIGNATURE,
"X-PAYPAL-REQUEST-DATA-FORMAT": "JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT": "JSON",
"X-PAYPAL-APPLICATION-ID": "APP-80W284485P519543T",
}
url = "https://svcs.paypal.com/Permissions/GetAccessToken/?token=%s&verifier=%s" %(token, verification)
dat2 = {"requestEnvelope": {"errorLanguage":"en_US"}}
req2 = urllib2.Request(url, simplejson.dumps(dat2), headers2)
res2 = urllib2.urlopen(req2).read()
What I'm I doing wrong??
You cannot use the sandbox application id on the live environment. See https://developer.paypal.com/webapps/developer/docs/classic/lifecycle/goingLive/#register to learn how to obtain a live application id.
The endpoint should be https://svcs.sandbox.paypal.com as Siddick said above. The paypal API documentation is so inconsistent, the endpoint i had used previously had been used in a sandbox situation in the documentation.
I'm interested in writing a python script to log into Facebook and then request some data (mainly checking the inbox). There are few nice examples out there on how to do this. One interesting script i found over here and there is some nice example on stackoverflow itself.
Now i could just copy-paste some of the code i need and get to do what i want, but that wouldn't be a good way to learn. So i am trying to understand what i am actually coding and can't understand some elements of the script in the first example, namely: what is a post_form_id?
Here is the section of the code which refers to "post_form_id" (line 56-72):
# Initialize the cookies and get the post_form_data
print 'Initializing..'
res = browser.open('http://m.facebook.com/index.php')
mxt = re.search('name="post_form_id" value="(\w+)"', res.read())
pfi = mxt.group(1)
print 'Using PFI: %s' % pfi
res.close()
# Initialize the POST data
data = urllib.urlencode({
'lsd' : '',
'post_form_id' : pfi,
'charset_test' : urllib.unquote_plus('%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84'),
'email' : user,
'pass' : passw,
'login' : 'Login'
})
Would you be so kind to tell me what a post_form_id is? And accessorily: would you know what the lsd key/value stands for?
Thanks.
I don't understand why you are trying to "hack" this ...
There is an official api from facebook to read the mailbox of a user, and you need to ask the "read_mailbox" permission for this.
So I advice you to check my post here on how to use facebook and python/django together, and how to login to facebook from python.
And then I would recommend you to read the facebook doc about the messages/inbox.
Basically you need an access_token then you can do http://graph.facebook.com/me/inbox/?access_token=XXX
You can also ask for the "offline_access" permission so you'll need only to get an access token once and you will be able to use it "forever"
And the you can do http://graph.facebook.com/MESSAGE_ID?access_token=XXX to get the details about a particular message.
Or using the api I use in the other thread :
f = Facebook()
res = f.get_object("me/inbox")
...
Feel free to comment if you have any question about this ?