summarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Morris2020-10-19 16:07:04 -0700
committerKevin Morris2020-10-19 16:07:04 -0700
commitaa50621a57d7170f4a41fb67dcc7ced91f479914 (patch)
tree47a2ff47228950ca3972950cea8f33ebd1b842fb
parent717ff71d4e10cc33bfec5f95c4ddd850190bd04d (diff)
downloadaur-aa50621a57d7170f4a41fb67dcc7ced91f479914.tar.gz
Update sprunge to 0.2.6.
Signed-off-by: Kevin Morris <kevr@0cost.org>
-rw-r--r--.SRCINFO4
-rw-r--r--PKGBUILD4
-rwxr-xr-xsprunge106
3 files changed, 76 insertions, 38 deletions
diff --git a/.SRCINFO b/.SRCINFO
index f3a38601de34..323c2254c77d 100644
--- a/.SRCINFO
+++ b/.SRCINFO
@@ -1,6 +1,6 @@
pkgbase = sprunge
pkgdesc = Upload data to sprunge.us via stdin
- pkgver = 0.2.2
+ pkgver = 0.2.6
pkgrel = 1
url = https://github.com/kevr/sprunge
arch = i686
@@ -10,7 +10,7 @@ pkgbase = sprunge
depends = python
optdepends = xclip: default --clipboard-command.
source = sprunge
- sha256sums = 5a9ff1ff05fca02df5b88dcb052bee1afca7ffa8290845ec94c1b8b61cad4ecf
+ sha256sums = 098b1a2e84c4251d5bd68d017597bb483dacd8565e0f28042c90ae052f1d0fb0
pkgname = sprunge
diff --git a/PKGBUILD b/PKGBUILD
index ac0d474c86d2..e73467e6ce90 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -1,5 +1,5 @@
pkgname='sprunge'
-pkgver='0.2.2'
+pkgver='0.2.6'
pkgrel=1
pkgdesc='Upload data to sprunge.us via stdin'
license=('GPL2')
@@ -10,7 +10,7 @@ depends=('python')
optdepends=('xclip: default --clipboard-command.')
source=('sprunge')
sha256sums=(
- '5a9ff1ff05fca02df5b88dcb052bee1afca7ffa8290845ec94c1b8b61cad4ecf'
+ '098b1a2e84c4251d5bd68d017597bb483dacd8565e0f28042c90ae052f1d0fb0'
)
package()
diff --git a/sprunge b/sprunge
index 69856910f531..076b7de795e1 100755
--- a/sprunge
+++ b/sprunge
@@ -23,54 +23,92 @@ def quit(code, msg):
print(msg)
return code
+def get_paste(id):
+ paste_url = f"http://sprunge.us/{id}"
+ request = urllib.request.Request(paste_url)
+ reply = urllib.request.urlopen(request, timeout=timeout)
+
+ if not reply:
+ return 3, "error: no response from '{}'".format(paste_url)
+
+ response = reply.read().decode()
+ reply.close()
+
+ return 0, response.rstrip()
+
+def post_paste(text):
+ post_data = urllib.parse.urlencode({ "sprunge": text })
+
+ # The actual request process
+ request = urllib.request.Request(url, post_data.encode())
+ reply = urllib.request.urlopen(request, timeout=timeout)
+
+ if not reply:
+ return 3, "error: no response from '{}'".format(url)
+
+ # Read in the reply socket's data and close it cleanly
+ response = reply.read().decode()
+ reply.close()
+
+ return 0, response.rstrip()
+
## Sorry for the C-style functions, I prefer them
def main():
- parser = argparse.ArgumentParser()
- parser.add_argument("--clipboard-command",
- dest="clipboard_command",
+ help_description = "Upload text from stdin to http://sprunge.us. If [id] "
+ help_description += "is provided, the corresponding paste is fetched and "
+ help_description += "displayed."
+ parser = argparse.ArgumentParser(description=help_description)
+ parser.add_argument("--clip-command", "-cc",
+ metavar="clip_command",
default="xclip -sel primary",
- help="Command to use for clipboard pipe.")
+ help="clipboard pipe command (default: 'xclip -sel primary')")
parser.add_argument("--clipboard", "-c",
- dest="clipboard",
+ metavar="clipboard",
action="store_const",
const=True,
default=False,
- help="Copy URL to the primary clipboard.")
+ help="pipe stdout to --clip-command")
+ parser.add_argument("id",
+ nargs="?",
+ help="when provided, fetches and displays a sprunge paste")
args = parser.parse_args()
- if not has_data(sys.stdin):
- return quit(1, "sprunge: no data given via stdin")
+ # If [id] was provided by the user.
+ if args.id is not None:
+ if args.id[:4] == "http" and args.id[:18] != "http://sprunge.us/":
+ return quit(1, "error: invalid id provided; "
+ + "URLs must begin with 'http://sprunge.us/'")
- try:
- stdin = sys.stdin.read()
- except UnicodeDecodeError:
- return quit(2, "sprunge: an error occured reading stdin")
-
- post_data = { "sprunge": stdin }
- post_data = urllib.parse.urlencode(post_data)
+ paste_id = args.id.split("/")[-1]
+ if not paste_id:
+ return quit(1, "error: no id provided")
- # The actual request process
- request = urllib.request.Request(url, post_data.encode("UTF-8"))
- reply = urllib.request.urlopen(request, timeout=timeout)
+ return_code, response = get_paste(paste_id)
+ if return_code != 0:
+ return quit(return_code, response)
+ print(response)
+ else:
+ try:
+ stdin = sys.stdin.read()
+ except UnicodeDecodeError as exc:
+ return quit(2, f"error: {str(exc)}")
- if not reply:
- return quit(3, "sprunge: no response from '{}'".format(url))
+ if not has_data(sys.stdin):
+ return quit(1, "error: no data given via stdin")
- # Read in the reply socket's data and close it cleanly
- response = reply.read().decode("UTF-8")
- reply.close()
+ return_code, response = post_paste(stdin)
+ if return_code != 0:
+ return quit(return_code, response)
+ print(response)
- response = response.rstrip()
- print(response)
-
- # If --clipboard was given, additionally use --clipboard-command
- # to save the resulting URL to the clipboard.
- if args.clipboard:
- proc = Popen([
- "/bin/sh", "-c",
- f'echo "{response}" | {args.clipboard_command}'
- ])
- proc.wait()
+ # If --clipboard was given, additionally use --clipboard-command
+ # to save the resulting URL to the clipboard.
+ if args.clipboard:
+ proc = Popen([
+ "/bin/sh", "-c",
+ f'echo "{response}" | {args.clip_command}'
+ ])
+ proc.wait()
return 0