mediacrush.py
· 3.2 KiB · Python
Raw
# MIT license
# Created by Steven Smith (blha303) 2013
import urllib2, urllib
import json
import base64
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:
# untested
with open(address, "rb") as f:
encoded_file = base64.b64encode(f.read())
params = urllib.urlencode({'file': encoded_file})
request = urllib2.Request(API_URL + "upload/file", params)
request.add_header("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
try:
return json.loads(urllib2.urlopen(request).read())
except urllib2.HTTPError as e:
return json.loads(e.read())
| 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()) |