Open Terminal and Run Curl in Python - python

I have been trying to run this code,
import csv
import os
token = 'Bearer xxx"'
with open('uris.csv', 'r', newline = '',encoding = 'utf-8') as ifp:
ir = csv.reader(ifp)
for i, row in enumerate(ir):
v = ('curl -X "POST" "https://api.spotify.com/v1/playlists/7miRhC7OZhQUvnP1ONghJm/tracks?uris=spotify%3Atrack%3A'+
', '.join(row)+
'" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: '+
token + '\n')
os.system("gnome-terminal -e 'bash -c \""+v+"; sleep 10\" '")
print(v)
which takes some spotify track uris from a csv and
puts them into the curl that anyone can get from Spotify Api. This curl adds the specific track to my playlist. Then I open a new terminal and execute the curl. (the os.system command was found from open terminal run command python)
The problem is that when I execute this code with python3 code.py new terminals for each curl open but none curl is executed. The curls themselves are right. If I copy paste one curl and run it, the track is added to my playlist. Also if I run a curl separately, I get a response in the terminal which i don't get if I run curls through the code. The response is something like this:
{
"snapshot_id" : "MTQxxx"
}
Thanks a lot.

Silly to ask, but why not just directly call curlOutput = os.system("curl [your concatenation]")?
Might just solve your issue.

Related

collect others parameters in my flask api [duplicate]

This question already has answers here:
Using cURL to upload POST data with files
(11 answers)
Closed 2 months ago.
I send a json file by curl with this command and its work :
curl -X POST -H "Content-Type: application/json" -d #/Users/test/testjson.json 'http://127.0.0.1:5000
I collect this data like and its work :
#app.route('/json_test', methods=['POST'])
def process_data():
# Collect the JSON file from the request
data = json.loads(request.data)
#
I want to add both parameters source and id.
How can I send these parameters in Curl plus my json file . I tried
curl -i -X POST -H "Content-Type: multipart/form-data"
-F "data /Users/test/testjson.json” -F "source=Value1" -F "ident=Value2" http://127.0.0.1:5000
But it's not working
if you can help me to have the command and how i can read this data with python.
Please provide more information of the content on both values you want to add.
I changed a quotation mark inside your curl, try with:
curl -i -X POST -H 'Content-Type: multipart/form-data'
-F 'data /Users/test/testjson.json' -F "source=Value1" -F 'ident=Value2' http://127.0.0.1:5000
To use variables from the curl, you need to update your method. The below curl is another example you can try for now:
#app.route('/json_test', methods=['POST'])
def process_data(source: str, ident: str):
# Collect the JSON file from the request
data = json.loads(request.data)
#
Curl Command:
curl -i -X POST \
'http://127.0.0.1:5000/json_test/?source=value1/?ident=value2' \
-H 'Content-Type: multipart/form-data' \
-F 'data /Users/test/testjson.json'
Try this:
curl -i -X POST -H "Content-Type: multipart/form-data"
-F "user_input=</Users/test/testjson.json” -F "source=Value1" -F "ident=Value2" http://127.0.0.1:5000/json_test
Note that this will create a form with three fields: user_input, source, and ident. You'll need to modify your controller endpoint as well:
#app.route('/json_test', methods=['POST'])
def process_data():
form_content_as_json = json.loads(request.form)

Converting python requests.post to CURL statement

I am trying to convert a python POST requests to a curl statement for the following request:
# this is the requests.post I want to convert to CURL - it works for python but
# I need to run this in a shell script, so I need to convert the following to
# curl statement:
response = requests.post(url,
files=files,
headers=headers)
# the "files" in the above request.post contain a json data AND
# a yaml data as shown below:
files = {
'json': (None, json.dumps(jsondata), 'application/json'),
'file': ('heat_template', heat_yaml,'application/yaml')}
# However, in python, the 'file' class that contains the yaml data is assigned with cgi.FieldStorage class.
# the header contains X-Auth-Token
headers = {}
headers['X-Auth-Token'] = token_value
Originally I tried to use the following curl statement but it doesn't work:
curl -i X POST -d $JSONDATA -H "Content-Type:application/json" -data-urlencode "file#datafile.yaml" -H "Content-Type:application/yaml" $url -H "X-Auth-Token:$TOKEN"
UPDATE: I motified the curl statement to the following and it worked 'partially':
curl -i -X POST -F json="$JSONDATA" -F file="$ENCODED_YAML" $URL -H "X-Auth-Token:$TOKEN"
The destination url $URL is able to translate the json data -F json="$JSONDATA" and the header data H "X-Auth-Token:$TOKEN") correctly from the curl statement, but the -F file="$ENCODED_YAML" is treated as a string python class instead of the expected cgi.FieldStorage python class. How do we pass a file data as a cgi.FieldStorage class in a curl statement?
Appreciate the help!

Posting a file with a REST API: from a curl example to Python code

