Steven Smith revised this gist . Go to revision
1 file changed, 14 insertions, 4 deletions
mediacrush.py
| @@ -1,6 +1,7 @@ | |||
| 1 | 1 | # MIT license | |
| 2 | 2 | # Created by Steven Smith (blha303) 2013 | |
| 3 | 3 | ||
| 4 | + | # r5: Return url if upload(blah, geturl=True) | |
| 4 | 5 | # r4: Turns out I don't need cookielib. | |
| 5 | 6 | # r3: Remove unneeded base64 import | |
| 6 | 7 | # r2: Fixed file upload support, added __name__ == "__main__" section for easy testing or usage from other languages (?) | |
| @@ -9,7 +10,8 @@ | |||
| 9 | 10 | import urllib2, urllib | |
| 10 | 11 | import json | |
| 11 | 12 | ||
| 12 | - | API_URL = "https://mediacru.sh/api/" | |
| 13 | + | BASE_URL = "https://mediacru.sh/" | |
| 14 | + | API_URL = BASE_URL + "api/" | |
| 13 | 15 | ||
| 14 | 16 | ||
| 15 | 17 | def info(hash): | |
| @@ -70,7 +72,7 @@ def status(hash): | |||
| 70 | 72 | except urllib2.HTTPError as e: | |
| 71 | 73 | return json.loads(e.read()) | |
| 72 | 74 | ||
| 73 | - | def upload(address, url=True): | |
| 75 | + | def upload(address, url=True, geturl=False): | |
| 74 | 76 | """ | |
| 75 | 77 | Returns dict: | |
| 76 | 78 | Either | |
| @@ -84,14 +86,22 @@ def upload(address, url=True): | |||
| 84 | 86 | """ | |
| 85 | 87 | if url: | |
| 86 | 88 | try: | |
| 87 | - | return json.loads(urllib2.urlopen(API_URL + "upload/url", urllib.urlencode({'url': address})).read()) | |
| 89 | + | data = json.loads(urllib2.urlopen(API_URL + "upload/url", urllib.urlencode({'url': address})).read()) | |
| 90 | + | if geturl: | |
| 91 | + | return BASE_URL + data["hash"] | |
| 92 | + | else: | |
| 93 | + | return data | |
| 88 | 94 | except urllib2.HTTPError as e: | |
| 89 | 95 | return json.loads(e.read()) | |
| 90 | 96 | else: | |
| 91 | 97 | import MultipartPostHandler | |
| 92 | 98 | opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler) | |
| 93 | 99 | try: | |
| 94 | - | return json.loads(opener.open(API_URL + "upload/file", {'file': open(address, "rb")}).read()) | |
| 100 | + | data = json.loads(opener.open(API_URL + "upload/file", {'file': open(address, "rb")}).read()) | |
| 101 | + | if geturl: | |
| 102 | + | return BASE_URL + data["hash"] | |
| 103 | + | else: | |
| 104 | + | return data | |
| 95 | 105 | except urllib2.HTTPError as e: | |
| 96 | 106 | return json.loads(e.read()) | |
| 97 | 107 | ||
Steven Smith revised this gist . Go to revision
1 file changed, 3 insertions, 4 deletions
mediacrush.py
| @@ -1,6 +1,7 @@ | |||
| 1 | 1 | # MIT license | |
| 2 | 2 | # Created by Steven Smith (blha303) 2013 | |
| 3 | 3 | ||
| 4 | + | # r4: Turns out I don't need cookielib. | |
| 4 | 5 | # r3: Remove unneeded base64 import | |
| 5 | 6 | # r2: Fixed file upload support, added __name__ == "__main__" section for easy testing or usage from other languages (?) | |
| 6 | 7 | # r1: Initial. all functionality in place | |
| @@ -87,10 +88,8 @@ def upload(address, url=True): | |||
| 87 | 88 | except urllib2.HTTPError as e: | |
| 88 | 89 | return json.loads(e.read()) | |
| 89 | 90 | else: | |
| 90 | - | import MultipartPostHandler, cookielib | |
| 91 | - | cookies = cookielib.CookieJar() | |
| 92 | - | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), | |
| 93 | - | MultipartPostHandler.MultipartPostHandler) | |
| 91 | + | import MultipartPostHandler | |
| 92 | + | opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler) | |
| 94 | 93 | try: | |
| 95 | 94 | return json.loads(opener.open(API_URL + "upload/file", {'file': open(address, "rb")}).read()) | |
| 96 | 95 | except urllib2.HTTPError as e: | |
Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
mediacrush.py
| @@ -1,12 +1,12 @@ | |||
| 1 | 1 | # MIT license | |
| 2 | 2 | # Created by Steven Smith (blha303) 2013 | |
| 3 | 3 | ||
| 4 | + | # r3: Remove unneeded base64 import | |
| 4 | 5 | # r2: Fixed file upload support, added __name__ == "__main__" section for easy testing or usage from other languages (?) | |
| 5 | 6 | # r1: Initial. all functionality in place | |
| 6 | 7 | ||
| 7 | 8 | import urllib2, urllib | |
| 8 | 9 | import json | |
| 9 | - | import base64 | |
| 10 | 10 | ||
| 11 | 11 | API_URL = "https://mediacru.sh/api/" | |
| 12 | 12 | ||
Steven Smith revised this gist . Go to revision
1 file changed, 39 insertions, 9 deletions
mediacrush.py
| @@ -1,6 +1,9 @@ | |||
| 1 | 1 | # MIT license | |
| 2 | 2 | # Created by Steven Smith (blha303) 2013 | |
| 3 | 3 | ||
| 4 | + | # r2: Fixed file upload support, added __name__ == "__main__" section for easy testing or usage from other languages (?) | |
| 5 | + | # r1: Initial. all functionality in place | |
| 6 | + | ||
| 4 | 7 | import urllib2, urllib | |
| 5 | 8 | import json | |
| 6 | 9 | import base64 | |
| @@ -39,7 +42,7 @@ def delete(hash): | |||
| 39 | 42 | Either | |
| 40 | 43 | * status: string, always "success", meaning: The IP matches the stored hash and the file was deleted. | |
| 41 | 44 | or | |
| 42 | - | * error: integer, error code. | |
| 45 | + | * error: integer, error code. | |
| 43 | 46 | 401 = The IP does not match the stored hash. | |
| 44 | 47 | 404 = There is no file with that hash. | |
| 45 | 48 | """ | |
| @@ -84,13 +87,40 @@ def upload(address, url=True): | |||
| 84 | 87 | except urllib2.HTTPError as e: | |
| 85 | 88 | return json.loads(e.read()) | |
| 86 | 89 | else: | |
| 87 | - | # untested | |
| 88 | - | with open(address, "rb") as f: | |
| 89 | - | encoded_file = base64.b64encode(f.read()) | |
| 90 | - | params = urllib.urlencode({'file': encoded_file}) | |
| 91 | - | request = urllib2.Request(API_URL + "upload/file", params) | |
| 92 | - | request.add_header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8") | |
| 90 | + | import MultipartPostHandler, cookielib | |
| 91 | + | cookies = cookielib.CookieJar() | |
| 92 | + | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), | |
| 93 | + | MultipartPostHandler.MultipartPostHandler) | |
| 93 | 94 | try: | |
| 94 | - | return json.loads(urllib2.urlopen(request).read()) | |
| 95 | + | return json.loads(opener.open(API_URL + "upload/file", {'file': open(address, "rb")}).read()) | |
| 95 | 96 | except urllib2.HTTPError as e: | |
| 96 | - | return json.loads(e.read()) | |
| 97 | + | return json.loads(e.read()) | |
| 98 | + | ||
| 99 | + | if __name__ == "__main__": | |
| 100 | + | from sys import argv | |
| 101 | + | if len(argv) > 2: | |
| 102 | + | if argv[1] == "uploadf" or argv[1] == "upload": | |
| 103 | + | print upload(argv[2], url=False) | |
| 104 | + | elif argv[1] == "uploadu" or argv[1] == "url": | |
| 105 | + | print upload(argv[2]) | |
| 106 | + | elif argv[1] == "info": | |
| 107 | + | print info(argv[2]) | |
| 108 | + | elif argv[1] == "infol": | |
| 109 | + | print info_list(argv[2].split(",")) | |
| 110 | + | elif argv[1] == "exists": | |
| 111 | + | print exists(argv[2]) | |
| 112 | + | elif argv[1] == "delete": | |
| 113 | + | print delete(argv[2]) | |
| 114 | + | elif argv[1] == "status": | |
| 115 | + | print status(argv[2]) | |
| 116 | + | else: | |
| 117 | + | print "Unsupported function." | |
| 118 | + | else: | |
| 119 | + | print "Usage: %s <function> <value>" % argv[0] | |
| 120 | + | print "Functions:" | |
| 121 | + | print "upload: filename uploadf: filename uploadu: url" | |
| 122 | + | print "url: url info: hash infol: comma-separated hash list" | |
| 123 | + | print "exists: hash delete: hash status: hash" | |
| 124 | + | print "by Steven Smith (blha303) 2013" | |
| 125 | + | print "MIT license" | |
| 126 | + | print "Support: https://gist.github.com/blha303/6239248 or mcrush@blha303.com.au" | |
Steven Smith revised this gist . Go to revision
1 file changed, 96 insertions
mediacrush.py(file created)
| @@ -0,0 +1,96 @@ | |||
| 1 | + | # MIT license | |
| 2 | + | # Created by Steven Smith (blha303) 2013 | |
| 3 | + | ||
| 4 | + | import urllib2, urllib | |
| 5 | + | import json | |
| 6 | + | import base64 | |
| 7 | + | ||
| 8 | + | API_URL = "https://mediacru.sh/api/" | |
| 9 | + | ||
| 10 | + | ||
| 11 | + | def info(hash): | |
| 12 | + | """ | |
| 13 | + | Returns dict: | |
| 14 | + | * compression: float representing amount of compression achieved | |
| 15 | + | * files: list containing dicts: | |
| 16 | + | * file: string, url of file | |
| 17 | + | * type: string, mime type of file. can be "video/mp4", "video/ogg", "image/gif" | |
| 18 | + | * original: string, url of original file | |
| 19 | + | * type: string, mime type of original file | |
| 20 | + | """ | |
| 21 | + | return json.loads(urllib2.urlopen(API_URL + hash).read()) | |
| 22 | + | ||
| 23 | + | def info_list(hashlist): | |
| 24 | + | """ | |
| 25 | + | Returns dict: | |
| 26 | + | * <hash>: dict of info, or None if hash isn't valid. see info() docs | |
| 27 | + | """ | |
| 28 | + | return json.loads(urllib2.urlopen(API_URL + "info?list=" + ",".join(hashlist)).read()) | |
| 29 | + | ||
| 30 | + | def exists(hash): | |
| 31 | + | """ | |
| 32 | + | Returns boolean | |
| 33 | + | """ | |
| 34 | + | return json.loads(urllib2.urlopen(API_URL + hash + "/exists").read())["exists"] | |
| 35 | + | ||
| 36 | + | def delete(hash): | |
| 37 | + | """ | |
| 38 | + | Returns dict: | |
| 39 | + | Either | |
| 40 | + | * status: string, always "success", meaning: The IP matches the stored hash and the file was deleted. | |
| 41 | + | or | |
| 42 | + | * error: integer, error code. | |
| 43 | + | 401 = The IP does not match the stored hash. | |
| 44 | + | 404 = There is no file with that hash. | |
| 45 | + | """ | |
| 46 | + | try: | |
| 47 | + | return json.loads(urllib2.urlopen(API_URL + hash + "/delete").read())["status"] | |
| 48 | + | except urllib2.HTTPError as e: | |
| 49 | + | return json.loads(e.read()) | |
| 50 | + | ||
| 51 | + | def status(hash): | |
| 52 | + | """ | |
| 53 | + | Returns dict: | |
| 54 | + | Either | |
| 55 | + | * status: string, one of four values: | |
| 56 | + | "done": The file has been processed. | |
| 57 | + | "processing": The file is being processed or in the processing queue. | |
| 58 | + | "error": The processing step finished early with an abnormal return code. | |
| 59 | + | "timeout": The file took too long to process. | |
| 60 | + | or | |
| 61 | + | * error: integer, error code. | |
| 62 | + | 404 = There is no file with that hash. | |
| 63 | + | """ | |
| 64 | + | try: | |
| 65 | + | return json.loads(urllib2.urlopen(API_URL + hash + "/status").read()) | |
| 66 | + | except urllib2.HTTPError as e: | |
| 67 | + | return json.loads(e.read()) | |
| 68 | + | ||
| 69 | + | def upload(address, url=True): | |
| 70 | + | """ | |
| 71 | + | Returns dict: | |
| 72 | + | Either | |
| 73 | + | * hash: string, resulting image hash | |
| 74 | + | or | |
| 75 | + | * error: integer, error code | |
| 76 | + | 409 = The file was already uploaded. | |
| 77 | + | 420 = The rate limit was exceeded. Enhance your calm. | |
| 78 | + | 415 = The file extension is not acceptable. | |
| 79 | + | * hash: string, resulting image hash, if error code is 409 | |
| 80 | + | """ | |
| 81 | + | if url: | |
| 82 | + | try: | |
| 83 | + | return json.loads(urllib2.urlopen(API_URL + "upload/url", urllib.urlencode({'url': address})).read()) | |
| 84 | + | except urllib2.HTTPError as e: | |
| 85 | + | return json.loads(e.read()) | |
| 86 | + | else: | |
| 87 | + | # untested | |
| 88 | + | with open(address, "rb") as f: | |
| 89 | + | encoded_file = base64.b64encode(f.read()) | |
| 90 | + | params = urllib.urlencode({'file': encoded_file}) | |
| 91 | + | request = urllib2.Request(API_URL + "upload/file", params) | |
| 92 | + | request.add_header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8") | |
| 93 | + | try: | |
| 94 | + | return json.loads(urllib2.urlopen(request).read()) | |
| 95 | + | except urllib2.HTTPError as e: | |
| 96 | + | return json.loads(e.read()) | |