From 1d702df22b31d705a7255f9e84a7d8e516a94b18 Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Sat, 4 May 2024 18:37:47 +0530 Subject: extention update --- dict | 51 --------------------------------------------------- dict.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 51 deletions(-) delete mode 100755 dict create mode 100755 dict.py diff --git a/dict b/dict deleted file mode 100755 index aca3fbf..0000000 --- a/dict +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/python - -import requests -import random -import os -import sys # Import for potential error handling - -# Customizable word list path -WORD_LIST_PATH = "~/.local/share/vocab/words.txt" - -def get_random_word_from_urban_dictionary(): - """Retrieves a random word and definition from Urban Dictionary API.""" - url = "http://api.urbandictionary.com/v0/random" - - try: - response = requests.get(url, timeout=2) - response.raise_for_status() # Raise an exception if request failed - - data = response.json() - word = data['list'][0]['word'] - definition = data['list'][0]['definition'] - return word, definition - - except requests.exceptions.RequestException: - return None, None - -def get_random_word_from_local_file(): - """Retrieves a random word from the local word list.""" - try: - # Expand '~' for the home directory in the path - word_list_path = os.path.expanduser(WORD_LIST_PATH) - - with open(word_list_path, 'r') as file: - words = file.readlines() - # Choose a random word (assuming words are separated by newlines) - return words[random.randint(0, len(words) - 1)].strip() - - except (FileNotFoundError, PermissionError) as e: - print(f"Error reading local word list: {e}") - return None - -if __name__ == "__main__": - word, definition = get_random_word_from_urban_dictionary() - - if not word: # Check if word retrieval failed - word = get_random_word_from_local_file() - - if word: - os.system(f"cowsay '{word} -> {definition if definition else ''}'") - else: - print("Could not get a word from either source.") diff --git a/dict.py b/dict.py new file mode 100755 index 0000000..aca3fbf --- /dev/null +++ b/dict.py @@ -0,0 +1,51 @@ +#!/bin/python + +import requests +import random +import os +import sys # Import for potential error handling + +# Customizable word list path +WORD_LIST_PATH = "~/.local/share/vocab/words.txt" + +def get_random_word_from_urban_dictionary(): + """Retrieves a random word and definition from Urban Dictionary API.""" + url = "http://api.urbandictionary.com/v0/random" + + try: + response = requests.get(url, timeout=2) + response.raise_for_status() # Raise an exception if request failed + + data = response.json() + word = data['list'][0]['word'] + definition = data['list'][0]['definition'] + return word, definition + + except requests.exceptions.RequestException: + return None, None + +def get_random_word_from_local_file(): + """Retrieves a random word from the local word list.""" + try: + # Expand '~' for the home directory in the path + word_list_path = os.path.expanduser(WORD_LIST_PATH) + + with open(word_list_path, 'r') as file: + words = file.readlines() + # Choose a random word (assuming words are separated by newlines) + return words[random.randint(0, len(words) - 1)].strip() + + except (FileNotFoundError, PermissionError) as e: + print(f"Error reading local word list: {e}") + return None + +if __name__ == "__main__": + word, definition = get_random_word_from_urban_dictionary() + + if not word: # Check if word retrieval failed + word = get_random_word_from_local_file() + + if word: + os.system(f"cowsay '{word} -> {definition if definition else ''}'") + else: + print("Could not get a word from either source.") -- cgit v1.2.3-59-g8ed1b