Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slack exported conversation avatar replacement with locally downloaded avatars from original URL
import os
import lxml.html as LH
import shutil
import requests
temp_dir = "/tmp/avatar_tmp"
if not os.path.isdir("html/avatars"):
os.mkdir("html/avatars")
if os.path.isdir(temp_dir):
shutil.rmtree(temp_dir, ignore_errors=True)
os.mkdir(temp_dir)
for html_file in os.listdir('html'):
if os.path.isfile("html/" + html_file):
with open('html/' + html_file, "rb") as html_string:
root = LH.parse(html_string)
for el in root.iter('img'):
avatar_original_url = el.attrib['src']
split_data = str(el.attrib['src']).split('/')
if str(avatar_original_url).startswith('https://secure.gravatar.com/avatar/') or\
str(avatar_original_url).startswith('https://avatars.slack-edge.com/'):
avatar_folder_date = 'html/avatars/' + split_data[3]
if str(avatar_original_url).startswith('https://secure.gravatar.com/avatar/'):
avatar_file_name = split_data[4].split('?',1)[0]
else:
avatar_file_name = split_data[4]
avatar_folder_file = 'avatars/' + split_data[3] + '/' + avatar_file_name
print(avatar_folder_file)
if not os.path.isdir(avatar_folder_date):
os.mkdir(avatar_folder_date)
if not os.path.isfile(avatar_folder_date + '/' + avatar_file_name):
with open(avatar_folder_date + '/' + avatar_file_name, "wb") as avatar_destination:
avatar_destination.write(bytes(requests.get(avatar_original_url, stream=True).content))
print("New avatar found: " + avatar_folder_date + '/' + avatar_file_name)
# else:
# print("Avatar " + avatar_folder_date + '/' + avatar_file_name + " already existing")
el.attrib['src'] = avatar_folder_file
with open(temp_dir + '/' + html_file, "wb") as temp_file:
temp_file.write(LH.tostring(root, pretty_print=True))
os.remove('html/' + html_file)
shutil.move(temp_dir + '/' + html_file, "html/")
@droberin

This comment has been minimized.

@droberin

This comment has been minimized.

Copy link
Owner Author

commented Feb 9, 2018

Using this script in a docker container as in previous steps steps

Download gist code at output folder created with previous gist

cd $HOME/slack2html_output
wget -O fix_avatars.py \
https://gist.githubusercontent.com/droberin/74d8880610964518345d7285bc70f898/raw/17677ea13375697b3f25459df717ec1c65c6a523/fix_avatars.py

Execute docker and convert output

docker run -ti --rm -v $HOME/slack2html_output:/mnt/slack_data \
 -v $HOME/slack2html_output:/mnt/slack2html/ \
 python:3-slim bash -c 'cd /mnt/slack_data && pip3 install lxml requests && python3 fix_avatars.py'

Check renewed content

firefox $HOME/slack2html_output/html/

Remove python image if it ain't gonna be in use any longer.

It uses less than 200MiB of disk but... you know... clean it up...

docker rmi python:3-slim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.