2020-02-05 12:22:01 -06:00
|
|
|
#!/usr/bin/python3
|
|
|
|
from os import mkdir, environ
|
|
|
|
from os.path import exists
|
2020-02-05 13:40:53 -06:00
|
|
|
import requests
|
|
|
|
import slack
|
|
|
|
import json
|
|
|
|
|
|
|
|
# Twitch emote api link for emote metadata
|
|
|
|
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/'
|
2020-02-05 12:22:01 -06:00
|
|
|
|
2020-02-05 13:40:53 -06:00
|
|
|
# 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/'
|
2020-02-05 12:22:01 -06:00
|
|
|
|
2020-02-06 22:44:04 -06:00
|
|
|
def import_client_id():
|
|
|
|
if(exists('.client_id')):
|
|
|
|
try:
|
|
|
|
client_id_file = open('.client_id', 'rt')
|
|
|
|
output = client_id_file.readline().strip()
|
|
|
|
print(output)
|
|
|
|
return output
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
client_id = environ['TWITCH_APP_CLIENT_ID']
|
|
|
|
return client_id
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
exit(1)
|
|
|
|
|
2020-02-05 12:22:01 -06:00
|
|
|
|
|
|
|
def grab_id(n):
|
2020-02-05 13:40:53 -06:00
|
|
|
"""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
|
|
|
|
"""
|
2020-02-05 12:22:01 -06:00
|
|
|
return [f'{n["id"]}', f'{n["code"]}']
|
|
|
|
|
|
|
|
|
2020-02-06 22:44:04 -06:00
|
|
|
def request_twitch_emote_ids(client_id, service):
|
2020-02-05 13:40:53 -06:00
|
|
|
"""Send the request for a json of the Twitch emote metadata
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
service {str} -- String containing either "Twitch" or "BTTV" to determine which code to use
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
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',
|
2020-02-06 22:44:04 -06:00
|
|
|
'Client-ID': client_id
|
2020-02-05 13:40:53 -06:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
|
2020-02-05 12:22:01 -06:00
|
|
|
return list(map(grab_id, req_dict['emotes']))
|
|
|
|
|
|
|
|
|
|
|
|
def save_twitch_emotes_metadata(dirpath, emote_array):
|
2020-02-05 13:40:53 -06:00
|
|
|
"""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')
|
|
|
|
|
2020-02-05 12:22:01 -06:00
|
|
|
for x in emote_array:
|
|
|
|
metadata_file.write(f'id:{x[0]}\tcode:{x[1]}\n')
|
|
|
|
metadata_file.close()
|
|
|
|
|
|
|
|
|
2020-02-06 22:44:04 -06:00
|
|
|
def save_twitch_emotes_images(client_id, service, dirpath, emote_array):
|
2020-02-05 13:40:53 -06:00
|
|
|
"""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
|
|
|
|
"""
|
|
|
|
|
2020-02-05 12:22:01 -06:00
|
|
|
if(not exists(dirpath)):
|
|
|
|
mkdir(dirpath)
|
|
|
|
|
2020-02-05 13:40:53 -06:00
|
|
|
if(service.lower() == 'twitch'):
|
|
|
|
for emote_index in emote_array:
|
2020-02-05 13:55:40 -06:00
|
|
|
if(not exists(f'{dirpath}/{emote_index[1].lower()}.png')):
|
2020-02-05 13:40:53 -06:00
|
|
|
headers = {
|
|
|
|
'accept': 'application/vnd.twitchtv.v5+json',
|
2020-02-06 22:44:04 -06:00
|
|
|
'Client-ID': client_id
|
2020-02-05 13:40:53 -06:00
|
|
|
}
|
|
|
|
req = requests.get(
|
|
|
|
f'{twitch_emote_file_tmpl}{emote_index[0]}/1.0', headers=headers)
|
2020-02-05 13:55:40 -06:00
|
|
|
if('/' in emote_index[1] or '\\' in emote_index[1] or ':' in emote_index[1]):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
emote_file = open(f'{dirpath}/{emote_index[1].lower()}.png', 'bw+')
|
|
|
|
emote_file.write(req.content)
|
|
|
|
emote_file.close()
|
2020-02-05 13:40:53 -06:00
|
|
|
elif(service.lower() == 'bttv'):
|
|
|
|
for emote_index in emote_array:
|
2020-02-05 13:55:40 -06:00
|
|
|
if(not exists(f'{dirpath}/{emote_index[1].lower()}.png')):
|
2020-02-05 13:40:53 -06:00
|
|
|
req = requests.get(
|
|
|
|
f'{bttv_emote_file_tmpl}{emote_index[0]}/1x')
|
2020-02-05 13:55:40 -06:00
|
|
|
emote_file = open(f'{dirpath}/{emote_index[1].lower()}.png', 'bw+')
|
2020-02-05 13:40:53 -06:00
|
|
|
emote_file.write(req.content)
|
|
|
|
emote_file.close()
|
2020-02-05 12:22:01 -06:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-02-06 22:44:04 -06:00
|
|
|
# Attempt import of Client ID
|
|
|
|
client_id = import_client_id()
|
|
|
|
|
2020-02-05 13:40:53 -06:00
|
|
|
# Grab the Twitch official emotes
|
2020-02-06 22:44:04 -06:00
|
|
|
emotes_array = request_twitch_emote_ids(client_id, 'twitch')
|
|
|
|
save_twitch_emotes_images(client_id, 'twitch', 'out', emotes_array)
|
2020-02-05 13:40:53 -06:00
|
|
|
save_twitch_emotes_metadata('out', emotes_array)
|
|
|
|
|
|
|
|
# Grab the BTTV emotes
|
2020-02-06 22:44:04 -06:00
|
|
|
emotes_array = request_twitch_emote_ids(client_id, 'bttv')
|
|
|
|
save_twitch_emotes_images(client_id, 'bttv', 'out', emotes_array)
|
2020-02-05 12:22:01 -06:00
|
|
|
save_twitch_emotes_metadata('out', emotes_array)
|
|
|
|
|
|
|
|
|
|
|
|
if(__name__ == "__main__"):
|
|
|
|
main()
|