Created
April 18, 2024 11:42
-
-
Save MolarFox/96132e621a67f0303db5a29553d18b13 to your computer and use it in GitHub Desktop.
[Python] Rename blobfox emotes to be indexable
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
| #!/usr/bin/python3 | |
| """ | |
| MolarFox 2024 | |
| On most platforms (Slack, Masto, Firefish, Discord, etc), you can search emotes by all words in them if the words are underscore delineated | |
| This doesn't work by default for blobfox emotes since the filenames are all 1 word | |
| This script will rename all files to be underscore delineated so you can let your software auto-index | |
| This is just a quick script I smashed out in a half hour, it's stable but could be improved w/ better encapsulation, reading dirpath from stdin etc | |
| Get the emotes: https://volpeon.ink/projects/emojis/blobfox/ | |
| Note that animated emotes are prefixed by `a` and are in apng format | |
| If not relying on the prefix, exiftool can reliably identify apngs | |
| I recommend converting to something more useful like `*.gif`. APNG in this format is rarely recognised properly as animated / multi-frame | |
| """ | |
| import os | |
| import re | |
| from pathlib import Path | |
| BASE_FILEPATH = "/path/to/blobfox/" # todo: argparse this from argv | |
| # These are words to chunk by, in order to attempt chunking | |
| ACCEPTED_WORDS = [ | |
| "blobfox", | |
| "happy", | |
| "pat", | |
| "smirk", | |
| "upset", | |
| "happy", | |
| "cofe", | |
| "blush", | |
| "owo", | |
| "nom", | |
| "aww", | |
| "peek", | |
| "boop", | |
| "cute", | |
| "mlem", | |
| "sad", | |
| "comfy", | |
| "loading", | |
| "kirby", | |
| "sign", | |
| "ghost", | |
| "snuggle", | |
| "bongo", | |
| "glare", | |
| "wink", | |
| "laugh", | |
| "googly", | |
| "rage", | |
| "think", | |
| "lurk", | |
| "center", | |
| "cookie", | |
| "right", | |
| "floof", | |
| "melt", | |
| "sob", | |
| "love", | |
| "terrified", | |
| "large", | |
| "cake", | |
| "strawberry", | |
| "astronaut", | |
| "gun", | |
| "hyper", | |
| "sip", | |
| "donut", | |
| "yoshi", | |
| "dealwithit", | |
| "upsidedown", | |
| "left", | |
| "snoot", | |
| "drake", | |
| "thumbs", | |
| "_w_", | |
| "big", | |
| "sweat" | |
| "eat", | |
| "reach", | |
| "flower", | |
| "sleepy", | |
| "burger", | |
| "terrified", | |
| "start", | |
| "3c", | |
| "large", | |
| "cry", | |
| "eat" | |
| ] | |
| EXCEPTION_ITEMS = { | |
| "blobfoxsleepdab": "blobfox_sleep_dab", | |
| "blobfoxnauseated": "blobfox_nauseated", | |
| "blobfoxsweating": "blobfox_sweating", | |
| "blobfoxsweat": "blobfox_sweat", | |
| "blobfoxthinking": "blobfox_thinking", | |
| "blobfoxlaughsweat": "blobfox_laugh_sweat", | |
| "blobfoxdealwithitfingerguns": "blobfox_dealwithit_fingerguns", | |
| "blobfoxfingerguns": "blobfox_fingerguns", | |
| "ablobfoxhyperthinking": "a_blobfox_hyper_thinking", | |
| "blobfoxmeltsoblove": "blobfox_melt_sob_love", | |
| "blobfoxastronout": "blobfox_astronaut", | |
| "blobfoxastronoutgun": "blobfox_astronaut_gun", | |
| "blobfox_w_": "blobfox_w_", | |
| "blobfoxboop_w_": "blobfox_boop_w_", | |
| "blobfoxfloof_w_": "blobfox_floof_w_", | |
| "blobfoxcofe_w_": "blobfox_cofe_w_", | |
| "blobfoxtea_w_": "blobfox_tea_w_", | |
| } | |
| def split_string(s: str, w: str) -> list[str]: | |
| if w not in s: | |
| return [s] | |
| return [part for part in re.split(f"({w})", s) if part] | |
| if __name__=="__main__": | |
| # new_words = [] | |
| # Iterate all files | |
| for path in Path(BASE_FILEPATH).iterdir(): | |
| if path.is_dir() or path.parts[-1] == "LICENSE": | |
| continue | |
| # Init filename for use | |
| raw_filename = path.parts[-1].split('.')[0] | |
| filename = split_string(raw_filename, "blobfox") | |
| # Handle exceptions that are explicitly mapped above | |
| if raw_filename in EXCEPTION_ITEMS: | |
| new_filename = EXCEPTION_ITEMS[raw_filename] + ".png" | |
| new_path = "/".join(path.parts[:-1] + (new_filename,)) | |
| # print(f"{path.parts[-1]:<32} -> {new_filename}") | |
| os.rename(str(path), new_path) | |
| continue | |
| # Iterate filename until no parts can be matched | |
| no_matches = False | |
| while not no_matches: | |
| no_matches = True | |
| # Iterate all parts of path to find new keyword matches | |
| for i, part in enumerate(filename): | |
| for w in ACCEPTED_WORDS: | |
| if w == part: # Don't look for more matches if the part is itself a match | |
| break | |
| elif w in part: | |
| no_matches = False | |
| filename = filename[:i] + (split_string(filename[i], w)) + filename[i+1:] | |
| if no_matches: | |
| break | |
| new_filename = "_".join(filename) + ".png" | |
| new_path = "/".join(path.parts[:-1] + (new_filename,)) | |
| # print(f"{path.parts[-1]:<32} -> {new_filename}") | |
| os.rename(str(path), new_path) | |
| ## DEBUG | |
| # for part in filename: | |
| # if part not in ACCEPTED_WORDS: | |
| # new_words.append(part) | |
| # [print(x) for x in sorted(set(new_words))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment