Skip to content

Instantly share code, notes, and snippets.

@cryocaustik
Last active October 8, 2018 19:48
Show Gist options
  • Save cryocaustik/8f144119fa06552f94ea2fdce8c5caa4 to your computer and use it in GitHub Desktop.
Save cryocaustik/8f144119fa06552f94ea2fdce8c5caa4 to your computer and use it in GitHub Desktop.
python script to apply dark theme to the latest Slack Chat app
import requests
import os
class PythonicSlack():
"""Identified latest version of Slack App and applies dark theme to it.
Raises:
Exception: Exception raised in the event that the Slack Root directory is not found
Exception: Exception is raised in event that the Slack App directory is not found in the Slack Root direcotry
ValueError: Exception is raised in the event that the slack theme is blank
"""
def __init__(self):
"""Applies core Slack and Theme variables
"""
self.SLACK_ROOT = '{}/slack'.format(os.environ.get('localappdata'))
self.THEME_URL = 'https://raw.githubusercontent.com/cryocaustik/Slack-Windows-Theme/master/slack_theme.js'
self.slack_app_dir = self._get_slack_dir()
self.slack_js_file = self._get_slack_js_file()
self.theme_js = self._get_theme_js()
def __call__(self):
"""Apply Slack Theme.
"""
theme_line_break = "\n\n\n{br}\n//\tslack theme applied by {author}\n{br}\n\n\n".format(br='//'*30, author='python')
with open(self.slack_js_file, 'a') as _f:
_f.write(theme_line_break)
_f.write(self.theme_js)
_f.close()
print("success! theme applied to {}".format(self.slack_js_file))
def _get_slack_dir(self, root_dir=None):
"""get the directory for the latest slack app version within the slack root directory
root_dir (str, optional): Defaults to None. Path to root directory of Slack
Raises:
Exception: Exception raised when Slack directory is not found
Returns:
str: Path to Slack root directory
"""
if not root_dir:
root_dir = self.SLACK_ROOT
dir_list = list()
for _d in os.listdir(root_dir):
_p = os.path.join(root_dir, _d)
if os.path.isdir(_p) and 'app-' in _p:
dir_list.append(_d)
if not dir_list:
raise Exception('no app directories found')
return os.path.join(root_dir, dir_list[-1])
def _get_slack_js_file(self, target_file='ssb-interop.js'):
"""Locates and returns the path to the Slack JS files used to apply the theme
target_file (str, optional): Defaults to 'ssb-interop.js'. Slack JS file name to be found
Raises:
Exception: Exception raisd when target file is not found
Returns:
str: Path to Target file
"""
slack_app_dir = self.slack_app_dir
for p, d, f in os.walk(slack_app_dir):
for _f in f:
if _f.lower() == target_file:
return os.path.join(p, _f)
else:
raise Exception('target file not found')
def _get_theme_js(self, theme_url=None):
"""Pulls (http) and returns the Slack Theme JS Code
theme_url (str, optional): Defaults to None. URL to Slack Theme JS
Raises:
ValueError: Exception raised when theme js is blank
Returns:
str: Slack Theme JS
"""
if not theme_url:
theme_url = self.THEME_URL
response = requests.get(theme_url)
if response.status_code != 200:
response.raise_for_status
response_text = response.text
if not response_text:
raise ValueError('response_test is blank')
return response_text
if __name__ == '__main__':
t = PythonicSlack()()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment