Created
August 13, 2024 06:16
-
-
Save blipk/921a6b376ef2f5e247750d50cf95fcfe to your computer and use it in GitHub Desktop.
Install Spell Checker Language Files for Ungoogled Chromium Linux
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
#!/usr/bin/env python | |
import os | |
import sys | |
import base64 | |
import requests | |
from bs4 import BeautifulSoup | |
repo_url = "https://chromium.googlesource.com" | |
file_ext = ".bdic" | |
def get_lang_file_links(): | |
url = f"{repo_url}/chromium/deps/hunspell_dictionaries/+/master" | |
response = requests.get(url) | |
if response.status_code != 200: | |
raise Exception(f"Failed to load page {url}") | |
soup = BeautifulSoup(response.text, 'html.parser') | |
file_links = soup.find_all('a', class_='FileList-itemLink') | |
links_dict = { | |
link_text.replace(file_ext, ""): link.get('href') | |
for link in file_links | |
if (link_text := link.get_text(strip=True)).endswith(file_ext) | |
} | |
return links_dict | |
def download_lang_file_text(links_dict, lang_code): | |
url = f"{repo_url}{links_dict[lang_code]}?format=TEXT" | |
response = requests.get(url) | |
if response.status_code != 200: | |
raise Exception(f"Failed to load page {url}") | |
return response.text | |
def main(): | |
links_dict = get_lang_file_links() | |
if len(sys.argv) < 2: | |
print("Please pass language code as first and only argument") | |
print("Available languages:") | |
print(list(links_dict.keys())) | |
return | |
lang_code = sys.argv[1] | |
if lang_code not in links_dict: | |
print("Unavailable/invalid language code passed") | |
print("Available languages:") | |
print(list(links_dict.keys())) | |
return | |
base64_lang_file = download_lang_file_text(links_dict, lang_code) | |
lang_file_binary = base64.b64decode(base64_lang_file) | |
# print(lang_file_binary) | |
dictionaries_directory = os.path.expanduser("~/.config/chromium/Dictionaries/") | |
flatpak_dictionaries_directory = os.path.expanduser("~/.var/app/com.github.Eloston.UngoogledChromium/config/chromium/Dictionaries/") | |
if os.path.exists(flatpak_dictionaries_directory): | |
dictionary_file_path = f"{flatpak_dictionaries_directory}/{lang_code}{file_ext}" | |
else: | |
dictionary_file_path = f"{dictionaries_directory}/{lang_code}{file_ext}" | |
with open(dictionary_file_path, "wb") as f: | |
f.write(lang_file_binary) | |
print(f"Saved to {dictionary_file_path}") | |
return links_dict | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment