From 64d1ded5bf1e86c896a4520626df1a7628203e16 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sun, 8 Nov 2015 14:08:34 +0100 Subject: Handle URL arguments Signed-off-by: Florian Pritz --- fb.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/fb.py b/fb.py index 6b1fd69..cc62f50 100755 --- a/fb.py +++ b/fb.py @@ -196,6 +196,26 @@ class CURLWrapper: return result["data"] + def dl_file(self, url, path): + # TODO: this is duplicated in __init__ (well mostly) + c = pycurl.Curl() + c.setopt(c.USERAGENT, self.config['useragent']) + c.setopt(c.HTTPHEADER, [ + "Expect:", + ]) + + if self.config["debug"]: + c.setopt(c.VERBOSE, 1) + + outfp = open(path, 'wb') + try: + c.setopt(c.URL, url) + c.setopt(c.WRITEDATA, outfp) + c.perform() + finally: + outfp.close() + c.close() + class ProgressBar: def __init__(self): @@ -519,6 +539,11 @@ class FBClient: def upload(self): if self.args.tar: + for arg in self.args.args: + if re.match('https?://', arg): + sys.stderr.write("Error: --tar does not support URLs as arguments") + return + tarPath = os.path.join(self.tempdir, 'upload.tar') tar = tarfile.open(tarPath, 'w') for file in self.args.args: @@ -541,9 +566,20 @@ class FBClient: self.upload_files([tempfile]) return else: - self.upload_files(self.args.args) + # TODO: detect paste URLs and add their IDs to a multipaste + files = [self.dl_file(arg) for arg in self.args.args] + self.upload_files(files) return + def dl_file(self, arg): + if re.match('https?://', arg): + outfile = os.path.basename(arg) + self.curlw.dl_file(arg, outfile) + return outfile + + return arg + + def extractId(self, arg): arg = arg.replace(self.config['pastebin'], '') match = re.match('^([^/]+)', arg) -- cgit v1.2.3-24-g4f1b