mirror of
https://github.com/moparisthebest/xeps
synced 2024-12-21 23:28:51 -05:00
gen.py: support for fetching remote images.
This commit is contained in:
parent
53eeaac258
commit
ff5614fff8
41
gen.py
41
gen.py
@ -48,6 +48,7 @@ from xml.dom.minidom import parse,parseString,Document,getDOMImplementation
|
|||||||
# for serializing inline images
|
# for serializing inline images
|
||||||
import base64
|
import base64
|
||||||
import urlparse
|
import urlparse
|
||||||
|
import urllib
|
||||||
|
|
||||||
XEPPATH = "/var/www/vhosts/xmpp.org/extensions"
|
XEPPATH = "/var/www/vhosts/xmpp.org/extensions"
|
||||||
CONFIGPATH = "/var/local/xsf"
|
CONFIGPATH = "/var/local/xsf"
|
||||||
@ -60,24 +61,28 @@ files_to_delete = [];
|
|||||||
|
|
||||||
def serializeInlineImage(output_dir, xep_nr, no, attrValue):
|
def serializeInlineImage(output_dir, xep_nr, no, attrValue):
|
||||||
up = urlparse.urlparse(attrValue)
|
up = urlparse.urlparse(attrValue)
|
||||||
head, data = up.path.split(',')
|
if up.scheme == 'data':
|
||||||
bits = head.split(';')
|
head, data = up.path.split(',')
|
||||||
mime_type = bits[0] if bits[0] else 'text/plain'
|
bits = head.split(';')
|
||||||
charset, b64 = 'ASCII', False
|
mime_type = bits[0] if bits[0] else 'text/plain'
|
||||||
for bit in bits[1]:
|
charset, b64 = 'ASCII', False
|
||||||
if bit.startswith('charset='):
|
for bit in bits[1]:
|
||||||
charset = bit[8:]
|
if bit.startswith('charset='):
|
||||||
elif bit == 'base64':
|
charset = bit[8:]
|
||||||
b64 = True
|
elif bit == 'base64':
|
||||||
|
b64 = True
|
||||||
# Do something smart with charset and b64 instead of assuming
|
|
||||||
plaindata = base64.b64decode(data)
|
# Do something smart with charset and b64 instead of assuming
|
||||||
|
plaindata = base64.b64decode(data)
|
||||||
# Do something smart with mime_type
|
|
||||||
if mime_type in ('image/png', 'image/jpeg'):
|
# Do something smart with mime_type
|
||||||
file_ext = mime_type.split('/')[1]
|
if mime_type in ('image/png', 'image/jpeg'):
|
||||||
f = open(output_dir + '/' + 'inlineimage-' + xep_nr + '-' + str(no) + '.' + file_ext, 'wb')
|
file_ext = mime_type.split('/')[1]
|
||||||
f.write(plaindata)
|
f = open(output_dir + '/' + 'inlineimage-' + xep_nr + '-' + str(no) + '.' + file_ext, 'wb')
|
||||||
|
f.write(plaindata)
|
||||||
|
elif up.scheme == 'http':
|
||||||
|
file_name, file_ext = os.path.splitext(up.path)
|
||||||
|
urllib.urlretrieve(attrValue, output_dir + '/' + 'inlineimage-' + xep_nr + '-' + str(no) + file_ext)
|
||||||
|
|
||||||
def serializeXEPInlineImages(output_dir, xep_nr, filename):
|
def serializeXEPInlineImages(output_dir, xep_nr, filename):
|
||||||
dom = parse(filename)
|
dom = parse(filename)
|
||||||
|
Loading…
Reference in New Issue
Block a user