~ubuntu-branches/debian/sid/nautilus-image-manipulator/sid

« back to all changes in this revision

Viewing changes to .pc/fix-702045/nautilus_image_manipulator/upload/z1fichiercom.py

  • Committer: Package Import Robot
  • Author(s): Emilien Klein
  • Date: 2013-03-02 09:29:45 UTC
  • Revision ID: package-import@ubuntu.com-20130302092945-jjkfw1a3bwouooje
Tags: 1.1-2
* Fix 2 upstream bugs, patches already applied upstream and released in v1.2:
  - debian/patches/fix-702044: Corrupted config file if width/height are
      defined from keyboard (Closes: #702044)
  - debian/patches/fix-702045: Upload to 1fichier.com not working
      (Closes: #702045)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
### BEGIN LICENSE
 
4
# Copyright (C) 2010-2012 Emilien Klein <emilien _AT_ klein _DOT_ st>
 
5
 
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.
 
9
 
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.
 
14
 
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/>.
 
17
### END LICENSE
 
18
 
 
19
import urllib2
 
20
import re
 
21
import logging
 
22
 
 
23
from poster.encode import multipart_encode
 
24
from poster.streaminghttp import register_openers
 
25
 
 
26
class UploadSite():
 
27
    def __init__(self):
 
28
        """Determines the url to upload to for 1fichier.com.
 
29
        
 
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
 
38
 
 
39
    def upload(self, filename, callback):
 
40
        """Uploads a single file and saves the links to download and delete that file.
 
41
        
 
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)
 
46
 
 
47
        # Register the streaming http handlers with urllib2
 
48
        register_openers()
 
49
 
 
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)
 
54
 
 
55
        # There is a bug in poster 0.7.0 that encodes parameter names. It will be fixed in the next release.
 
56
 
 
57
        request = urllib2.Request(self.uploadUrl, datagen, headers)
 
58
        urllib2.urlopen(request)
 
59
 
 
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()
 
64
 
 
65
        # The return is like this:
 
66
        # filename;size;download identifier;deletion identifier;domain identifier;control hash
 
67
 
 
68
        try:
 
69
            (filename, size, download_id, deletion_id, domain_id, control_hash) = re.search("(.*);(.*);(.*);(.*);(.*);(.*)", downloaded_page).groups()
 
70
        except:
 
71
            # TODO: Better failed upload handling
 
72
            logging.error('the upload has failed, this is the returned page:\n"%s"\n' % downloaded_page)
 
73
            raise
 
74
 
 
75
        downloadPage = "http://%s.1fichier.com" % download_id
 
76
        deletePage = "http://www.1fichier.com/remove/%s/%s" % (download_id, deletion_id)
 
77
 
 
78
        return (downloadPage, deletePage)
 
79
 
 
80
if __name__ == "__main__":
 
81
    pass
 
82