2
# -*- coding: utf-8 -*-
4
# Copyright (C) 2010-2012 Emilien Klein <emilien _AT_ klein _DOT_ st>
6
# This program is free software: you can redistribute it and/or modify it
7
# under the terms of the GNU General Public License version 3, as published
8
# by the Free Software Foundation.
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranties of
12
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13
# PURPOSE. See the GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License along
16
# with this program. If not, see <http://www.gnu.org/licenses/>.
23
from poster.encode import multipart_encode
24
from poster.streaminghttp import register_openers
28
"""Determines the url to upload to for 1fichier.com.
30
Documentation for 1fichier.com: http://www.1fichier.com/api/web.html
31
Note: it's not up to date..."""
32
# The session ID is read from the "files" form on http://www.1fichier.com
33
html = urllib2.urlopen('http://www.1fichier.com').read()
34
(sessionId) = re.search('<form enctype="multipart/form-data" id="files" action="http://upload\.1fichier\.com/upload.cgi\?id=(.*)" method="post">', html).groups()
35
# Build the url to upload to and to retrieve the download and delete links:
36
self.uploadUrl = "http://upload.1fichier.com/upload.cgi?id=%s" % sessionId
37
self.endUploadUrl = "http://upload.1fichier.com/end.pl?xid=%s" % sessionId
39
def upload(self, filename, callback):
40
"""Uploads a single file and saves the links to download and delete that file.
42
``filename`` is the file to upload
43
``callback`` is the function that updates the progress bar while uploading"""
44
logging.debug('uploading %s to %s' % (filename, self.uploadUrl))
45
logging.debug('end upload url: %s' % self.endUploadUrl)
47
# Register the streaming http handlers with urllib2
50
# Start the multipart/form-data encoding of the file "filename"
51
# headers contains the necessary Content-Type and Content-Length
52
# datagen is a generator object that yields the encoded parameters
53
datagen, headers = multipart_encode([ ("file[]", open(filename, "rb")), ('domain', '0') ], cb=callback)
55
# There is a bug in poster 0.7.0 that encodes parameter names. It will be fixed in the next release.
57
request = urllib2.Request(self.uploadUrl, datagen, headers)
58
urllib2.urlopen(request)
60
# Retrieve the download page
61
request = urllib2.Request(self.endUploadUrl)
62
request.add_header("EXPORT", 1) # To get simplified values in the format explained below
63
downloaded_page = urllib2.urlopen(request).read()
65
# The return is like this:
66
# filename;size;download identifier;deletion identifier;domain identifier;control hash
69
(filename, size, download_id, deletion_id, domain_id, control_hash) = re.search("(.*);(.*);(.*);(.*);(.*);(.*)", downloaded_page).groups()
71
# TODO: Better failed upload handling
72
logging.error('the upload has failed, this is the returned page:\n"%s"\n' % downloaded_page)
75
downloadPage = "http://%s.1fichier.com" % download_id
76
deletePage = "http://www.1fichier.com/remove/%s/%s" % (download_id, deletion_id)
78
return (downloadPage, deletePage)
80
if __name__ == "__main__":