Last active 1450630756

mediacru.sh API Python wrapper

Revision 74909c2f2ebe54f8980f26421b84893bfc5ace3d

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