nk9 / get_dropbox_link

Code to get the URL of a file in the Dropbox folder
MIT License
7 stars 1 forks source link

Change default URL argument `?dl=0` to `?raw=1` and whitespace from ugly `%20` to better human readable `+` #3

Closed porg closed 1 year ago

porg commented 1 year ago

Thanks for offering this script!

Finally being able to assign a keyboard shortcut to the "Copy Dropbox Link" contextual menu item in Finder is a huge time saver.

Feature Ideas

Realized the potential for other fully automated postprocessing features which for now I do:

  1. manually — Change the default ?dl=0 argument to ?raw=1

    ❌ https://www.dropbox.com/s/xxxx/cat.jpg?dl=0
    ✅ https://www.dropbox.com/s/xxxx/cat.jpg?raw=0
  2. assisted — Find/replace URL encoded space characters in TextEdit from the default ugly %20 to the better human readable +

    ❌ https://www.dropbox.com/s/xxxx/preserve+whitespace+in+filenames+but+well+readable.jpg
    ✅ https://www.dropbox.com/s/xxxx/preserve%20whitespace%20in%20filenames%20but%20well%20readable.jpg

Where must I change the script o achieve these two customizations?

porg commented 1 year ago

My attempts

Outside of Python in the Shell by piping through sed

✅ Normal operation

$ get_dropbox_link.py ~/Dropbox/Public/Notion-Hide-a-property-via-drag-n-drop.mp4 
https://www.dropbox.com/s/•••/Notion-Hide-a-property-via-drag-n-drop.mp4?dl=0

✅ Testing the sed replacement

$ echo 'https://www.dropbox.com/s/•••/Notion-Hide-a-property-via-drag-n-drop.mp4?dl=0' | sed 's|?dl=0|?raw=1|g'
https://www.dropbox.com/s/•••/Notion-Hide-a-property-via-drag-n-drop.mp4?raw=1

✅ Running the postprocessing pipe for real

$ get_dropbox_link.py ~/Dropbox/Public/Notion-Hide-a-property-via-drag-n-drop.mp4 | sed 's|?dl=0|?raw=1|g'
https://www.dropbox.com/s/•••/Notion-Hide-a-property-via-drag-n-drop.mp4?raw=1

✅ Testing to pipe into less

get_dropbox_link.py ~/Dropbox/Public/Notion-Hide-a-property-via-drag-n-drop.mp4 | less
Less opens and shows the result.
In case of submitting multiple filepath arguments less shows multiple lines just fine.

❌ But I fail to set this up as an alias (in aliases.zsh as I use oh-my-zsh )

aliases.zsh contains:

alias dropboxlink="get_dropbox_link.py \$1 | sed 's|?dl=0|?raw=1|g'"

Logging in afresh, running command alias:

$ dropboxlink ~/Dropbox/Public/Notion-Hide-a-property-via-drag-n-drop.mp4 
sed: RE error: illegal byte sequence
usage: get_dropbox_link.py [-h] [--verbose] paths [paths ...]
get_dropbox_link.py: error: the following arguments are required: paths

aliases.zsh contains:

alias dropboxlink="get_dropbox_link.py \$1 | less"

Logging in afresh, running command alias:

$ dropboxlink ~/Dropbox/Public/Notion-Hide-a-property-via-drag-n-drop.mp4         
"/Users/•••/Dropbox/Public/Notion-Hide-a-property-via-drag-n-drop.mp4" may be a binary file.  See it anyway? usage: get_dropbox_link.py [-h] [--verbose] paths [paths ...]
get_dropbox_link.py: error: the following arguments are required: paths
# Pipe is stuck. Aborting with ⌃-C.

My guesses

sed's complaint "illegal byte sequence" and your python script's complaint "may be a binary file" indicated that not the argument is piped through, but rather the file.

nk9 commented 1 year ago

Thanks for opening the issue. Here is the Dropbox docs page which discusses raw vs dl. It's not clear to me that dl=1 would be a better default than dl=0, but it should be easy enough to add a configuration option to allow for either in post-processing.

As for replacing %20 with +, I'm wary of making that change since it could corrupt some URLs. Does it successfully match URLs with literal plus characters in them, for example? Can you find any Dropbox documentation saying how they handle encoding spaces in shared URLs?

porg commented 1 year ago

Hi, glad to hear back from you that soon!

nk9 commented 1 year ago

I've provided arguments for both of these features now. Give them a try and let me know if they work for you.

To answer my question from before: a URL with a plus in the filename is rendered as file%2Bname.txt. So plus characters sent in the URL are always turned into spaces, and it's not ambiguous. So it's maybe possible for this to be the default, but I don't plan to change it. Seems safest to just provide the URLs as directly from Dropbox as possible.

porg commented 1 year ago

Thanks for you initiative!

Tested the current version:

  1. In general the ideal synopsis for CLI apps is arguments first and file(s) last, because if you repeat a previous command (with arrow up) the cursor is at the end of the line, and then you can edit at the end, and the filepath(s) is what you will most likely want to vary on the next invocation.

  2. --query raw=1 worked fine

    $ get_dropbox_link.py  ~/Dropbox/Public/Click\ and\ lift\ finger\ 1a\ works.mp4 --query raw=1    
    https://www.dropbox.com/s/xxx/Click%20and%20lift%20finger%201a%20works.mp4?raw=1
  3. --plus-for-space fails

    get_dropbox_link.py  ~/Dropbox/Public/Click\ and\ lift\ finger\ 1a\ works.mp4 --plus-for-space
    ERROR:root:'PosixPath' object has no attribute 'with_stem'
porg commented 1 year ago

Using the customization arguments in the Automator script is real easy too:

Automator "Run Shell Script" per the original instruction is:

$HOME/bin/get_dropbox_link.py "$@"

Modification, just add your desired arguments after the "$@":

$HOME/bin/get_dropbox_link.py "$@" --query raw=1

Note: When my proposal °1 from the previous post gets implemented, to put the file aruments at the last position, then simply put the formatting arguments before the "$@".

nk9 commented 1 year ago

Actually, it looks like the long option arguments (with --) would be best passed before the files. Certainly, they should work when passed before the file paths. I can change the README, but I don't believe a code change is necessary. Can you please confirm it's working this way for you now?

porg commented 1 year ago

Ad 1) Good news! Tested, and indeed argument --query raw=1 worked fine before and after the files.

Ad 3) --plus-for-space though fails both when inserted before or after the filepath(s), and per each supplied file returns:

ERROR:root:'PosixPath' object has no attribute 'with_stem'
nk9 commented 1 year ago

Which version of Python are you running? It looks like with_stem is new in 3.9.

I can add a warning about the required Python version to the README.

porg commented 1 year ago

My Python versions

$ which python
/usr/bin/python

$ python --version
Python 2.7.16

$ which python3
/usr/local/bin/python3

$ python3 --version
Python 3.11.2

Which Python interpreter runs?

porg commented 1 year ago
nk9 commented 1 year ago

I've revised the feature to account for the new Dropbox shared link style.

porg commented 1 year ago

Sadly I get a bug, created followup issue:

nk9 commented 1 year ago

Thanks for the report. This has been resolved now.