Script can grab official Twitch emotes and BTTV emotes.
This commit is contained in:
parent
73a36b7cbf
commit
2f6821d8ae
2 changed files with 100 additions and 27 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -0,0 +1,6 @@
|
||||||
|
out/**
|
||||||
|
.api_token*
|
||||||
|
.vscode
|
||||||
|
Pipfile.lock
|
||||||
|
**/__pycache__/**
|
||||||
|
**.pyc
|
|
@ -1,55 +1,122 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import json
|
|
||||||
import requests
|
|
||||||
import slack
|
|
||||||
from os import mkdir, environ
|
from os import mkdir, environ
|
||||||
from os.path import exists
|
from os.path import exists
|
||||||
|
import requests
|
||||||
|
import slack
|
||||||
|
import json
|
||||||
|
|
||||||
emote_api_link = 'https://api.twitchemotes.com/api/v4/channels/0'
|
# Twitch emote api link for emote metadata
|
||||||
emote_file_tmpl = 'https://static-cdn.jtvnw.net/emoticons/v1/'
|
twitch_emote_api_link = 'https://api.twitchemotes.com/api/v4/channels/0'
|
||||||
|
# Twitch emote link for emote images
|
||||||
|
twitch_emote_file_tmpl = 'https://static-cdn.jtvnw.net/emoticons/v1/'
|
||||||
|
|
||||||
|
# BTTV emote api link for emote metadata
|
||||||
|
bttv_emote_api_link = 'https://api.betterttv.net/2/emotes'
|
||||||
|
# BTTV emote link for emote images
|
||||||
|
bttv_emote_file_tmpl = 'https://cdn.betterttv.net/emote/'
|
||||||
|
|
||||||
|
|
||||||
def grab_id(n):
|
def grab_id(n):
|
||||||
|
"""Map function to extract the id and code from each dictionary entry
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
n {int} -- Current dictionary object
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
arr -- Array containing the id and code
|
||||||
|
"""
|
||||||
return [f'{n["id"]}', f'{n["code"]}']
|
return [f'{n["id"]}', f'{n["code"]}']
|
||||||
|
|
||||||
|
|
||||||
def request_twitch_emote_ids():
|
def request_twitch_emote_ids(service):
|
||||||
headers = {
|
"""Send the request for a json of the Twitch emote metadata
|
||||||
'accept': 'application/vnd.twitchtv.v5+json',
|
|
||||||
'Client-ID': 'x9pe1nhx03034xssm1kcla36unubrc'
|
Arguments:
|
||||||
}
|
service {str} -- String containing either "Twitch" or "BTTV" to determine which code to use
|
||||||
req = requests.get(emote_api_link, headers=headers)
|
|
||||||
req_dict = json.loads(req.content)
|
Returns:
|
||||||
req.close()
|
list -- List iterable of the ids and codes for the emotes returned by the request json
|
||||||
|
"""
|
||||||
|
|
||||||
|
req_dict = {}
|
||||||
|
|
||||||
|
if(service.lower() == 'twitch'):
|
||||||
|
headers = {
|
||||||
|
'accept': 'application/vnd.twitchtv.v5+json',
|
||||||
|
'Client-ID': 'x9pe1nhx03034xssm1kcla36unubrc'
|
||||||
|
}
|
||||||
|
req = requests.get(twitch_emote_api_link, headers=headers)
|
||||||
|
req_dict = json.loads(req.content)
|
||||||
|
req.close()
|
||||||
|
elif(service.lower() == 'bttv'):
|
||||||
|
req = requests.get(bttv_emote_api_link)
|
||||||
|
req_dict = json.loads(req.content)
|
||||||
|
req.close()
|
||||||
|
|
||||||
return list(map(grab_id, req_dict['emotes']))
|
return list(map(grab_id, req_dict['emotes']))
|
||||||
|
|
||||||
|
|
||||||
def save_twitch_emotes_metadata(dirpath, emote_array):
|
def save_twitch_emotes_metadata(dirpath, emote_array):
|
||||||
metadata_file = open(f'{dirpath}/emote_metadata.txt')
|
"""Save the emote_array metadata to a text file to refer to later
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
dirpath {str} -- String containing the directory path for output
|
||||||
|
emote_array {list} -- List iterable of the ids and code for the emotes returned by the original request json
|
||||||
|
"""
|
||||||
|
|
||||||
|
if(not exists(f'{dirpath}/emote_metadata.txt')):
|
||||||
|
metadata_file = open(f'{dirpath}/emote_metadata.txt', 'w+')
|
||||||
|
else:
|
||||||
|
metadata_file = open(f'{dirpath}/emote_metadata.txt', 'a')
|
||||||
|
|
||||||
for x in emote_array:
|
for x in emote_array:
|
||||||
metadata_file.write(f'id:{x[0]}\tcode:{x[1]}\n')
|
metadata_file.write(f'id:{x[0]}\tcode:{x[1]}\n')
|
||||||
metadata_file.close()
|
metadata_file.close()
|
||||||
|
|
||||||
|
|
||||||
def save_twitch_emotes_images(dirpath, emote_array):
|
def save_twitch_emotes_images(service, dirpath, emote_array):
|
||||||
|
"""Send a request for the emote images and save them locally to a specified directory
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
service {str} -- String contianing either "Twitch" or "BTTV" to determine which code to use
|
||||||
|
dirpath {str} -- String containing the directory path for output
|
||||||
|
emote_array {list} -- List iterable of the ids and codes for the emotes returned by the original request json
|
||||||
|
"""
|
||||||
|
|
||||||
if(not exists(dirpath)):
|
if(not exists(dirpath)):
|
||||||
mkdir(dirpath)
|
mkdir(dirpath)
|
||||||
|
|
||||||
for emote_index in emote_array:
|
if(service.lower() == 'twitch'):
|
||||||
headers = {
|
for emote_index in emote_array:
|
||||||
'accept': 'application/vnd.twitchtv.v5+json',
|
if(not exists(f'{dirpath}/{emote_index[0]}.png')):
|
||||||
'Client-ID': 'x9pe1nhx03034xssm1kcla36unubrc'
|
headers = {
|
||||||
}
|
'accept': 'application/vnd.twitchtv.v5+json',
|
||||||
req = requests.get(f'{emote_file_tmpl}{emote_index[0]}/1.0', headers=headers)
|
'Client-ID': 'x9pe1nhx03034xssm1kcla36unubrc'
|
||||||
emote_file = open(f'{dirpath}/{emote_index[0]}.png', 'bw+')
|
}
|
||||||
emote_file.write(req.content)
|
req = requests.get(
|
||||||
emote_file.close()
|
f'{twitch_emote_file_tmpl}{emote_index[0]}/1.0', headers=headers)
|
||||||
|
emote_file = open(f'{dirpath}/{emote_index[0]}.png', 'bw+')
|
||||||
|
emote_file.write(req.content)
|
||||||
|
emote_file.close()
|
||||||
|
elif(service.lower() == 'bttv'):
|
||||||
|
for emote_index in emote_array:
|
||||||
|
if(not exists(f'{dirpath}/{emote_index[0]}.png')):
|
||||||
|
req = requests.get(
|
||||||
|
f'{bttv_emote_file_tmpl}{emote_index[0]}/1x')
|
||||||
|
emote_file = open(f'{dirpath}/{emote_index[0]}.png', 'bw+')
|
||||||
|
emote_file.write(req.content)
|
||||||
|
emote_file.close()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Hello, world!")
|
# Grab the Twitch official emotes
|
||||||
emotes_array = request_twitch_emote_ids()
|
emotes_array = request_twitch_emote_ids('twitch')
|
||||||
save_twitch_emotes_images('out', emotes_array)
|
save_twitch_emotes_images('twitch', 'out', emotes_array)
|
||||||
|
save_twitch_emotes_metadata('out', emotes_array)
|
||||||
|
|
||||||
|
# Grab the BTTV emotes
|
||||||
|
emotes_array = request_twitch_emote_ids('bttv')
|
||||||
|
save_twitch_emotes_images('bttv', 'out', emotes_array)
|
||||||
save_twitch_emotes_metadata('out', emotes_array)
|
save_twitch_emotes_metadata('out', emotes_array)
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue