Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mburakerman committed Dec 26, 2022
0 parents commit 268a886
Show file tree
Hide file tree
Showing 6 changed files with 987 additions and 0 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# cambridge-cli

> 🇬🇧 Terminal dictionary for English Learners
<br />

`cambridge-cli` is **English to [🇬🇧 🇹🇷 🇳🇱 🇫🇷 🇩🇪 🇮🇩 🇮🇹 🇯🇵 🇵🇱 🇵🇹 🇪🇸 🇸🇦 🇨🇿 🇩🇰 🇰🇷 🇲🇾 🇳🇴 🇷🇺 🇹🇭 🇺🇦 🇻🇳 ]** terminal dictonary.

It scrapes data from https://dictionary.cambridge.org and displays the level(if exist), definition, and example sentence for a given word.

The default language for the definition is **English**, but you can specify a different language if desired.

<br />

## 🪡 Simple Usage

Type `cambridge-cli` into your terminal and enter your word. That's it!

<img src="https://user-images.githubusercontent.com/17620102/209534443-96b33937-a20c-4f9e-be24-9279cd6e1629.gif" width="600px" alt="simple-usage">

## ⚙️ Options

`cambridge-cli` has 2 flags:

- `-language` (to set language)
- `-showAllMeanings` (to display all of the meanings of the word)

#### ⌨️ Usage:

- `cambridge-cli -language=turkish` or `cambridge-cli -language=japanese`
- `cambridge-cli -showAllMeanings`

### ⬇️ Download

`go install github.com/mburakerman/cambridge-cli@latest`

## 💪🏼 Supported Languages

- 🇹🇷 Turkish
- 🇳🇱 Dutch
- 🇫🇷 French
- 🇩🇪 German
- 🇮🇩 Indonesian
- 🇮🇹 Italian
- 🇯🇵 Japanese
- 🇵🇱 Polish
- 🇵🇹 Portuguese
- 🇪🇸 Spanish
- 🇸🇦 Arabic
- 🇨🇿 Czech
- 🇩🇰 Danish
- 🇰🇷 Korean
- 🇲🇾 Malay
- 🇳🇴 Norwegian
- 🇷🇺 Russian
- 🇹🇭 Thai
- 🇺🇦 Ukrainian
- 🇻🇳 Vietnamese

## 🕹️ Usage Examples

`-language` usage

<img src="https://user-images.githubusercontent.com/17620102/209534556-2addcabe-f317-4b1d-91ee-bf52cbf1aad1.gif" width="600px" loading="lazy" alt="usage-language">

`-showAllMeanings` usage

<img src="https://user-images.githubusercontent.com/17620102/209534599-0e5c97cf-1f77-4ac2-b5e0-763bebf36698.gif" width="600px" loading="lazy" alt="usage-showAllMeanings">
28 changes: 28 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module github.com/mburakerman/cambridge-cli

go 1.19

require (
github.com/fatih/color v1.13.0
github.com/gocolly/colly v1.2.0
)

require (
github.com/PuerkitoBio/goquery v1.8.0 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/antchfx/htmlquery v1.2.5 // indirect
github.com/antchfx/xmlquery v1.3.13 // indirect
github.com/antchfx/xpath v1.2.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/temoto/robotstxt v1.1.2 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
)
67 changes: 67 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/antchfx/htmlquery v1.2.5 h1:1lXnx46/1wtv1E/kzmH8vrfMuUKYgkdDBA9pIdMJnk4=
github.com/antchfx/htmlquery v1.2.5/go.mod h1:2MCVBzYVafPBmKbrmwB9F5xdd+IEgRY61ci2oOsOQVw=
github.com/antchfx/xmlquery v1.3.13 h1:wqhTv2BN5MzYg9rnPVtZb3IWP8kW6WV/ebAY0FCTI7Y=
github.com/antchfx/xmlquery v1.3.13/go.mod h1:3w2RvQvTz+DaT5fSgsELkSJcdNgkmg6vuXDEuhdwsPQ=
github.com/antchfx/xpath v1.2.1 h1:qhp4EW6aCOVr5XIkT+l6LJ9ck/JsUH/yyauNgTQkBF8=
github.com/antchfx/xpath v1.2.1/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gocolly/colly v1.2.0 h1:qRz9YAn8FIH0qzgNUw+HT9UN7wm1oF9OBAilwEWpyrI=
github.com/gocolly/colly v1.2.0/go.mod h1:Hof5T3ZswNVsOHYmba1u03W65HDWgpV5HifSuueE0EA=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY=
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca h1:NugYot0LIVPxTvN8n+Kvkn6TrbMyxQiuvKdEwFdR9vI=
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/temoto/robotstxt v1.1.2 h1:W2pOjSJ6SWvldyEuiFXNxz3xZ8aiWX5LbfDiOFd7Fxg=
github.com/temoto/robotstxt v1.1.2/go.mod h1:+1AmkuG3IYkh1kv0d2qEB9Le88ehNO0zwOr3ujewlOo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20200421231249-e086a090c8fd/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
52 changes: 52 additions & 0 deletions languages/languages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package languages

