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