how to curl file using python - python

I have curl code recived a image file
curl -X GET -H 'Authorization: Bearer TOKENS' https://api.line.me/v2/bot/message/8595925133330/content -o image.png
how to get the file using python?

curl -X GET -H 'Authorization: Bearer TOKENS' https://api.line.me/v2/bot/message/8595925133330/content -o image.png
Comparable Python Script should be like below:
import requests
endpoint = "https://api.line.me/v2/bot/message/8595925133330/content"
headers = {"Authorization":"Bearer TOKENS"}
r = requests.post(endpoint,headers=headers)
open('image.png', 'wb').write(r.content)

Related

API call to Fathom Analytics via Postman

I'm trying to make an API call to Fathom Analytics. But I cannot figure out the filtering option (e.g. filter reportings for specific pathname). Can someone help me figuring this out?
curl --location --request GET 'https://api.usefathom.com/v1/aggregations?entity=pageview&entity_id=[SITE_ID_HERE]&aggregates=visits, uniques, pageviews, avg_duration, bounce_rate' \
--header 'Authorization: Bearer [BEARER_TOKEN_HERE]'
How can I insert the json-payload here in order to use the Fathom Filter option (https://usefathom.com/api#aggregation)
Also, I rebuilt the api call in python, without success. When I remove the payload from the API call it works just fine.
import requests
endpoint = "https://api.usefathom.com/v1/aggregations?entity=pageview&entity_id=[SITE_ID]&aggregates=pageviews,visits,uniques,avg_duration,bounce_rate&date_from=2022-10-01&date_to=2022-10-31"
payload = [{"property": "pathname",
"operator": "is",
"value": "/[URL TO FILTER]"}]
headers = {
"Authorization": "[BEARER_TOKEN]"}
response=requests.get(endpoint, json=payload, headers=headers)
print(response.json())
I struggled a bit with this too, and in my case it worked when I included the "filter" key name in the JSON payload itself rather than the curl command.
Here's what worked for me:
curl https://api.usefathom.com/v1/aggregations \
-H "Authorization: Bearer [BEARER_TOKEN_HERE]" \
-H "Accept: application/json" \
-d entity="pageview" \
-d entity_id="[SITE_ID_HERE]" \
-d aggregates="pageviews" \
-d "{'filters': [{'property':'country', 'operator':'is', 'value':'UK'}]}" \
-G
Moving the JSON to its own file (payload.json) and updating that curl line to this also worked:
-d #payload.json \

Python code for REST calls with cookiejar file

I use following curl commands to fetch data using cookiejar file:
$ curl -sk -X 'POST' -d "username=name#domain.com&password=mypassword" -c app.cookie-jar -k https://website.com/auth/authenticate
$ curl -sk -X 'GET' -H 'Accept: text/csv' -b app.cookie-jar https://website.com/api/systems > out.csv
Can someone help with python script that can help in achieving the same
Using Requests, set up a session to keep track of the cookie:
import requests
with requests.Session() as s:
resp = s.post('https://website.com/auth/authenticate', data='username=name#domain.com&password=mypassword')
resp.raise_for_status()
resp = s.get('https://website.com/api/systems', headers={'Accept': 'text/csv'})
resp.raise_for_status()
with open('out.csv', 'wb') as outf:
outf.write(resp.content)

Submitting Python file in batch mode in Livy(without Hadoop installed)

i have made a simple python file which want to submit in Livy.Livy is currently running in local mode.Also I have mentioned following property in livy.conf file.
Property name: livy.file.local-dir-whitelist,
value "/usr/local/livy/scripts"
My file is kept in following path "/usr/local/livy/scripts"
import json, pprint, requests, textwrap
host = 'http://localhost:8998'
data = {'kind': 'spark'}
headers = {'Content-Type': 'application/json'}
r = requests.post(host + '/sessions', data=json.dumps(data), headers=headers)
r.json()
I am submitting it using curl as follows:
curl -X POST --data '{"file": "/usr/local/livy/scripts/pi.py"}' -H "Content-Type: application/json" 10.140.178.24:8999/batches
It is giving me following error:
requirement failed: Local path /usr/local/livy/scripts/pi.py cannot be added to user sessions.
My Ubuntu system only have following things:
a)Spark
b)Livy
c)Java
What am I doing wrong here?
For people using incubating mode of livy for first time,kindly check that the template file is renamed with stripping off .template in livy.conf.template.Then make sure that the following configurations are present in it.
livy.spark.master = local
livy.file.local-dir-whitelist = /path/to/script/folder/
Kindly make sure that forward slash is present in end of path
Then write url in following manner for
Python:
curl -v -X POST --data '{"file": "/path/to/script/folder/name-of-python-file.py"}' -H "Content-Type: application/json" localhost:8998/batches
Note:It will not accept relative path,whole absolute path needs to be defined in it.
curl -X POST --data '{"file": "/usr/local/livy/scripts/pi.py"}' -H "Content-Type: application/json" 10.140.178.24:8999/batches
{"id":2,"state":"starting","log":[]}

how to use below curl command in python

Below is the curl command and wanted to use in python by using request. I am beginner to python. Appreciate advice/help.
curl --header 'Content-Type: text/xml;charset=UTF-8' --data-binary #c:/abcd.xml -X POST http://www.dneonline.com/calculator.asmx
You can use Requests to POST data:
import requests
url = 'http://www.dneonline.com/calculator.asmx'
files = {'c': open('/abcd.xml', 'rb')}
r = requests.post(url, files=files)
Requests is now a defacto standard.
Either use requests module or call it from a shell.
So,
from subprocess import call
call("curl --header 'Content-Type: text/xml;charset=UTF-8' --data-binary #c:/abcd.xml -X POST",shell=True)

Converting cURL to Python Requests

I need to convert the following cURL command to python requests.
curl 'https://test.com/api/v1/courses/xx/discussion_topics/xx/entries.json' \
-F 'message=<message>' \
-H "Authorization: Bearer <token>"
So far I have requests.post("https://test.com/api/v1/courses/xx/discussion_topics/xx/entries")
but how do I add the message and auth?
requests.post("https://test.com/api/v1/courses/xx/discussion_topics/xx/entries.json, data=json.dumps({"message": "<message>"}), headers={"Authorization": "Bearer <token>"})

Categories

Resources