Last active
January 11, 2022 21:12
-
-
Save Davr1/9171f2d1cc639abd7e8df81796aab310 to your computer and use it in GitHub Desktop.
This file contains 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 json | |
import re | |
from operator import itemgetter | |
import requests | |
# finds the most similar list | |
# master = [] | |
# candidates = [{}, {}, {},...] | |
def best_similarity(master, candidates): | |
counts = [0] * len(candidates) | |
for candidate_index, candidate in enumerate(candidates): | |
for item in candidate: | |
if item in master: | |
counts[candidate_index] += 1 | |
# makes sure that candidates with a similar amount of items but less matches have some advantage | |
# over candidates with more items and more matches | |
# can be visualised as y = -x^2 + 100, where x = 0 is a perfect match | |
counts[candidate_index] *= (-((len(candidate) - len(master)) ** 2) + 100) | |
candidate_scores = zip(counts, candidates) | |
# returns a map | |
return max(candidate_scores, key=itemgetter(0))[1] | |
def main(): | |
old = requests.get("https://discord.com/assets/454210598d8beed381ab.js")._content.decode("utf8") | |
new = requests.get("https://canary.discord.com/assets/782c119d5f878e08f08e.js")._content.decode("utf8") | |
# jsonifies the source files | |
replacements = [[r"\n", ""], [r"\},\d+.*?s=", ","], [r"\}{2,}.*", "}]"], [r"^.*\{,", "["], [r"(?<=\{|,)\b", "\""], [":", "\":"],["\"+", "\""]] | |
for r in replacements: | |
old = re.sub(r[0], r[1], old) | |
new = re.sub(r[0], r[1], new) | |
old = json.loads(old) | |
new = json.loads(new) | |
output = open("output.md", "w+") | |
for element_map in old: | |
match = best_similarity(element_map, new) | |
for element in element_map: | |
# checks if element contains a number as the first character, or a parenthesis | |
# -> gets rid of elements such as "calc(...)" or "24px" | |
if element in match and re.search(r"^\d|\(|\)", match[element]): | |
print(f"Discarding {element}") | |
else: | |
try: | |
# splits elements with multiple classes into multiple lines | |
for substring in zip(element_map[element].split(' '), match[element].split(' ')): | |
output.write(f"{substring[0]} = {substring[1]}\n") | |
print(f"Found {element_map[element]}") | |
except: | |
print(f"Couldn't find {element}") | |
print("Finished") | |
output.close() | |
main() |
how use
@Errorcrafter download the py file, open cmd, pip install requests
wait while it installs
python classfinder.py
enjoy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example files that this could be used with:https://cdn.discordapp.com/attachments/898335083944099880/929688339936866314/new_classes.json
https://cdn.discordapp.com/attachments/898335083944099880/929688340150779925/old_classes.json
Edit: additional files are no longer needed