#!/usr/bin/env python """ Script for upload an image to http://elitepicturehost.com/ """ __version__ = '0.1-1' __author__ = 'Elite Picture Host team' __url__ = 'http://elitepicturehost.com/' __copyright__ = 'Copyright (C) 2007 - Elite Picture Host team' __license__ = """ 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ # PyLint disabled message: W0312,C0103 UPLOAD_FILE_URL = __url__ + 'upload.php' #USER_AGENT = os.path.basename(sys.argv[0]) + " " + __version__ USER_AGENT = "EPH-console" import httplib, mimetools, mimetypes, os, sys, urlparse def postMultipartUrl(url, fields, files): urlparts = urlparse.urlsplit(url) content_type, body = encodeMultipartFormdata(fields, files) h = httplib.HTTPConnection(urlparts[1]) headers = { 'User-Agent': USER_AGENT, 'Content-Type': content_type } h.request('POST', urlparts[2], body, headers) return h.getresponse() def encodeMultipartFormdata(fields, files): BOUNDARY = mimetools.choose_boundary() CRLF = '\r\n' buffer = [] for (key, value) in fields: buffer.append('--' + BOUNDARY) buffer.append('Content-Disposition: form-data; name="%s"' % key) buffer.append('') buffer.append(value) for (key, filename, value) in files: buffer.append('--' + BOUNDARY) buffer.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename)) buffer.append('Content-Type: %s' % getContentType(filename)) buffer.append('') buffer.append(value) buffer.append('--' + BOUNDARY + '--') buffer.append('') body = CRLF.join(buffer) content_type = 'multipart/form-data; boundary=%s' % BOUNDARY return content_type, body def getContentType(filename): return mimetypes.guess_type(filename)[0] or 'application/octet-stream' def usage(): print os.path.basename(sys.argv[0]), "-", __doc__ print __copyright__ print 'Usage:', os.path.basename(sys.argv[0]), '[FILE] [OPTIONS]' print print 'Options:' print ' -n\tonly displays the direct link' def main(): if (len(sys.argv) < 2) or (sys.argv[1] == '-h') or (sys.argv[1] == '--help'): usage() sys.exit(1) # Check for the file if not os.path.exists(sys.argv[len(sys.argv) - 1]): print sys.argv[len(sys.argv) - 1] + ': File not found.' sys.exit(1) response = postMultipartUrl(UPLOAD_FILE_URL, {}, [('upfile', sys.argv[len(sys.argv) - 1], open(sys.argv[len(sys.argv) - 1]).read())]) if '-n' in sys.argv: print response.read().split('\n')[4].replace('Direct link: ', '') else: print response.read() if __name__ == '__main__': main() sys.exit(0)