0

Consider the following PHP code that summarizes text from a URL:

$url = 'http://www.my-website.com/article-123.php';
$webService = 'https://resoomer.pro/websummarizer/';
$datasPost = 'API_KEY=MY_API_KEY&url='.$url;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $webService);
curl_setopt($ch,CURLOPT_POST, 2);
curl_setopt($ch,CURLOPT_POSTFIELDS, $datasPost);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

It returns a JSON result like:

{
"ok":1,
"message":"Resoomer ok",
"longText":{
"size":45,
"content":"Votre texte résumé à 45%..."
},
"mediumText":{
"size":25,
"content":"Votre texte résumé à 25%..."
},
"smallText":{
"size":15,
"content":"Votre texte résumé à 15%..."
},
"codeResponse":200
}

I tried:

response=requests.get("https://resoomer.pro/websummarizer/?API_KEY=MY_API_KEY&url=https://en.wikipedia.org/wiki/Statistical_hypothesis_testing")

This works perfectly fine.

So I tried the Resoomer API text summarizer:

$MyText = 'My text plain...';
$webService = 'https://resoomer.pro/summarizer/';
$datasPost = 'API_KEY=MY_API_KEY&text='.$MyText;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $webService);
curl_setopt($ch,CURLOPT_POST, 2);
curl_setopt($ch,CURLOPT_POSTFIELDS, $datasPost);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

This will give an JSON response in return like:

{
"ok":1,
"message":"ok",
"text":{
"size":45,
"total_words":219,
"content":"Your text summarize to 45%..."
},
"codeResponse":200
}

I want to accomplish this in Python 3.5 or Python 3.6.

I tried:

url = 'https://resoomer.pro/summarizer/'
Text="'A statistical hypothesis, sometimes called confirmatory data analysis, is a hypothesis that is testable on the basis of observing a process that is modeled via a set of random variables.[1] A statistical hypothesis test is a method of statistical inference. Commonly, two statistical data sets are compared, or a data set obtained by sampling is compared against a synthetic data set from an idealized model. A hypothesis is proposed for the statistical relationship between the two data sets, and this is compared as an alternative to an idealized null hypothesis that proposes no relationship between two data sets. The comparison is deemed statistically significant if the relationship between the data sets would be an unlikely realization of the null hypothesis according to a threshold probability—the significance level. Hypothesis tests are used in determining what outcomes of a study would lead to a rejection of the null hypothesis for a pre-specified level of significance. The process of distinguishing between the null hypothesis and the alternative hypothesis is aided by identifying two conceptual types of errors, type 1 and type 2, and by specifying parametric limits on e.g. how much type 1 error will be permitted.An alternative framework for statistical hypothesis testing is to specify a set of statistical models, one for each candidate hypothesis, and then use model selection techniques to choose the most appropriate model.[2] The most common selection techniques are based on either Akaike information criterion or Bayes factor.Confirmatory data analysis can be contrasted with exploratory data analysis, which may not have pre-specified hypotheses.'"
datasPost = r'API_KEY=MY_API_KEY&text='+Text
response = requests.get(url+r"?"+datasPost)

It doesn't work.

Does anyone have a clue on how to fix this?

I checked for any escaping in text, but I found nothing.

For more reference about API, please visit resoomer API.

3
  • "It doesn't work." - what do you mean by that. Is there some error returned from the service? Do you get local exception from your code? You'll need to give us more details about what exactly happens when you try. Commented Sep 3, 2018 at 0:43
  • returned a status "Invalid TOKEN". Commented Sep 3, 2018 at 17:47
  • It looks like the answer from the service you are calling, have you tried to add your own API key? The r before 'API_KEY' was intentional? Commented Sep 3, 2018 at 20:24

2 Answers 2

1

You're doing a few things differently with requests:

  1. This should be a POST, not a GET - requests.post

  2. You wanted to send the api key and text as POST data, not as url parameters.

This should work instead: (or at least match what you're doing with PHP/curl)

requests.post(url, data=datasPost)

If you still get the invalid token response, you'll have to double-check you're actually sending what you expected.

Sign up to request clarification or add additional context in comments.

Comments

0

i managed to solve this problem using pycurl

here's the code for jupyter notebook - python 3.x

installating pycurl

!apt-get install libcurl4-openssl-dev
!apt-get install libssl-dev
!pip install pycurl

code for retriving response

import pycurl
from io import BytesIO
datasPost='API_KEY='+API+'&text='+str(article)
response= BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.URL, 'https://resoomer.pro/summarizer/')
c.setopt(pycurl.POST, 2)
c.setopt(pycurl.POSTFIELDS, datasPost)
c.setopt(c.WRITEFUNCTION, response.write)
c.perform()
content = response.getvalue().decode('UTF-8')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.