From e93cc8ea3c0e37553fa5c358fcfe25c03f1da86a Mon Sep 17 00:00:00 2001 From: Aylon Armstrong Date: Sat, 8 Jul 2017 04:04:17 -0500 Subject: [PATCH 1/5] Enable specification of a folder --- sinusbot_uploader.py | 346 +++++++++++++++++++++++-------------------- 1 file changed, 182 insertions(+), 164 deletions(-) diff --git a/sinusbot_uploader.py b/sinusbot_uploader.py index b60a9c8..f197377 100644 --- a/sinusbot_uploader.py +++ b/sinusbot_uploader.py @@ -6,172 +6,190 @@ import httplib import os.path + class Sinusbot: - jwt_token = '' #Auth Token - botId = '' #Bot ID required for the Login - url = '' #Base URL - port = 8087 #default Sinusbot Port - ssl = False # Disable SSL by default - username = '' - password = '' - success_count = 0 - error_count = 0 - extensions=['mp3', 'mp4', 'wav', '3gp'] - - def __init__(self, url, port, username, password, ssl): - self.url = url - self.port = port - self.ssl = ssl - self.username = username - self.password = password - self.botId = self.DefaultId() - self.success_count = 0 - self.error_count = 0 - - - def DefaultId(self): - - if self.ssl: - conn = httplib.HTTPSConnection(self.url, self.port) - - else: - conn = httplib.HTTPConnection(self.url, self.port) - - conn.request("GET", "/api/v1/botId") - response = conn.getresponse() - - if response.status == 200: - j = json.loads(response.read()) - conn.close() - return j['defaultBotId'] - - - else: - conn.close() - return '' - - - - def Auth(self): - - if self.ssl: - conn = httplib.HTTPSConnection(self.url, self.port) - - else: - conn = httplib.HTTPConnection(self.url, self.port) - - data = {"username":self.username, "password":self.password, "botId": str(self.botId)} #Parse the botId with str() to pre event a golang error in case of unicode - hdr = {"Content-type": "application/json"} - - conn.request("POST", "/api/v1/bot/login", json.dumps(data), hdr) - response = conn.getresponse() - - if response.status == 200: - response = str(response.read()) - j = json.loads(response) - - try: - self.jwt_token = j['token'] - conn.close() - self.username = '' - self.password = '' - return True - - - except: - conn.close() - print 'Could not get token: %s' % (response) - return False - - - else: - conn.close() - return False - - def Upload(self, LocalPath): - - if not os.path.isfile(LocalPath): - return False - - - try: - f = open(LocalPath, 'r') - bytes = f.read() - f.close() - except: - print 'Could not read -> ' + LocalPath - return False - - if self.ssl: - conn = httplib.HTTPSConnection(self.url, self.port) - - else: - conn = httplib.HTTPConnection(self.url, self.port) - - hdr = {"Content-type": "application/json", "Authorization": "bearer " + str(self.jwt_token), "Content-Length": len(bytes)} - - conn.request("POST", "/api/v1/bot/upload", bytes, hdr) - response = conn.getresponse() - - - if response.status == 200: - conn.close() - self.success_count += 1 - return True - else: - conn.close() - self.error_count += 1 - return False - + jwt_token = '' # Auth Token + botId = '' # Bot ID required for the Login + url = '' # Base URL + port = 8087 # default Sinusbot Port + ssl = False # Disable SSL by default + username = '' + password = '' + success_count = 0 + error_count = 0 + extensions = ['mp3', 'mp4', 'wav', '3gp'] + + def __init__(self, url, port, username, password, folder, ssl): + self.url = url + self.port = port + self.ssl = ssl + self.folder = folder + self.username = username + self.password = password + self.botId = self.DefaultId() + self.success_count = 0 + self.error_count = 0 + + def DefaultId(self): + + if self.ssl: + conn = httplib.HTTPSConnection(self.url, self.port) + + else: + conn = httplib.HTTPConnection(self.url, self.port) + + conn.request("GET", "/api/v1/botId") + response = conn.getresponse() + + if response.status == 200: + j = json.loads(response.read()) + conn.close() + return j['defaultBotId'] + + + else: + conn.close() + return '' + + def Auth(self): + + if self.ssl: + conn = httplib.HTTPSConnection(self.url, self.port) + + else: + conn = httplib.HTTPConnection(self.url, self.port) + + data = {"username": self.username, "password": self.password, + "botId": str(self.botId)} # Parse the botId with str() to pre event a golang error in case of unicode + hdr = {"Content-type": "application/json"} + + conn.request("POST", "/api/v1/bot/login", json.dumps(data), hdr) + response = conn.getresponse() + + if response.status == 200: + response = str(response.read()) + j = json.loads(response) + + try: + self.jwt_token = j['token'] + conn.close() + self.username = '' + self.password = '' + return True + + + except: + conn.close() + print + 'Could not get token: %s' % (response) + return False + + + else: + conn.close() + return False + + def Upload(self, LocalPath): + + if not os.path.isfile(LocalPath): + return False + + try: + f = open(LocalPath, 'r') + bytes = f.read() + f.close() + except: + print + 'Could not read -> ' + LocalPath + return False + if self.ssl: + conn = httplib.HTTPSConnection(self.url, self.port) + + else: + conn = httplib.HTTPConnection(self.url, self.port) + + hdr = {"Content-type": "application/json", "Authorization": "bearer " + str(self.jwt_token), + "Content-Length": len(bytes)} + url = "/api/v1/bot/upload" + if self.folder is not False: + url = url + "?folder=" + self.folder + conn.request("POST", url, bytes, hdr) + response = conn.getresponse() + + if response.status == 200: + conn.close() + self.success_count += 1 + return True + else: + conn.close() + self.error_count += 1 + return False + def uploadHelper(directory, bot, recurse): - truePath = os.path.abspath(directory) - contents = os.listdir(truePath) - - for entry in contents: - entryTruePath = os.path.join(truePath, entry) - if os.path.isfile(entryTruePath): - fileExtension = entryTruePath.split('.')[-1] - if (fileExtension in bot.extensions): - if bot.Upload(entryTruePath): - print 'Success uploaded: ' + entryTruePath - else: - print 'Error while uploading: ' + entryTruePath - else: - print 'File type not supported: ' + entryTruePath - elif os.path.isdir(entryTruePath) and recurse: - uploadHelper(entryTruePath, bot, recurse) - - + truePath = os.path.abspath(directory) + contents = os.listdir(truePath) + + for entry in contents: + entryTruePath = os.path.join(truePath, entry) + if os.path.isfile(entryTruePath): + fileExtension = entryTruePath.split('.')[-1] + if (fileExtension in bot.extensions): + if bot.Upload(entryTruePath): + print + 'Success uploaded: ' + entryTruePath + else: + print + 'Error while uploading: ' + entryTruePath + else: + print + 'File type not supported: ' + entryTruePath + elif os.path.isdir(entryTruePath) and recurse: + uploadHelper(entryTruePath, bot, recurse) + + if len(sys.argv) < 6: - print 'Usage: ./sinusbot_uploader.py 123.124.125.1 port username password LOCAL_DIRECTORY SSL(optional) -R (Recursive)' - sys.exit(1) + print + 'Usage: ./sinusbot_uploader.py 123.124.125.1 port username password LOCAL_DIRECTORY SSL(optional) -R (Recursive)' + sys.exit(1) else: - recursive = False - if len(sys.argv) >= 7: - if sys.argv[6] == 'SSL': - bot = Sinusbot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], True) - if sys.argv[6] == '-R' or sys.argv[7] == '-R': - recursive = True - else: - bot = Sinusbot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], False) - - - if not os.path.isdir(sys.argv[5]): - print 'LOCAL_DIRECTORY must be a valid directory!' - sys.exit(1) - - bot = Sinusbot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], False) - if bot.Auth(): - print 'Success Authenticated!' - - dir = os.path.abspath(sys.argv[5]) - files = os.listdir(dir) - - uploadHelper(dir, bot, recursive) - - print 'Completed -> Uploaded %d files with %d errors.' % (bot.success_count, bot.error_count) - - else: - print 'Error on Authentication!' - sys.exit(1) + recursive = False + folder = False + SSL = False + if len(sys.argv) >= 7: + if sys.argv[6] != '-R' and sys.argv[6] != 'SSL' and sys.argv[6] is not None: + folder = sys.argv[6] + if len(sys.argv) >= 8: + if sys.argv[6] == 'SSL' or sys.argv[7] == 'SSL': + SSL = True + if sys.argv[6] == '-R' or sys.argv[7] == '-R': + recursive = True + else: + bot = Sinusbot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], False, False) + if len(sys.argv) == 9: + if sys.argv[8] == 'SSL': + SSL = True + if sys.argv[8] == '-R': + recursive = True + if not os.path.isdir(sys.argv[5]): + print + 'LOCAL_DIRECTORY must be a valid directory!' + sys.exit(1) + + bot = Sinusbot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], folder, SSL) + if bot.Auth(): + print + 'Success Authenticated!' + + dir = os.path.abspath(sys.argv[5]) + files = os.listdir(dir) + + uploadHelper(dir, bot, recursive) + + print + 'Completed -> Uploaded %d files with %d errors.' % (bot.success_count, bot.error_count) + + else: + print + 'Error on Authentication!' + sys.exit(1) From 12c3ffdbfd974141d78be47d65604912c8173e68 Mon Sep 17 00:00:00 2001 From: Aylon Armstrong Date: Sat, 8 Jul 2017 04:10:52 -0500 Subject: [PATCH 2/5] Update Readme.md with examples of folder specification --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 7a9cfdc..134aeb5 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,18 @@ Success uploaded: /home/xuxe/test/subdir/Glowworm+-+Periphescence.mp3
Success uploaded: /home/xuxe/test/subdir/Death+Grips+-+Come+Up+and+Get+Me.mp3
Completed -> Uploaded 3 files with 0 errors.
+# Specify Folder: +You need the folder UUID to specify a folder. +To get this, when viewing a folder i.e. http://127.0.0.1:8087/play/list/6167f60e-acd0-4e84-b5c0-d2e102d951c3, the 6167f60e-acd0-4e84-b5c0-d2e102d951c3 on the end would be the UUID +Examples: +- **Basic ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foober /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 +- **Recursive ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 -R +- **Recurisve + SSL ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 -R SSL +- **SSL ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 SSL +- **SSL + Recursive ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 SSL -R + +**You can still use the original way specified in the Usage** + SUPPORTED FILE TYPES: extensions=['mp3', 'mp4', 'wav', '3gp'] - i have added at the moment only mp3, mp4, wav and 3gp. If you need others you can add your own or report it and i will add it! From f7f2f49aa91cc43fb64999ead1cb7134a96d8757 Mon Sep 17 00:00:00 2001 From: Aylon Armstrong Date: Sat, 8 Jul 2017 04:12:13 -0500 Subject: [PATCH 3/5] Forgot to allow original commands too --- sinusbot_uploader.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sinusbot_uploader.py b/sinusbot_uploader.py index f197377..45668bc 100644 --- a/sinusbot_uploader.py +++ b/sinusbot_uploader.py @@ -157,6 +157,10 @@ def uploadHelper(directory, bot, recurse): folder = False SSL = False if len(sys.argv) >= 7: + if sys.argv[6] == 'SSL': + SSL = True + if sys.argv[6] == '-R': + recursive = True if sys.argv[6] != '-R' and sys.argv[6] != 'SSL' and sys.argv[6] is not None: folder = sys.argv[6] if len(sys.argv) >= 8: @@ -193,3 +197,4 @@ def uploadHelper(directory, bot, recurse): print 'Error on Authentication!' sys.exit(1) + From 04f97135b223e7fb9929750fc7968de206a477e3 Mon Sep 17 00:00:00 2001 From: Aylon Armstrong Date: Sat, 8 Jul 2017 04:19:57 -0500 Subject: [PATCH 4/5] Use print as a function I had issues with printing not working how it was, switching to this type of printing worked for me, might just be a me thing --- sinusbot_uploader.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/sinusbot_uploader.py b/sinusbot_uploader.py index 45668bc..ed8eaca 100644 --- a/sinusbot_uploader.py +++ b/sinusbot_uploader.py @@ -80,8 +80,7 @@ def Auth(self): except: conn.close() - print - 'Could not get token: %s' % (response) + print('Could not get token: %s' % (response)) return False @@ -99,8 +98,7 @@ def Upload(self, LocalPath): bytes = f.read() f.close() except: - print - 'Could not read -> ' + LocalPath + print('Could not read -> ' + LocalPath) return False if self.ssl: conn = httplib.HTTPSConnection(self.url, self.port) @@ -136,21 +134,17 @@ def uploadHelper(directory, bot, recurse): fileExtension = entryTruePath.split('.')[-1] if (fileExtension in bot.extensions): if bot.Upload(entryTruePath): - print - 'Success uploaded: ' + entryTruePath + print('Success uploaded: ' + entryTruePath) else: - print - 'Error while uploading: ' + entryTruePath + print('Error while uploading: ' + entryTruePath) else: - print - 'File type not supported: ' + entryTruePath + print('File type not supported: ' + entryTruePath) elif os.path.isdir(entryTruePath) and recurse: uploadHelper(entryTruePath, bot, recurse) if len(sys.argv) < 6: - print - 'Usage: ./sinusbot_uploader.py 123.124.125.1 port username password LOCAL_DIRECTORY SSL(optional) -R (Recursive)' + print('Usage: ./sinusbot_uploader.py 123.124.125.1 port username password LOCAL_DIRECTORY SSL(optional) -R (Recursive)') sys.exit(1) else: recursive = False @@ -176,25 +170,20 @@ def uploadHelper(directory, bot, recurse): if sys.argv[8] == '-R': recursive = True if not os.path.isdir(sys.argv[5]): - print - 'LOCAL_DIRECTORY must be a valid directory!' + print('LOCAL_DIRECTORY must be a valid directory!') sys.exit(1) bot = Sinusbot(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], folder, SSL) if bot.Auth(): - print - 'Success Authenticated!' + print('Success Authenticated!') dir = os.path.abspath(sys.argv[5]) files = os.listdir(dir) uploadHelper(dir, bot, recursive) - print - 'Completed -> Uploaded %d files with %d errors.' % (bot.success_count, bot.error_count) + print('Completed -> Uploaded %d files with %d errors.' % (bot.success_count, bot.error_count)) else: - print - 'Error on Authentication!' + print('Error on Authentication!') sys.exit(1) - From ae9a9a7187be9b1c195ebe66cf3a5ffe684d7694 Mon Sep 17 00:00:00 2001 From: Aylon Armstrong Date: Sat, 8 Jul 2017 20:21:20 -0500 Subject: [PATCH 5/5] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 134aeb5..f9fcbb1 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ To get this, when viewing a folder i.e. http://127.0.0.1:8087/play/list/6167f60e Examples: - **Basic ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foober /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 - **Recursive ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 -R -- **Recurisve + SSL ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 -R SSL +- **Recursive + SSL ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 -R SSL - **SSL ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 SSL - **SSL + Recursive ->** ./sinusbot_uploader.py 127.0.0.1 8087 admin foobar /myfolder 6167f60e-acd0-4e84-b5c0-d2e102d951c3 SSL -R