import (
"fmt"

"github.com/fatih/color"
)

type Language string

const (
English Language = "english"
Turkish Language = "turkish"
Dutch Language = "dutch"
French Language = "french"
German Language = "german"
Indonesian Language = "indonesian"
Italian Language = "italian"
Japanese Language = "japanese"
Polish Language = "polish"
Portuguese Language = "portuguese"
Spanish Language = "spanish"
Arabic Language = "arabic"
Czech Language = "czech"
Danish Language = "danish"
Korean Language = "korean"
Malay Language = "malay"
Norwegian Language = "norwegian"
Russian Language = "russian"
Thai Language = "thai"
Ukrainian Language = "ukrainian"
Vietnamese Language = "vietnamese"
)

var ValidLanguages = []Language{English, Turkish, Dutch, French, German,
Indonesian, Italian, Japanese, Polish, Portuguese, Spanish,
Arabic, Czech, Danish, Korean,
Malay, Norwegian, Russian, Thai, Ukrainian, Vietnamese}

func CheckSupportedLanguage(selectedLanguage string) bool {
for _, lang := range ValidLanguages {
if selectedLanguage == string(lang) {
return true
}
}

color.Red("\n😔 Invalid language ❌" + "\n ")
color.Green("💪 Supported languages: %+v\n", ValidLanguages)
fmt.Println("\n ")

return false
}
98 changes: 98 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
"strings"

"github.com/fatih/color"
"github.com/gocolly/colly"
"github.com/mburakerman/cambridge-cli/languages"
)

// flags
var language = flag.String("language", string(languages.English), "select language")
var showAllMeanings = flag.Bool("showAllMeanings", false, "display all of the meanings of the word")

func main() {
flag.Parse()

if !languages.CheckSupportedLanguage(*language) {
return
}

fmt.Print("Enter word: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
word := scanner.Text()

c := colly.NewCollector(
colly.AllowedDomains("dictionary.cambridge.org"),
)

var wordLevel string
var meanings []string
var exampleSentence string

// get english level info
c.OnHTML(".epp-xref.dxref", func(e *colly.HTMLElement) {
if e.Index == 0 && !*showAllMeanings {
wordLevel = e.Text
wordLevel = strings.TrimSpace(wordLevel)
}
})

// get meanings
var meaningSelector = ".def.ddef_d.db"
if *language != string(languages.English) {
meaningSelector = ".trans.dtrans"
}
c.OnHTML(meaningSelector, func(e *colly.HTMLElement) {
if *showAllMeanings {
results := strings.TrimSpace(e.Text)
results = strings.Replace(results, ":", "", -1)
meanings = append(meanings, results)
} else if e.Index == 0 {
result := strings.TrimSpace(e.Text)
result = strings.Replace(result, ":", "", -1)
meanings = append(meanings, result)
}
})

// get example sentence
c.OnHTML(".eg.deg", func(e *colly.HTMLElement) {
if e.Index == 0 {
result := e.Text
exampleSentence = strings.TrimSpace(result)
}
})

// print when scrapping done
yellowBackground := color.New(color.FgBlack, color.BgYellow)
c.OnScraped(func(r *colly.Response) {
if len(wordLevel) > 0 {
fmt.Print("\n📈 ")
yellowBackground.Print(" " + wordLevel + " ")
fmt.Print("\n ")
}

for i := 0; i < len(meanings); i++ {
color.Green("\n✅ " + meanings[i])
}

if len(exampleSentence) > 0 {
fmt.Println("\n📝 " + color.CyanString(exampleSentence) + "\n")
} else {
fmt.Println("")
}
})

// visit url to scrap
if *language == string(languages.English) {
c.Visit(fmt.Sprintf("https://dictionary.cambridge.org/dictionary/english/%v", word))
}
c.Visit(fmt.Sprintf("https://dictionary.cambridge.org/dictionary/english-%v/%v", *language, word))

}

0 comments on commit 268a886

Please sign in to comment.