├── .gitignore ├── LICENSE ├── Procfile ├── README.md ├── app.json ├── goodreads.py ├── main.py ├── requirements.txt └── runtime.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 W4RR10R 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: gunicorn main:api -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quotes-API 2 | > Unofficial API for fetching quotes from Goodreads 3 | 4 | 5 | [![cw projects](https://img.shields.io/badge/cwprojects-%E2%9D%A4%EF%B8%8F-green?style=style=flat-square&logo=telegram)](https://t.me/cwprojects/) 6 | [![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) 7 | 8 | 9 | ### Demo 10 | https://quotes.cwprojects.live/random 11 | 12 | [API docs](https://github.com/CW4RR10R/Quotes-API/wiki/API-Docs) 13 | 14 | #### Sample Response 15 | ```json 16 | { 17 | "author": "Lao Tzu", 18 | "image": "https://images.gr-assets.com/authors/1435903703p2/2622245.jpg", 19 | "tags": [ 20 | "courage", 21 | "deeply-loved", 22 | "love", 23 | "strength", 24 | "widely-misattributed" 25 | ], 26 | "text": "\u201cBeing deeply loved by someone gives you strength, while loving someone deeply gives you courage.\u201d \u2015 Lao Tzu" 27 | } 28 | ``` 29 | ### Deploy 30 | 31 | [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/CW4RR10R/Quotes-API/tree/main) 32 | 33 | [![Run on Repl.it](https://repl.it/badge/github/CW4RR10R/Quotes-API)](https://repl.it/github/CW4RR10R/Quotes-API) 34 | 35 | ### Installation 36 | * Cloning the repository 37 | ``` bash 38 | git clone https://github.com/CW4RR10R/Quotes-API 39 | cd Quotes-API 40 | ``` 41 | 42 | * Setting up a virtual environment 43 | ``` bash 44 | pip3 install virtualenv 45 | virtualenv quotesapi 46 | source quotesapi/bin/activate 47 | ``` 48 | 49 | * Installing the requirements and run your flask application 50 | ``` bash 51 | pip3 install -r requirements.txt 52 | python3 main.py 53 | ``` 54 | 55 | #### Contact Me 56 | [![Telegram](https://img.shields.io/badge/Telegram-blue?style=for-the-badge&logo=telegram)](https://t.me/W4RR10R/) 57 | [![Mail](https://img.shields.io/badge/Mail-grey?style=for-the-badge&logo=gmail)](mailto:arunpt@hi2.in?subject=Quotes-API) -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"Quotes-API", 3 | "description":"Unofficial API for fetching quotes from Goodreads", 4 | "keywords":[ 5 | "quotes", 6 | "flask" 7 | ], 8 | "repository":"https://github.com/CW4RR10R/Quotes-API", 9 | "success_url":"/", 10 | "buildpacks":[ 11 | { 12 | "url":"heroku/python" 13 | } 14 | ], 15 | "formation":{ 16 | "web":{ 17 | "quantity":1, 18 | "size":"free" 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /goodreads.py: -------------------------------------------------------------------------------- 1 | from bs4 import BeautifulSoup 2 | import requests 3 | import random 4 | 5 | 6 | class GoodReads: 7 | def __init__(self): 8 | pass 9 | 10 | @staticmethod 11 | def extract(url): 12 | results = [] 13 | soup = BeautifulSoup(requests.get(url).text, "html.parser") 14 | for quote in soup.find_all("div", class_= "quote"): 15 | squote = {} 16 | squote['text']= quote.find("div", {"class": "quoteText"}).text.replace('\n','').strip() 17 | squote['author'] = quote.find("span", {"class": "authorOrTitle"}).text.replace('\n','').strip() 18 | leftAlignedImage = quote.find("a", {"class": "leftAlignedImage"}) 19 | image = leftAlignedImage.img['src'] if leftAlignedImage else None 20 | squote['image'] = image 21 | if image: 22 | squote["image"] = image.replace("p2", "p8") 23 | quoteFooter = quote.find("div", {"class": "quoteFooter"}) 24 | squote['tags'] = [tag.text.strip() for tag in quoteFooter.find_all("a") if tag and "likes" not in tag.text] 25 | results.append(squote) 26 | return results 27 | 28 | @property 29 | def random(self): 30 | ran_pageNo = random.randint(1, 99) 31 | baseUrl = f"https://www.goodreads.com/quotes?format=html&mobile_xhr=1&page={ran_pageNo}" 32 | results = GoodReads.extract(baseUrl) 33 | return results[random.randint(0, len(results))] 34 | 35 | 36 | @staticmethod 37 | def search_all(search_query, pages=1): 38 | res = [] 39 | for page in range(1, pages + 1): 40 | baseUrl = f"https://www.goodreads.com/quotes/search?commit=Search&page={page}&q={search_query.replace(' ', '+')}&utf8=%E2%9C%93" 41 | results = GoodReads.extract(baseUrl) 42 | res.extend(results) 43 | return res 44 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, jsonify 2 | from goodreads import GoodReads 3 | 4 | api = Flask(__name__) 5 | 6 | @api.route('/') 7 | def index(): 8 | res = { 9 | 'status': 200, 10 | 'about': 'Quotes api by W4RR10R', 11 | 'usage': [ 12 | '/random', 13 | '/search/query/no of pages' 14 | ] 15 | } 16 | return jsonify(res) 17 | 18 | @api.route('/random') 19 | def random_quotes(): 20 | return jsonify(GoodReads().random) 21 | 22 | @api.route('/search') 23 | def search(): 24 | res = { 25 | "error": { 26 | 'status': 400, 27 | 'detail': 'No search query found' 28 | } 29 | } 30 | return jsonify(res) 31 | 32 | @api.route('/search/', defaults= {"pages": 1}) 33 | @api.route('/search//') 34 | def search_quotes(query, pages): 35 | results = GoodReads.search_all(query, int(pages)) 36 | return jsonify(results) 37 | 38 | if __name__ == '__main__': 39 | api.run(host="0.0.0.0", port=5000, debug=True) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | flask 2 | gunicorn 3 | requests 4 | beautifulsoup4 5 | -------------------------------------------------------------------------------- /runtime.txt: -------------------------------------------------------------------------------- 1 | python-3.9.0 --------------------------------------------------------------------------------