-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimgur.py
More file actions
86 lines (72 loc) · 2.85 KB
/
imgur.py
File metadata and controls
86 lines (72 loc) · 2.85 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
#!/usr/bin/env python
#
# imgur.py
# Tools for interacting with imgur.com via their api (http://code.google.com/p/imgur-api/)
#
# Author: Marc Sutton <ric@codev.co.uk>
# Copyright (c) 2009 Codev Ltd
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import os.path
import urllib2
try:
import json
except:
print("Failed to load json, requires Python 2.6 or later")
sys.exit(1)
class ImgurApi:
"""
A class for using the imgur.com api
See http://code.google.com/p/imgur-api
"""
def __init__(self, apikey):
self._apikey = apikey
def upload_image(self, filename):
"""
Upload an image to imgur and return the viewing url.
"""
boundary = '----------boundary'
body = '--' + boundary + '\r\n'
body += 'Content-Disposition: form-data; name="key"\r\n'
body += '\r\n'
body += self._apikey + '\r\n'
body += '--' + boundary + '\r\n'
body += 'Content-Disposition: form-data; name="image"; filename="ric'
body += os.path.splitext(filename)[1]
body += '"\r\n'
body += 'Content-Type: application/octet-stream\r\n'
body += '\r\n'
handle = open(filename, 'r')
body += handle.read() + '\r\n'
handle.close()
body += '--' + boundary + '--\r\n'
body += '\r\n'
content_type = 'multipart/form-data; boundary=%s' % boundary
# TODO: Switch to urllib2
request = urllib2.Request('http://imgur.com/api/upload.json', body, { 'content-type': content_type, 'content-length': str(len(body)) })
http = urllib2.urlopen(request)
contents = http.read()
http.close()
result = json.loads(contents)
if result['rsp']['stat'] != 'ok':
raise RuntimeError('Failed to upload image to imgur.com, error %d: %s' % (result['rsp']['error_code'], result['rsp']['error_msg']))
return result['rsp']['image']['imgur_page']
if __name__ == '__main__':
# Test the class
key = raw_input("Enter your Imgur key:")
imgur = ImgurApi(key)
filename = raw_input("Enter the full path to an image:")
print("Please check this url contains the correct image:")
print(imgur.upload_image(filename))