Files
copyparty/bin/hooks/notify.py

67 lines
1.5 KiB
Python
Raw Normal View History

2023-01-22 23:35:31 +00:00
#!/usr/bin/env python3
2023-01-29 15:14:22 +00:00
import os
2023-01-22 23:35:31 +00:00
import sys
2023-01-29 15:14:22 +00:00
import subprocess as sp
2023-01-22 23:35:31 +00:00
from plyer import notification
_ = r"""
2023-01-29 15:14:22 +00:00
show os notification on upload; works on windows, linux, macos, android
2023-01-22 23:35:31 +00:00
depdencies:
2023-01-29 15:14:22 +00:00
windows: python3 -m pip install --user -U plyer
linux: python3 -m pip install --user -U plyer
macos: python3 -m pip install --user -U plyer pyobjus
android: just termux and termux-api
2023-01-22 23:35:31 +00:00
2023-01-29 15:14:22 +00:00
example usages; either as global config (all volumes) or as volflag:
2023-01-22 23:35:31 +00:00
--xau f,bin/hooks/notify.py
2023-03-26 22:18:48 +00:00
-v srv/inc:inc:r:rw,ed:c,xau=f,bin/hooks/notify.py
^^^^^^^^^^^^^^^^^^^^^^^^^^^
(share filesystem-path srv/inc as volume /inc,
readable by everyone, read-write for user 'ed',
running this plugin on all uploads with the params listed below)
2023-01-22 23:35:31 +00:00
parameters explained,
xau = execute after upload
f = fork so it doesn't block uploads
"""
2023-01-31 19:03:13 +00:00
try:
from copyparty.util import humansize
except:
def humansize(n):
return n
2023-01-22 23:35:31 +00:00
def main():
2023-01-31 19:03:13 +00:00
fp = sys.argv[1]
dp, fn = os.path.split(fp)
try:
sz = humansize(os.path.getsize(fp))
except:
sz = "?"
msg = "{} ({})\n📁 {}".format(fn, sz, dp)
2023-01-29 15:14:22 +00:00
title = "File received"
if "com.termux" in sys.executable:
sp.run(["termux-notification", "-t", title, "-c", msg])
return
icon = "emblem-documents-symbolic" if sys.platform == "linux" else ""
notification.notify(
title=title,
message=msg,
app_icon=icon,
timeout=10,
)
2023-01-22 23:35:31 +00:00
if __name__ == "__main__":
main()