Last active 1450630756

mediacru.sh API Python wrapper

Revision 563feab602deb17cf54b540106a77c68d7d538d2

mediacrush.py Raw
1# MIT license
2# Created by Steven Smith (blha303) 2013
3
4import urllib2, urllib
5import json
6import base64
7
8API_URL = "https://mediacru.sh/api/"
9
10
11def 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
23def 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
30def exists(hash):
31 """
32 Returns boolean
33 """
34 return json.loads(urllib2.urlopen(API_URL + hash + "/exists").read())["exists"]
35
36def 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
51def 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
69def 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())