working URL uploader
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
import os
|
||||
from datetime import datetime
|
||||
from flask import current_app as app
|
||||
import requests
|
||||
import re
|
||||
try:
|
||||
from urllib.request import pathname2url
|
||||
from urllib.request import URLopener
|
||||
from urllib.request import urlparse
|
||||
from urllib.error import HTTPError
|
||||
except ImportError:
|
||||
from urllib import pathname2url
|
||||
from urllib import URLopener
|
||||
from urlparse import urlparse
|
||||
from urllib2 import HTTPError
|
||||
|
||||
|
||||
def download_url(url,filename):
|
||||
downloader = URLopener()
|
||||
def download_url(url, filename):
|
||||
try:
|
||||
downloader.retrieve(url, filename)
|
||||
except HTTPError as e:
|
||||
r = requests.get(url, stream=True)
|
||||
with open(filename, 'wb') as f:
|
||||
for chunk in r.iter_content(chunk_size=1024 * 1024):
|
||||
if chunk: # filter out keep-alive new chunks
|
||||
f.write(chunk)
|
||||
except requests.exceptions.RequestException as e:
|
||||
return (False, ("%s %s"%(e.code,e.reason), e.code))
|
||||
return (True, ("OK", 200 ))
|
||||
|
||||
@@ -144,6 +145,9 @@ def safe_path(s):
|
||||
return safe_string(s, "-_/")
|
||||
|
||||
|
||||
def safe_string(s, valid):
|
||||
def safe_string(s, valid, no_repeat = False):
|
||||
""" return a safe string, replace non alnum characters with _ . all characters in valid are considered valid. """
|
||||
return "".join([c if c.isalnum() or c in valid else "_" for c in s])
|
||||
safe = "".join([c if c.isalnum() or c in valid else "_" for c in s])
|
||||
if no_repeat:
|
||||
safe = re.sub(r'_+', '_', safe)
|
||||
return safe
|
||||
|
||||
Reference in New Issue
Block a user