use requests library for media downloads
fixes SSL errors when pasting media from https website
This commit is contained in:
parent
554ff3d8d2
commit
23e0034278
@ -24,6 +24,7 @@ from aqt.utils import shortcut, showInfo, showWarning, getFile, \
|
|||||||
openHelp, tooltip, downArrow
|
openHelp, tooltip, downArrow
|
||||||
import aqt
|
import aqt
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
import requests
|
||||||
|
|
||||||
pics = ("jpg", "jpeg", "png", "tif", "tiff", "gif", "svg", "webp")
|
pics = ("jpg", "jpeg", "png", "tif", "tiff", "gif", "svg", "webp")
|
||||||
audio = ("wav", "mp3", "ogg", "flac", "mp4", "swf", "mov", "mpeg", "mkv", "m4a", "3gp", "spx", "oga")
|
audio = ("wav", "mp3", "ogg", "flac", "mp4", "swf", "mov", "mpeg", "mkv", "m4a", "3gp", "spx", "oga")
|
||||||
@ -584,13 +585,23 @@ to a cloze type first, via Edit>Change Note Type."""))
|
|||||||
if url.lower().startswith("file://"):
|
if url.lower().startswith("file://"):
|
||||||
url = url.replace("%", "%25")
|
url = url.replace("%", "%25")
|
||||||
url = url.replace("#", "%23")
|
url = url.replace("#", "%23")
|
||||||
|
local = True
|
||||||
|
else:
|
||||||
|
local = False
|
||||||
# fetch it into a temporary folder
|
# fetch it into a temporary folder
|
||||||
self.mw.progress.start(
|
self.mw.progress.start(
|
||||||
immediate=True, parent=self.parentWindow)
|
immediate=True, parent=self.parentWindow)
|
||||||
try:
|
try:
|
||||||
req = urllib.request.Request(url, None, {
|
if local:
|
||||||
'User-Agent': 'Mozilla/5.0 (compatible; Anki)'})
|
req = urllib.request.Request(url, None, {
|
||||||
filecontents = urllib.request.urlopen(req).read()
|
'User-Agent': 'Mozilla/5.0 (compatible; Anki)'})
|
||||||
|
filecontents = urllib.request.urlopen(req).read()
|
||||||
|
else:
|
||||||
|
r = requests.get(url)
|
||||||
|
if r.status_code != 200:
|
||||||
|
showWarning(_("Unexpected response code: %s") % r.status_code)
|
||||||
|
return
|
||||||
|
filecontents = r.content
|
||||||
except urllib.error.URLError as e:
|
except urllib.error.URLError as e:
|
||||||
showWarning(_("An error occurred while opening %s") % e)
|
showWarning(_("An error occurred while opening %s") % e)
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user