forked from willasaywhat/pyimgur
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgur.py
More file actions
90 lines (78 loc) · 2.84 KB
/
imgur.py
File metadata and controls
90 lines (78 loc) · 2.84 KB
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
#!/bin/env python
__author__ = 'Devon Meunier <devon.meunier@myopicvoid.org>'
__description__ = "A Pythonic interface to the imgur api."
__version__ = '0.7.5'
import urllib
try:
import simplejson as json
except ImportError:
import json
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 a file-like python object
Returns the parsed json that imgur returns.
"""
if self.apikey is None:
raise imgurAPIError, "API Key is missing."
return None
else:
postdata = {"key": self.apikey,
"image": base64.b64encode(image)}
data = urllib.urlencode(postdata)
response = urllib.urlopen("http://imgur.com/api/upload.json", data)
return json.loads(response.read())
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:
postdata = {"key": self.apikey,
"image": image}
data = urllib.urlencode(postdata)
response = urllib.urlopen("http://imgur.com/api/upload.json", data)
return json.loads(response.read())
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.
"""
response = urllib.urlopen("http://imgur.com/api/delete/%s.json" % dhash)
return json.loads(response.read())
def istats(self, ihash):
"""
Returns the image's stats corresponding to its hash, ihash.
"""
response = urllib.urlopen("http://imgur.com/api/stats/%s.json" % ihash)
return json.loads(response.read())
def stats(self, view="all"):
"""
Returns imgur statistics.
"""
data = urllib.urlencode({"view": view})
response = urllib.urlopen("http://imgur.com/api/stats.json", data)
return json.loads(response.read())
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.
"""
postdata = {"sort": sort,
"view": view,
"count": count,
"page": page}
data = urllib.urlencode(postdata)
response = urllib.urlopen("http://imgur.com/api/stats.json", data)
return json.loads(response.read())