From c9ecbb88bfab85d5149d7b7681169d4855ebac4a Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me> Date: Tue, 1 Dec 2020 10:25:23 -0500 Subject: [PATCH] update formatting and convinience * Add extract-install make target * Make README more descriptive * Change all occurences of 'fortune' to 'phrase' * Update comments at start of files --- Makefile | 7 +++++++ README.md | 13 ++++++++----- extract.py | 4 +++- phrases.py | 33 ++++++++++++++++++--------------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index ac6d7ed..9f3520b 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,13 @@ install: cp phrases.py /usr/local/bin cp phrases /usr/share/phrases/ +extract-install: + ./extract.py + mkdir -p /usr/local/bin + mkdir -p /usr/share/phrases/ + cp phrases.py /usr/local/bin + cp phrases /usr/share/phrases/ + uninstall: rm /usr/local/bin/phrases.py rm -r /usr/share/phrases/ diff --git a/README.md b/README.md index 6541458..66c0625 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,16 @@ # phrases -Get latin famous phrases in your terminal! +Get Latin famous phrases in your terminal! ## Source -The Wikipedia page "[List of Latin phrases (full)](https://en.wikipedia.org/wiki/List_of_Latin_phrases_(full))". See the references [here](https://en.wikipedia.org/wiki/List_of_Latin_phrases_(full)#References) for more information +The Wikipedia page "[List of Latin phrases (full)](https://en.wikipedia.org/wiki/List_of_Latin_phrases_(full))". See the references [here](https://en.wikipedia.org/wiki/List_of_Latin_phrases_(full)#References) for more information. There are currently 2225 phrases in the database ## Installation -`sudo make` +`sudo make` to install. Requires Python 3. +`sudo make uninstall` to uninstall completely. -If you want to run the `extract.py` script, then you need BeautifulSoup +If you want to run the `extract.py` script, then you need the BeautifulSoup Python module. This is not needed for installation however, as this repository has a pre-extracted list of phrases [here](phrases). If the repository has not been updated in some time, then it may be prudent to run it again to get a fresh list. You can do this at install-time with `sudo make extract-install`. As this is run as root, make sure to install BeautifulSoup globally. ## License -Phrases is MIT Licensed by [Armaan Bhojwani](https://armaanb.net), 2020. In part built on [fortune](https://github.com/bmc/fortune) by Brian M. Clapper +Phrases is MIT Licensed by [Armaan Bhojwani](https://armaanb.net), 2020. See the LICENSE file for more information. + +In part built on [fortune](https://github.com/bmc/fortune) by Brian M. Clapper which is BSD licensed diff --git a/extract.py b/extract.py index fa74530..65f9914 100755 --- a/extract.py +++ b/extract.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 -# Tool to extract famous phrases from wikipedia +# Extract Latin famous phrases from wikipedia +# Armaan Bhojwani 2020 + from bs4 import BeautifulSoup import requests diff --git a/phrases.py b/phrases.py index 93f8a2f..bcf3f73 100755 --- a/phrases.py +++ b/phrases.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Display famous phrases in the terminal +# Display Latin famous phrases in the terminal # Armaan Bhojwani 2020 import random @@ -7,8 +7,8 @@ import os import sys import re -def _random_int(start, end): # Use system random if available, otherwise, use Python's +def _random_int(start, end): try: r = random.SystemRandom() except: @@ -16,19 +16,20 @@ def _random_int(start, end): return r.randint(start, end) -def _read_fortunes(fortune_file): - f = open(fortune_file, 'r') +# Create list of phrases from phrase file +def _read_phrases(phrase_file): + f = open(phrase_file, 'r') contents = f.read() lines = [line.rstrip() for line in contents.split('\n')] delim = re.compile(r'^%$') - fortunes = [] + phrases = [] cur = [] def save_if_nonempty(buf): - fortune = '\n'.join(buf) - if fortune.strip(): - fortunes.append(fortune) + phrase = '\n'.join(buf) + if phrase.strip(): + phrases.append(phrase) for line in lines: if delim.match(line): @@ -41,15 +42,17 @@ def _read_fortunes(fortune_file): if cur: save_if_nonempty(cur) - return fortunes + return phrases -def get_random_fortune(fortune_file): - fortunes = list(_read_fortunes(fortune_file)) - randomRecord = _random_int(0, len(fortunes) - 1) - randFortune = fortunes[randomRecord] - return randFortune.partition('\n')[0] +# Return a random phrase from the phrases list +def get_random_phrase(phrase_file): + phrases = list(_read_phrases(phrase_file)) + randomRecord = _random_int(0, len(phrases) - 1) + randphrase = phrases[randomRecord] + return randphrase.partition('\n')[0] +# Main program def main(): - print(get_random_fortune("/usr/share/phrases/phrases")) + print(get_random_phrase("/usr/share/phrases/phrases")) main() -- 2.39.2