I'm trying to code the upload of a file to a web service through a REST API in Python. The service's documentation shows a example using curl as client:
curl -X POST -H \
-H "Content-Type: multipart/form-data" \
-F "file=filename.ext" \
-F "property1=value1" \
-F "property2=value2" \
-F "property3=value3" \
https://domain/api/endpoint
The difficulty for me is that this syntax doesn't match multipart form-data examples I found, including the requests documentation. I tried this, which doesn't work (rejected by the API):
import requests
file_data = [
("file", "filename.ext"),
("property1", "value1"),
("property2", "value2"),
("property3", "value3"),
]
response = requests.post("https://domain/api/endpoint",
headers={"Content-Type": "multipart/form-data"}, files=file_data)
With the error: "org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found"
Can anybody help in transposing that curl example to proper Python code?
Thanks!
R.
OK, looks like the web services documentation is wrong, and metadata simply needs to be sent as parameters. Moreover, I found in another request that you shouldn't set the header. So I was starting from a wrong example.

Instagram pagination tag

I am doing a development based on instagram, I do not use the API because they do not allow applications in development mode but in production mode.
So, I'm trying to get the following pages regarding a hashtag, for example:
https://www.instagram.com/explore/tags/plebiscito/
The next page is done by sending a POST method, to a /query / and a /ajax/bz, however, trying to try it with cURL does not work for me.
I leave as I have been doing with cURL.
curl "https://www.instagram.com/explore/tags/plebiscito/" --http1.1 -k "https://www.instagram.com/query/" -H "Content-Type: application/x-www-form-urlencoded" -X POST -d "q=ig_hashtag(plebiscito)+{+media.after(j0hwe66aaaaaf0hwexjmwaaafkwa,+12)+{++count,++nodes+{++++caption,++++code,++++comments+{++++++count++++},++++comments_disabled,++++date,++++dimensions+{++++++height,++++++width++++},++++display_src,++++id,++++is_video,++++likes+{++++++count++++},++++owner+{++++++id++++},++++thumbnail_src,++++video_views++},++page_info}+}&ref=tags::show&query_id=/" --next --http1.1 -k "https://www.instagram.com/ajax/bz" -H "Content-Type: application/json" -X POST -d '{"q":[{"page_id":"7mj51x","posts":[["timespent_bit_array",{"tos_id":"7mj51x","start_time":1481556875,"tos_array":[3,0],"tos_len":2,"tos_seq":2,"tos_cum":19,"log_time":1481556876912},1481556876912,0]],"trigger":"timespent_bit_array"}],"ts":1481556877336}' --next -k "https://www.instagram.com/query/" -H "Content-Type: application/x-www-form-urlencoded" -X POST -d "q=ig_hashtag(plebiscito)+{+media.after(j0hwe66aaaaaf0hwexjmwaaafkwa,+8)+{++count,++nodes+{++++caption,++++code,++++comments+{++++++count++++},++++comments_disabled,++++date,++++dimensions+{++++++height,++++++width++++},++++display_src,++++id,++++is_video,++++likes+{++++++count++++},++++owner+{++++++id++++},++++thumbnail_src,++++video_views++},++page_info}+}&ref=tags::show&query_id=/" --next -k "https://www.instagram.com/ajax/bz" -H "Content-Type: application/json" -X POST -d '{"q":[{"page_id":"7mj51x","posts":[["timespent_bit_array",{"tos_id":"7mj51x","start_time":1481556875,"tos_array":[3,0],"tos_len":2,"tos_seq":2,"tos_cum":19,"log_time":1481556876912},1481556876912,0]],"trigger":"timespent_bit_array"}],"ts":1481556877336}'
Here I was trying to get page two, however, it responds with a page not found.
In short, I need to automate the pages with Python, but I was testing it with cURL.
Could you help me? thank you very much.
First of all, you can get a json https://www.instagram.com/explore/tags/plebiscito/?__a=1 It contains page_info with end_cursor. Paginate using max_id=VALUE-FROM-end_cursor in GET-request. More info here.
If you want to use POST /query you need right cookie.

python curl for assembla

I am trying to port the following curl call into a python script using urllib/urllib2 :
curl -H "X-Api-Key: ccccccccccccccccccccccc" -H "X-Api-Secret: cbcwerwerwerwerwerwerweweewr9" https://api.assembla.com/v1/users/user.xml
I tried using the standard url call, but it failed:
url_assembla = 'https://api.assembla.com/v1/users/suer.xml'
base64string_assembla = base64.encodestring('%s:%s' % ('ccccccccccccccccccccccc','cbcwerwerwerwerwerwerweweewr9')).replace('\n', '')
req_assembla = urllib2.Request(url_assembla)
req_assembla.add_header("Authorization", "Basic %s" % base64string_assembla)
ET.parse(urllib2.urlopen(req_assembla))
Can any one advice on how to incorporate the Api-secret and Api-key. I want to do this as a script, so did not want to install the assembla package.
Thanks
If the curl command above works, you should perhaps just add the same headers into the urllib2 query?
Try this instead of the add_header("Authorization") line:
req_assembla.add_header("X-Api-Key", 'ccccccccccccccccccccccc')
req_assembla.add_header("X-Api-Secret", 'cbcwerwerwerwerwerwerweweewr9')

Categories