From 17b8ba27b3f316a3f0b23e0edc576b334eeda7af Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Fri, 27 Nov 2009 23:46:40 +0000 Subject: [PATCH] - FEATURE: Search engine plugins now handle HTTP protocol gzip compression --- Changelog | 1 + src/search_engine/helpers.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Changelog b/Changelog index a7e5a20ee..5ec7cca09 100644 --- a/Changelog +++ b/Changelog @@ -21,6 +21,7 @@ - FEATURE: Dropped Qt 4.3 support (Qt >= 4.4 is now required) - FEATURE: Display close tab button into the tabs in search engine (Qt >= 4.5 only) - FEATURE: Show official documentation when pressing F1 key + - FEATURE: Search engine plugins now handle HTTP protocol gzip compression - FEATURE: Announce to all trackers specified for a torrent (µTorrent behavior) (libtorrent >= v0.15 only) - FEATURE: Added per-torrent super seeding mode (libtorrent >= v0.15 only) - FEATURE: Support for storing symbolic links in .torrent files (libtorrent >= v0.15 only) diff --git a/src/search_engine/helpers.py b/src/search_engine/helpers.py index 6d1198d0d..111b5f71f 100644 --- a/src/search_engine/helpers.py +++ b/src/search_engine/helpers.py @@ -22,7 +22,7 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. -#VERSION: 1.02 +#VERSION: 1.1 # Author: # Christophe DUMEZ (chris@qbittorrent.org) @@ -31,6 +31,7 @@ import re, htmlentitydefs import urllib2 import tempfile import os +import StringIO, gzip, httplib def htmlentitydecode(s): # First convert alpha entities (such as é) @@ -66,11 +67,20 @@ def retrieve_url(url): def download_file(url): """ Download file at url and write it to a file, return the path to the file and the url """ file, path = tempfile.mkstemp() - file = os.fdopen(file, "wb") + file = os.fdopen(file, "w") # Download url req = urllib2.Request(url) response = urllib2.urlopen(req) dat = response.read() + # Check if data is gzip encoded + response_info = response.info() + content_encoding = response_info.get('Content-Encoding') + if content_encoding is not None and 'gzip' in content_encoding: + # Data is gzip encoded, decode it + compressedstream = StringIO.StringIO(dat) + gzipper = gzip.GzipFile(fileobj=compressedstream) + extracted_data = gzipper.read() + dat = extracted_data # Write it to a file file.write(dat) file.close()