Last active
October 23, 2022 12:56
-
-
Save spyoungtech/1b44a6b83b41e1239ee9b89be73a1f1b to your computer and use it in GitHub Desktop.
Download twitch emotes from twitchemotes.com channel
This file contains hidden or 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 | |
from requests import Session | |
from bs4 import BeautifulSoup | |
requests = Session() | |
class EmoteNotFoundError(RuntimeError): | |
... | |
def get_emote(emote_page, size="3.0"): # valid sizes are 1.0 2.0 and 3.0 | |
soup = BeautifulSoup(requests.get(emote_page).text, 'html.parser') | |
big_img = soup.find(lambda tag: tag.name == 'img' and tag.get('src', '').endswith(f'/{size}')) | |
if not big_img: | |
raise EmoteNotFoundError(f'Emote image not found on page {emote_page}') | |
name = soup.find('h2').text | |
return name, big_img.get('src') | |
def download_file(link, dest_fp): | |
data = requests.get(link).content | |
with open(dest_fp, 'wb') as f: | |
f.write(data) | |
def overwrite_yesnoall(filepath): | |
prompt = f'{filepath} already exists. Do you want to overwrite this file? ([Y]es/[N]o/[A]ll): ' | |
while True: | |
response = input(prompt) | |
if response.lower() in ('y', 'yes'): | |
return 'y' | |
if response.lower() in ('n', 'no'): | |
return 'n' | |
if response.lower() in ('a', 'all'): | |
return 'a' | |
def emote_links_from_channel_link(channel_link): | |
resp = requests.get(channel_link) | |
soup = BeautifulSoup(resp.text, 'html.parser') | |
emote_pages = [] | |
for tag in soup.find_all(lambda tag: tag.name == 'a' and 'emote' in tag.get('href', '')): | |
link = 'https://twitchemotes.com'+tag.get('href') | |
emote_pages.append(link) | |
return emote_pages | |
def download_channel(channel_link, dest=None): | |
print('Never use emotes without creator permission!') | |
overwrite_all = False | |
if dest is None: | |
dest = os.getcwd() | |
emote_pages = emote_links_from_channel_link(channel_link) | |
for page in emote_pages: | |
try: | |
name, image_link = get_emote(page) | |
except EmoteNotFoundError as e: | |
print(str(e), 'Skipping') | |
continue | |
if 'animated' in image_link: | |
extension = '.gif' | |
else: | |
extension = '.png' | |
fp = os.path.join(dest, name + extension) | |
if not overwrite_all and os.path.exists(fp): | |
user_response = overwrite_yesnoall(fp) | |
if user_response == 'n': | |
continue | |
if user_response == 'a': | |
overwrite_all = True | |
print(f'Downloading {name} to {fp}') | |
download_file(image_link, fp) |
You need to go to twitchemotes.com and find the URL for their channel and pass that to the download_channel
function
download_channel('https://twitchemotes.com/channels/40972890')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, may I ask how to use this code you've written? I am trying to archive a list of twitch channels and the only thing I don't have right now are their emotes. I haven't figured out how to run this successfully, can you provide me more context please? I would greatly appreciate it! I have the channel names in a txt file currently separate by a new line and I can fetch the channel IDs using Twitch's API if needed. Thank you!