Last active 1450630756

mediacru.sh API Python wrapper

Revision 421be0322ec17f9d8b335371d6fbfcb00f874283

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