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

Added lyrics app project #473

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Web Dev Projects_or_Portfolios/lyrics app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="lyrics.css"/>
<title>LyricsSearch</title>
</head>
<body>
<header>
<h1>Lyrics Search</h1>

<form id="form">
<input
type="text"
id="search"
placeholder="Enter artist or song name"
/>
<button>Search</button>
</form>
</header>

<div id="result" class="container">
<p>Results will be displayed here</p>
</div>

<div id="more" class="container centered"></div>

<script src="lyrics.js"></script>
</body>
</html>

126 changes: 126 additions & 0 deletions Web Dev Projects_or_Portfolios/lyrics app/lyrics.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
* {
box-sizing: border-box;
}

body {
background-color: #fff;
margin: 0;
}

button {
cursor: pointer;
}

button:active {
transform: scale(0.95);
}

input:focus,
button:focus {
outline: none;
}

header {
background-image: url("https://images.unsplash.com/photo-1510915361894-db8b60106cb1?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80");
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 100px 0;
position: relative;
}

header::after {
content: "";
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: rgba(0, 0, 0, 0.2);
}

header * {
z-index: 1;
}

header h1 {
margin: 0 0 30px;
}

form {
position: relative;
width: 500px;
max-width: 100%;
}

form input {
border: 0;
border-radius: 50px;
font-size: 16px;
padding: 15px 30px;
width: 100%;
}

form button {
position: absolute;
top: 2px;
right: 2px;
background-color: black;
border: 0;
border-radius: 50px;
color: #fff;
font-size: 16px;
padding: 13px 30px;
}

.btn {
background-color: #8d56fd;
border: 0;
border-radius: 10px;
color: #fff;
padding: 10px 10px;
}

ul.songs {
list-style-type: none;
padding: 0;
}

ul.songs li {
display: flex;
align-items: center;
justify-content: space-between;
margin: 10px 0;
}

.container {
margin: 30px auto;
max-width: 100%;
width: 700px;
text-align: center;
font-size: 21px;
}

.container h2 {
font-weight: 300;
}

.container p {
text-align: center;
}

.centered {
display: flex;
justify-content: center;
}

.centered button {
transform: scale(1.3);
margin: 15px;
}

107 changes: 107 additions & 0 deletions Web Dev Projects_or_Portfolios/lyrics app/lyrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const form = document.getElementById("form");
const search = document.getElementById("search");
const result = document.getElementById("result");
const more = document.getElementById("more");

const apiURL = "https://api.lyrics.ovh";

async function searchSongs(term) {
const res = await fetch(`${apiURL}/suggest/${term}`);
const data = await res.json();

showData(data);
}

function showData(data) {
if (!data && window.realData) data = window.realData;
console.log(data.data.length);
if (data.data.length === 0) {
window.dataLength = data.data.length;
return (result.innerHTML = `
<p>No Results Found.</p>
<p>Try something different</p>
`);
} else {
window.realData = data;
result.innerHTML = `
<ul class="songs">
${data.data
.map(
(song) => `<li>
<img src=${song.album.cover} /><span><strong>${song.artist.name}</strong> - ${song.title}</span>
<button class="btn" data-artist="${song.artist.name}" data-name = 'button' data-songtitle="${song.title}">Lyrics</button>
</li>`
)
.join("")}
</ul>
`;

if (data.prev || data.next) {
more.innerHTML = `
${
data.prev
? `<button class="btn" onclick="getMoreSongs('${data.prev}')">Prev</button>`
: ""
}
${
data.next
? `<button class="btn" onclick="getMoreSongs('${data.next}')">Next</button>`
: ""
}
`;
} else {
more.innerHTML = "";
}
}
}

async function getMoreSongs(url) {
const res = await fetch(`https://cors-anywhere.herokuapp.com/${url}`);
const data = await res.json();

showData(data);
}

async function getLyrics(artist, songTitle) {
const res = await fetch(`${apiURL}/v1/${artist}/${songTitle}`);
const data = await res.json();

if (data.error) {
result.innerHTML = data.error;
} else {
const lyrics = data.lyrics.replace(/(\r\n|\r|\n)/g, "<br>");
result.innerHTML = `
<h2><strong>${artist}</strong> - ${songTitle}</h2>
<span>${lyrics}</span>
</br></br>
<button class="btn" onclick="showData()">Back</button>
`;
}

more.innerHTML = "";
}

form.addEventListener("submit", (e) => {
e.preventDefault();

const searchTerm = search.value.trim();

if (!searchTerm) {
alert("Please type in a search term");
} else {
searchSongs(searchTerm);
}
});

result.addEventListener("click", (e) => {
const clickedEl = e.target;
console.log(clickedEl);
const property = clickedEl.getAttribute("data-name");

if (property === "button") {
const artist = clickedEl.getAttribute("data-artist");
const songTitle = clickedEl.getAttribute("data-songtitle");

getLyrics(artist, songTitle);
}
});
Empty file.