From 7192e7fde6e1b106496de2267b25f2192bcd92e7 Mon Sep 17 00:00:00 2001 From: silver spring Date: Sat, 26 Apr 2014 16:16:42 -0400 Subject: [PATCH 1/4] adding a little getopts to install * adding a -p options to allow alternate prefix for installation --- install.sh | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/install.sh b/install.sh index 45fefbb..724505d 100755 --- a/install.sh +++ b/install.sh @@ -1,6 +1,18 @@ #! /bin/sh -cp BeautifulSoup.py /usr/local/bin/ -cp MultipartPostHandler.py /usr/local/bin/ -cp microdata.py /usr/local/bin -cp pythonbits.py /usr/local/bin/pythonbits -chmod o+x /usr/local/bin/pythonbits +PREFIX=/usr/local + +while getopts ":p:" Option +do + case $Option in + p) PREFIX=$OPTARG + esac +done + +INSTALLDIR=$PREFIX/bin +echo "Installing to $INSTALLDIR" + +cp BeautifulSoup.py $INSTALLDIR +cp MultipartPostHandler.py $INSTALLDIR +cp microdata.py $INSTALLDIR +cp pythonbits.py $INSTALLDIR/pythonbits +chmod o+x $INSTALLDIR/pythonbits From f27a49672f6eca27e2eef5d8801da549175d0460 Mon Sep 17 00:00:00 2001 From: silver spring Date: Sat, 26 Apr 2014 18:40:52 -0400 Subject: [PATCH 2/4] changing config updating * removing open and status check logic * adding FancyURLopener.retrieve logic --- pythonbits.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pythonbits.py b/pythonbits.py index 19b5601..6d6a7d1 100755 --- a/pythonbits.py +++ b/pythonbits.py @@ -129,16 +129,12 @@ def __init__(self): self.tempdir = TMPDIR self.file=self.tempdir+"config.xml" if not os.path.exists(self.file): - update_url = "https://github.com/Ichabond/Pythonbits/raw/master/config.xml" + update_url = "https://raw.github.com/Ichabond/Pythonbits/master/config.xml" opener = _MyOpener() - nconf = opener.open(update_url) - if nconf.info()["Status"]=="200 OK": - fh = open(self.tempdir+"config.xml", "w") - fh.write(nconf.read()) - fh.close() - else: + try: + f, m = opener.retrieve(update_url, self.tempdir+"config.xml") + except: __logerror("Cannot update config file.") - nconf.close() def __del__(self): From 6d314ea267473b20bf8d75debf033fce62f3a6d9 Mon Sep 17 00:00:00 2001 From: silver spring Date: Sat, 26 Apr 2014 21:50:25 -0400 Subject: [PATCH 3/4] refactoring upload function * removing subprocess wait in favor of communicate * adding a local variable for imagefile * restructuring upload logic to fail before updating the image list --- pythonbits.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/pythonbits.py b/pythonbits.py index 6d6a7d1..0eb810e 100755 --- a/pythonbits.py +++ b/pythonbits.py @@ -645,14 +645,30 @@ def upload(self): stops = range(20,81,60/(self.shots-1)) imgs = [ ] try: - count=0 for stop in stops: - imgs.append(TMPDIR+"screen%d.png" % count) - subprocess.Popen([r"ffmpeg","-ss",str((self.duration * stop)/100), "-vframes", "1", "-i", self.path , "-y", "-sameq", "-f", "image2", imgs[-1] ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).wait() - count+=1 + imgfilename = TMPDIR+"screen%d.png" % len(imgs) + ffmpegproc =subprocess.Popen([r"/usr/bin/ffmpeg", + "-ss", str((self.duration * stop)/100), + "-i", self.path , + "-vframes", "1", + "-y", + "-qscale", "0", + "-f","image2", + imgfilename], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT) + stdoutdata, stderrdata = ffmpegproc.communicate() + if ffmpegproc.returncode != 0: + raise Exception("Ffmpeg call failed, error text:\n" + + str(stdoutdata)) + imgs.append(imgfilename) except OSError: sys.stderr.write("Error: Ffmpeg not installed, refer to http://www.ffmpeg.org/download.html for installation") exit(1) + except Exception as e: + print(str(e)) + exit(1) + opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler) try: From f586cd4663680020f1921b6c33c53c865f75fbd2 Mon Sep 17 00:00:00 2001 From: silver spring Date: Mon, 28 Apr 2014 19:06:58 -0400 Subject: [PATCH 4/4] adding bbimgup helper --- bbimgup.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 bbimgup.py diff --git a/bbimgup.py b/bbimgup.py new file mode 100755 index 0000000..1544eca --- /dev/null +++ b/bbimgup.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python +''' +bbimgup - A quick and dirty images.baconbits.org uploader + +Usage: + + $ bbimgup.py /path/to/image.png + +If everthing has worked the image url will be printed to stdout. + +''' +import json +import sys +import urllib2 +import MultipartPostHandler + + +if __name__ == '__main__': + fn = sys.argv[1] + print('uploading '+fn) + imageurl = None + opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler) + try: + params=({'ImageUp' : open(fn, "rb")}) + socket = opener.open("https://images.baconbits.org/upload.php", params) + json_str = socket.read() + if hasattr(json,'loads'): + read = json.loads( json_str ) + elif hasattr(json,'read'): + read = json.read( json_str ) + else: + err_msg = "I cannot decipher your `json`;\n" + \ + "please report the following output to the bB forum:\n" + \ + ("%s" % dir(json)) + raise Exception( err_msg ) + imageurl = 'https://images.baconbits.org/images/' + read['ImgName'] + except Exception as e: + print(e) + sys.exit(1) + print(imageurl) + sys.exit(0)