Last active 1450630756

mediacru.sh API Python wrapper

mediacrush.py Raw
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
10import urllib2, urllib
11import json
12
13BASE_URL = "https://mediacru.sh/"
14API_URL = BASE_URL + "api/"
15
16
17def 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
29def 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
36def exists(hash):
37 """
38 Returns boolean
39 """
40 return json.loads(urllib2.urlopen(API_URL + hash + "/exists").read())["exists"]
41
42def 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
57def 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
75def 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
108if __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