I wanna automate the download of a specific file from a webserver this specific webserver unfortunately has no public API I could use but they published following shell script to download the file. My problem is I never worked with shell script before so I have no idea how I could port the shell code so it works in python. I have written a script that works with selenium to automate the task but I don't want to see the browser popping up every time I download a file. I already know how to use requests to download files but I don't really understand the "sed -n" part of the code. I appreciate any kind of help. Shell code:
# Make sure the supplied URL looks valid if [ "$(grep -Poc 'nopy.to\/([a-zA-Z0-9]{8})\/(.*?)$' <<< $1)" -lt 1 ]; then echo -e " ! Error: Only nopy.to URLs are supported\n" exit 1 fi # Parse the code and file from URL code=`echo $1 | sed -n 's/.*nopy.to\/\([a-zA-Z0-9]\{8\}\)\/.*/\1/p'` file=`echo $1 | sed -n 's/.*nopy.to\/[a-zA-Z0-9]\{8\}\/\(.*\)\/\?/\1/p'` # Fetch the session echo -e " Fetching session ...\n" sessionreq=`curl -s --data-urlencode "code=$code" --data-urlencode "file=$file" -X POST https://data.nopy.to/file` if [ "$(echo $sessionreq | jq -r '.status')" != "ok" ]; then echo -e " ! Error: Session request failed\n" exit 1 fi if [ "$(echo $sessionreq | jq -r '.msg.error_fatal')" != "false" ]; then echo -e " ! Error: Nopy is having technical issues\n" exit 1 fi if [ -f "$(echo $sessionreq | jq -r '.msg.filename')" ]; then echo -e " ! Error: File \"$(echo $sessionreq | jq -r '.msg.filename')\" already exists\n" exit 1 fi # Fetch the download URL echo -e " Requesting download ticket ...\n" downloadreq=`curl -s --data-urlencode "code=$code" --data-urlencode "fid=$(echo $sessionreq | jq -r '.msg.fid')" --data-urlencode "request=$(echo $sessionreq | jq -r '.msg.request')" --data-urlencode "session=$(echo $sessionreq | jq -r '.msg.session')" -X POST https://data.nopy.to/download`
https://stackoverflow.com/questions/65912151/is-there-a-way-to-port-this-shell-code-to-python January 27, 2021 at 10:36AM
没有评论:
发表评论