Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a readline app with replic #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -0,0 +1,7 @@
LISP?=sbcl

build:
$(LISP) --non-interactive \
--load lyrics.asd \
--eval '(ql:quickload :lyrics)' \
--eval '(asdf:make :lyrics)'
@@ -1,6 +1,10 @@
# lyrics

Search song lyrics or, the other way around, search songs from lyrics.

Available as a CL library and as a terminal application.


# Installation

```bash
@@ -18,6 +22,16 @@ git clone https://github.com/mihaiolteanu/lyrics ~/quicklisp/local-projects/lyri
sudo pacman -S sqlite3
```

Optionally, build the terminal app:

```bash
make build
```

This produces a `lyrics` binary with the two commands `lyrics` and
`search-song` available.


# Usage

```common-lisp
@@ -84,6 +98,10 @@ If the song cannot be found on any of the websites, `lyrics` returns nil. Otherw
`lyrics` returns the song lyrics and saves them in the database from where they
will be fetched on the next call. The `lyrics` function is memoized.

The readline application is built quite automatically with the
[replic](https://github.com/vindarel/replic/) library.


## Authors
Copyright (c) 2019 [Mihai Olteanu](www.mihaiolteanu.me)

@@ -12,7 +12,13 @@
:sqlite
:alexandria
:bordeaux-threads
:defmemo)
:defmemo
:replic)
:serial t

:build-operation "program-op"
:build-pathname "lyrics"
:entry-point "lyrics::main"

:components ((:file "package")
(:file "lyrics")))
@@ -1,9 +1,11 @@
;;;; lyrics.lisp
(in-package #:lyrics)

(defparameter *db* nil)

(defun setup-db ()
"Create a sqlite table in ~/ if one does not already exist."
(defparameter *db*
(setf *db*
(connect (merge-pathnames "cl-lyrics.db"
(xdg-config-home))))
(execute-non-query *db*
@@ -17,10 +19,9 @@
COLLATE NOCASE,
lyrics TEXT COLLATE NOCASE);"))

(setup-db)

(defstruct website
;; website name, only used for documentation purposes
;; website name, only used for documentation purposes
(name)
;; Template used to construct the actual url. It contains the artist-name and
;; song-name string, respectively, which need to be replaced with the user
@@ -160,8 +161,10 @@ name. If the lyrics are not in the db, try and extract them from one of the
supported lyrics websites. If found, save the lyrics the db and return them. If
not found, return nil."
(declare (string artist song))
(unless *db*
(setup-db))
(if-let ((lyrics (lyrics-from-db artist song)))
lyrics ;already in db
lyrics ;already in db
(dolist (website
;; Try to minimize the chance of getting banned; try a different
;; order of sites on every request.
@@ -196,3 +199,13 @@ the thread that is started for the request."
;; at a future date; take your time; better to be safe than being banned
;; for making too many requests in a short time.
(sleep (random-elt '(1 2 3)))))))

(defun main ()
(setup-db)

(setf replic:*prompt* "lyrics> ")
(replic.completion:functions-to-commands :replic.base)
(replic.completion:functions-to-commands :lyrics)
(replic:autoprint-results-from :lyrics)

(replic:repl))
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.