-
Notifications
You must be signed in to change notification settings - Fork 1
/
imgur.py
128 lines (111 loc) · 3.43 KB
/
imgur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/env python
__author__ = 'Devon Meunier <devon.meunier@myopicvoid.org>'
__description__ = "A Pythonic interface to the imgur api."
import simplejson
import pycurl
import StringIO
FILE = pycurl.FORM_FILE
class imgurAPIError(Exception):
pass
class imgur:
def __init__(self, apikey=None):
self.apikey = apikey
def upload(self, image):
"""
Upload an image to imgur.
'image' Must be in the form (imgur.FILE, "/path/to/file")
Returns the parsed json that imgur returns.
"""
if self.apikey is None:
raise imgurAPIError, "API Key is missing."
return None
else:
out = StringIO.StringIO()
c = pycurl.Curl()
values = [("key", self.apikey),
("image", (c.FORM_FILE,image)),
]
c.setopt(pycurl.URL, "http://imgur.com/api/upload.json")
c.setopt(pycurl.HTTPPOST, values)
c.setopt(pycurl.WRITEFUNCTION, out.write)
c.perform()
c.close()
return simplejson.loads(out.getvalue())
def upload_from_url(self, image):
"""
Upload an image to imgur.
'image' must be a URL
Returns the parsed json that imgur returns.
"""
if self.apikey is None:
raise imgurAPIError, "API Key is missing."
return None
else:
out = StringIO.StringIO()
c = pycurl.Curl()
values = [("key", self.apikey),
("image", image),
]
c.setopt(pycurl.URL, "http://imgur.com/api/upload.json")
c.setopt(pycurl.HTTPPOST, values)
c.setopt(pycurl.WRITEFUNCTION, out.write)
c.perform()
c.close()
return simplejson.loads(out.getvalue())
def delete(self, dhash):
"""
Delete an image from imgur.
dhash is the delete hash found in the object returned by a call to
imgur.upload
Returns the parsed json that imgur returns.
"""
c = pycurl.Curl()
out = StringIO.StringIO()
c.setopt(pycurl.URL, "http://imgur.com/api/delete/%s.json" % dhash)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.WRITEFUNCTION, out.write)
c.perform()
c.close()
return simplejson.loads(out.getvalue())
def istats(self, ihash):
"""
Returns the image's stats corresponding to its hash, ihash.
"""
out = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://imgur.com/api/stats/%s.json" % ihash)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.WRITEFUNCTION, out.write)
c.perform()
c.close()
return simplejson.loads(out.getvalue())
def stats(self, view="all"):
"""
Returns imgur statistics.
"""
out = StringIO.StringIO()
c = pycurl.Curl()
values = [("view", view)]
c.setopt(pycurl.URL, "http://imgur.com/api/stats.json")
c.setopt(pycurl.HTTPPOST, values)
c.setopt(pycurl.WRITEFUNCTION, out.write)
c.perform()
c.close()
return simplejson.loads(out.getvalue())
def gallery(self, sort="latest", view="all", count=20, page=1):
"""
Returns the stats of several images from the imgur database.
There is no way to specify which images are returned.
"""
out = StringIO.StringIO()
values = [("sort", sort),
("view", view),
("count", count),
("page", page),
]
c.setopt(pycurl.URL, "http://imgur.com/api/gallery.json")
c.setopt(pycurl.HTTPPOST, values)
c.setopt(pycurl.WRITEFUNCTION, out.write)
c.perform()
c.close()
return simplejson.loads(out.getvalue())