Skip to content

Instantly share code, notes, and snippets.

@kshitij10496
Created December 8, 2017 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshitij10496/b6e0da2ea9f9c207a926ade9d824c688 to your computer and use it in GitHub Desktop.
Save kshitij10496/b6e0da2ea9f9c207a926ade9d824c688 to your computer and use it in GitHub Desktop.
Script to print random quotes from/on Richard Feynman
#!env/bin/python3
# encoding: utf-8
"""
This scripts prints a random quote from WikiQuote page of Richard Feynman.
$ python3 feynmanize.py
!!! FEYMANIZE !!!
================================================================================
||The fact that you are not sure means that it is possible that there is another
||way someday. lecture II: "The Uncertainty of Values"
"""
import random
import requests
from bs4 import BeautifulSoup
import textwrap
def feynmanize():
print("\n\t{}!!! FEYMANIZE !!!{}".format(' '*28, ' '*28))
print("\t{}".format("="*76))
URL = 'https://en.wikiquote.org/w/api.php'
params = {
'action': 'parse',
'format': 'json',
'page': 'Richard_Feynman'
}
response = requests.get(URL, params)
if response.status_code != 200:
print('Cannot Feynmanize!')
soup = BeautifulSoup(response.text, "html5lib")
quotes = soup.find_all('ul')
for item in quotes:
subsection = item.find_all('li')
if len(subsection) > 4:
quotes.remove(item)
elif len(subsection) == 1:
quotes.remove(item)
## TODO: Find a way to remove only references
quote = random.choice(quotes)
subsection = quote.find('li')
b = bytes(subsection.text, encoding='ascii')
c = b.decode('unicode-escape')
print('\n\t|| ' + '\n\t|| '.join(textwrap.wrap(c, width=73)))
if __name__ == '__main__':
feynmanize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment