Created
January 31, 2022 09:40
-
-
Save iam-mhaseeb/e5bc7d387eab41395b2f52ccdd6e4c17 to your computer and use it in GitHub Desktop.
Slackmojis.com Downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 tryfoo-2.png
, thenfoo-3.png
etc.). Feel free to check it out here: https://gist.github.com/azabost/c73394854e4022e9cc67547913b231d5