Browse Source

- Commit changes to helpers.py (add referer to download_file() helper function)

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
21eb26a374
  1. 9
      src/search_engine/helpers.py

9
src/search_engine/helpers.py

@ -22,16 +22,15 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
#VERSION: 1.1 #VERSION: 1.2
# Author: # Author:
# Christophe DUMEZ (chris@qbittorrent.org) # Christophe DUMEZ (chris@qbittorrent.org)
import re, htmlentitydefs import re, htmlentitydefs
import urllib2
import tempfile import tempfile
import os import os
import StringIO, gzip, httplib import StringIO, gzip, urllib2
def htmlentitydecode(s): def htmlentitydecode(s):
# First convert alpha entities (such as é) # First convert alpha entities (such as é)
@ -64,12 +63,14 @@ def retrieve_url(url):
dat = htmlentitydecode(dat) dat = htmlentitydecode(dat)
return dat.encode('utf-8', 'replace') return dat.encode('utf-8', 'replace')
def download_file(url): def download_file(url, referer=None):
""" Download file at url and write it to a file, return the path to the file and the 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, path = tempfile.mkstemp()
file = os.fdopen(file, "w") file = os.fdopen(file, "w")
# Download url # Download url
req = urllib2.Request(url) req = urllib2.Request(url)
if referer is not None:
req.add_header('referer', referer)
response = urllib2.urlopen(req) response = urllib2.urlopen(req)
dat = response.read() dat = response.read()
# Check if data is gzip encoded # Check if data is gzip encoded

Loading…
Cancel
Save