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