Skip to content

Instantly share code, notes, and snippets.

@iam-mhaseeb
Created January 31, 2022 09:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iam-mhaseeb/e5bc7d387eab41395b2f52ccdd6e4c17 to your computer and use it in GitHub Desktop.
Save iam-mhaseeb/e5bc7d387eab41395b2f52ccdd6e4c17 to your computer and use it in GitHub Desktop.
Slackmojis.com Downloader
import os
import sys
import requests
from bs4 import BeautifulSoup
def show_arg():
req = requests.get(sys.argv[1])
soup_response = BeautifulSoup(req.text, "html.parser")
script_dir = os.path.dirname(os.path.abspath(__file__))
dest_dir = os.path.join(script_dir, "slackmojis")
try:
os.makedirs(dest_dir)
except OSError:
pass # already exists
for img in soup_response.find_all("img"):
src_attr = img.get("src")
if src_attr.find("/"):
filename = src_attr.rsplit("/", 1)[1].split("?", 1)[0]
with open(os.path.join(dest_dir, filename), "wb") as file:
print("Downloading %s" % filename)
response = requests.get(src_attr, stream=True)
file_size = response.headers.get("content-length")
if file_size is None: # no content length header
file.write(response.content)
else:
dl = 0
file_size = int(file_size)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
file.write(data)
done = int(50 * dl / file_size)
sys.stdout.write("\r[%s%s]" % ("=" * done, " " * (50 - done)))
sys.stdout.flush()
if __name__ == "__main__":
show_arg()
@azabost
Copy link

azabost commented Oct 12, 2022

Thanks for this. It was really helpful. However, I noticed that sometimes Slackmojis page contains more than one emoji with the same filename. As a result, only the last one was preserved after using this script to download them. Therefore, I slightly improved this script by generating filenames with suffixes in case of duplicates (e.g. if foo.png is taken, then it will try foo-2.png, then foo-3.png etc.). Feel free to check it out here: https://gist.github.com/azabost/c73394854e4022e9cc67547913b231d5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment