Skip to content

Instantly share code, notes, and snippets.

@communiteq
Created October 13, 2021 11:27
Show Gist options
  • Save communiteq/c6241df1ef57292e479ff13e873e2ba3 to your computer and use it in GitHub Desktop.
Save communiteq/c6241df1ef57292e479ff13e873e2ba3 to your computer and use it in GitHub Desktop.
retrieve images from a raw discourse post
def get_dimensions(dimensions):
# either 123x456 or 123x456,78%
if ',' in dimensions:
dim, perc = dimensions.split(',')
perc = int(perc.replace('%', ''))
else:
dim = dimensions
perc = 100
x, y = dim.split('x')
x = int(int(x)*perc / 100)
y = int(int(y)*perc / 100)
return "{x}x{y}".format(x=x, y=y)
def fix_uploads(doc, basehref, outputdir):
uploads = re.findall('!\[(.*?)]\(upload://([A-Za-z0-9\.]*)\)', doc)
for alt, upl in uploads:
if '|' in alt:
alttext, dimensions = alt.split('|')
dimensions = get_dimensions(dimensions)
else:
alttext = alt
dimensions = ''
base, ext = upl.split('.')
rebase = hex(base62.decode(base, base62.CHARSET_INVERTED))[2:].zfill(40)
upload_location = '{basehref}/{c1}/{c2}/{rebase}.{ext}'.format(
basehref=basehref,
c1=rebase[0],
c2=rebase[1],
rebase=rebase,
ext=ext)
filename = '{base}.{ext}'.format(
base = hashlib.sha256(upload_location.encode('utf-8')).hexdigest(),
ext = ext)
fullfilename = '{outdir}/{filename}'.format(outdir = outputdir, filename = filename)
urllib.request.urlretrieve(upload_location, fullfilename)
if dimensions:
cmd = "mogrify -resize {dimensions} {filename}".format(
dimensions = dimensions,
filename = fullfilename
)
os.system(cmd)
search = "![{alt}](upload://{upl})".format(alt=alt, upl=upl)
replace = "\n![]({upl})\ \n".format(alt=alttext, upl=filename)
doc = doc.replace(search,replace)
return doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment