Skip to content

Commit

Permalink
feat: add caching mechanism
Browse files Browse the repository at this point in the history
A basic cachesim feature has been added. It stores newly searched words
in a file named `.cambd-cache.json` in `$HOME`. And for every new word
lookup it checks the cache file if the definition for that specfic word
already exists. If yes then return immendietly else fetch the new
definition and add in the cache file for future reference.

fixes #6
  • Loading branch information
rocktimsaikia committed Nov 12, 2022
1 parent 2c22f74 commit e56e020
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 104 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
dest_dir := /usr/local/src/cambd-cli
bin_file := /usr/bin/cambd
DIST = /usr/local/src/cambd-cli
BIN = /usr/bin/cambd

install:
cp -f cambd-cli/cambd.sh $(bin_file)
chmod +x $(bin_file)
mkdir -m 777 -p $(dest_dir)
cp -f cambd-cli/cambd.py $(dest_dir)/cambd.py
@echo "\nInstall successful"
cp -f cambd-cli/cambd.sh $(BIN)
chmod +x $(BIN)
mkdir -m 777 -p $(DIST)
cp -f cambd-cli/cambd.py $(DIST)
@echo "Install successful."

uninstall:
rm -rf $(bin_file)
Expand Down
26 changes: 10 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,23 @@ cambd neccesseery

> The above word spelling is incorrect. So cambd will suggest related words.
## Uninstall

To uninstall you can run the below command from the `cambd-cli` directory

```sh
sudo make uninstall
```

## TODO:

- [x] Add loading animation
- [x] Handle error for getting definition of words with spaces
- [x] Show only 2 examples per definition by default
- [ ] Implement a basic local caching mechanism
- [x] Implement a basic local caching mechanism
- [ ] Add flag to show all definitions. Default is 1

## LICENSE
## Caching

[MIT](./LICENSE) License © [Rocktim Saikia](https://rocktimsaikia.com) 2022
This how the basic caching feature is implemented

## CACHE
- Search definition for given word in the CLI.
- Check if the word was already looked up before in the cache file in `$HOME`.
- If yes then return it from the cache file immediently.
- If not fetch the new definition and add it to the cache file for future usecase.

- Search a given word
- Check if the word definition is available in the data json file
- If available return it
- If not scrap and add it to the data json file
## LICENSE

[MIT](./LICENSE) License © [Rocktim Saikia](https://rocktimsaikia.com) 2022
13 changes: 7 additions & 6 deletions cambd-cli/cambd.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#!/bin/env python3

import os
import re
import sys
import yaml
import json
import requests
from bs4 import BeautifulSoup
from simple_term_menu import TerminalMenu
from halo import Halo
import yaml
import re
import json

spinner = Halo(text="Loading", spinner="dots")

# Don't touch this. Unless you want to deal with permission issues in other places
cached_dictionary = os.path.expanduser("~") + "/.cambd-cache.json"

spellcheck_url = "https://dictionary.cambridge.org/spellcheck/english/?q="
definition_url = "https://dictionary.cambridge.org/dictionary/english/"
cached_dictionary = os.path.expanduser("~") + "/.cambd/dictionary.json"
# This is needed to not get detected as a bot
headers = {"User-Agent": "Mozilla/5.0"}


Expand Down Expand Up @@ -44,7 +45,7 @@ def is_cached(word: str):


def cache_it(word, definitions):
# File does not exists or file is empty
# File does not exists or file is empty;
if not os.path.exists(cached_dictionary) or os.path.getsize(cached_dictionary) == 0:
with open(cached_dictionary, "w") as ofile:
json.dump({word: definitions}, ofile)
Expand Down
75 changes: 0 additions & 75 deletions dictionary.json

This file was deleted.

0 comments on commit e56e020

Please sign in to comment.