├── requirements.txt ├── genius.py ├── app.py ├── README.md ├── test_app.py └── .gitignore /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rg3915/cft-challenge/master/requirements.txt -------------------------------------------------------------------------------- /genius.py: -------------------------------------------------------------------------------- 1 | import os 2 | from dotenv import load_dotenv 3 | from lyricsgenius import Genius 4 | from typing import List 5 | 6 | load_dotenv() 7 | 8 | 9 | def get_popularity_songs(artist_name: str, max_songs: int, sort="popularity"): 10 | genius = Genius(os.environ["TOKEN"]) 11 | artist = genius.search_artist(artist_name, max_songs=max_songs, sort=sort) 12 | return artist.songs 13 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, jsonify 2 | from genius import get_popularity_songs 3 | 4 | 5 | def artist(name): 6 | # artist route 7 | songs = get_popularity_songs(name, max_songs=10) 8 | content = [ 9 | { 10 | "title": song.title, 11 | "year": song.year, 12 | "album": song.album 13 | } 14 | for song in songs 15 | ] 16 | return jsonify(content) 17 | 18 | 19 | def create_app(): 20 | app = Flask(__name__) 21 | app.add_url_rule("/artist/", 'artist', artist) 22 | 23 | return app 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cft-challenge 2 | 3 | ## Teste 4 | 5 | Criar uma API REST em Python (Django ou Flask) que consuma a API do [Genius](https://docs.genius.com/#/getting-started-h1) e que dado um artista, liste as 10 músicas mais populares do artista pesquisado. 6 | 7 | 8 | ## Este projeto foi feito com: 9 | 10 | * Python 3.7 11 | * Flask==1.1.1 12 | 13 | ## Como rodar o projeto? 14 | 15 | * Clone esse repositório. 16 | * Crie um virtualenv com Python 3. 17 | * Ative o virtualenv. 18 | * Instale as dependências. 19 | * Rode os testes. 20 | 21 | ``` 22 | git clone https://github.com/rg3915/cft-challenge.git 23 | cd cft-challenge 24 | python -m venv .venv 25 | source .venv/bin/activate 26 | pip install -r requirements.txt 27 | ``` 28 | 29 | ## Variáveis de ambientes: 30 | 31 | ``` 32 | TOKEN=SEU_TOKEN_DO_GENIUS 33 | FLASK_APP=app:create_app 34 | ``` 35 | 36 | ## Rodando os testes 37 | 38 | ``` 39 | python -m unittest test_app.py 40 | ``` 41 | 42 | ## Rodando a app 43 | 44 | ``` 45 | flask run 46 | ``` 47 | 48 | Em outro terminal faça: 49 | 50 | ``` 51 | http get http://127.0.0.1:5000/artist/Metallica 52 | ``` 53 | 54 | ou 55 | 56 | ``` 57 | curl http://127.0.0.1:5000/artist/Metallica 58 | ``` 59 | 60 | ou navegue pelo browser em 61 | 62 | ``` 63 | http://127.0.0.1:5000/artist/Metallica 64 | ``` 65 | 66 | -------------------------------------------------------------------------------- /test_app.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from collections import namedtuple 3 | from unittest import mock 4 | from app import create_app 5 | 6 | Song = namedtuple("Song", ["title", "year", "album"]) 7 | 8 | 9 | class ArtistTestCase(unittest.TestCase): 10 | 11 | def setUp(self) -> None: 12 | self.app = create_app() 13 | self.app.config["TESTING"] = True 14 | self.context = self.app.app_context() 15 | self.context.push() 16 | self.client = self.app.test_client() 17 | 18 | @mock.patch("app.get_popularity_songs") 19 | def test_get_artist_popularity_songs(self, song_mocked): 20 | data = [ 21 | Song( 22 | title="Nothing Else Matters", 23 | year="1991-08-12", 24 | album="Metallica" 25 | ) 26 | ] 27 | song_mocked.return_value = data 28 | 29 | response = self.client.get("/artist/Metallica") 30 | self.assertListEqual( 31 | list(response.get_json()), 32 | [ 33 | { 34 | 'title': data[0].title, 35 | 'year': data[0].year, 36 | 'album': data[0].album, 37 | } 38 | ] 39 | ) 40 | 41 | def tearDown(self) -> None: 42 | self.context.pop() 43 | -------------------------------------------------------------------------------- /.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 | --------------------------------------------------------------------------------