Skip to content

Commit

Permalink
marvel api
Browse files Browse the repository at this point in the history
  • Loading branch information
komal-agarwal5 committed Jul 24, 2024
1 parent 96edefb commit 72635a4
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Existing_API_Collection/Marvel_API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Marvel API App</title>
<!-- Google Font -->
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap" rel="stylesheet" />
<!-- Stylesheet -->
<link rel="stylesheet" href="style.css" />
</head>

<body>
<div class="container">
<div class="input-container">
<input type="text" class="input" value="iron man" id="input-box" />
<button class="button" id="submit-button">Submit</button>
</div>
<div class="list"></div>
<div class="display-container" id="show-container"></div>
</div>
<!-- Script -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<script src="script.js"></script>
</body>

</html>
116 changes: 116 additions & 0 deletions Existing_API_Collection/Marvel_API/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
let input = document.getElementById("input-box");
let button = document.getElementById("submit-button");
let showContainer = document.getElementById("show-container");
let listContainer = document.querySelector(".list");

// Replace with your actual public and private keys
const publicKey = '';
const privateKey = '';

let ts = "1681802982683";
const hash = CryptoJS.MD5(ts + privateKey + publicKey).toString();

function displayWords(value) {
input.value = value;
removeElements();
}

function removeElements() {
listContainer.innerHTML = "";
}

input.addEventListener("keyup", async () => {
removeElements();
if (input.value.length < 4) {
return false;
}

const url = `https://gateway.marvel.com:443/v1/public/characters?ts=${ts}&apikey=${publicKey}&hash=${hash}&nameStartsWith=${input.value}`;

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const jsonData = await response.json();

jsonData.data["results"].forEach((result) => {
let name = result.name;
let div = document.createElement("div");
div.style.cursor = "pointer";
div.classList.add("autocomplete-items");
div.setAttribute("onclick", `displayWords('${name}')`);
let word = `<b>${name.substr(0, input.value.length)}</b>${name.substr(input.value.length)}`;
div.innerHTML = `<p class="item">${word}</p>`;
listContainer.appendChild(div);
});
} catch (error) {
console.error('Error fetching data:', error);
}
});

button.addEventListener("click", async () => {
if (input.value.trim().length < 1) {
alert("Input cannot be blank");
return;
}
showContainer.innerHTML = "";
const url = `https://gateway.marvel.com:443/v1/public/characters?ts=${ts}&apikey=${publicKey}&hash=${hash}&name=${input.value}`;

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const jsonData = await response.json();

jsonData.data["results"].forEach((element) => {
showContainer.innerHTML += `
<div class="card-container">
<div class="container-character-image">
<img src="${element.thumbnail.path}.${element.thumbnail.extension}" alt="${element.name}" />
</div>
<div class="character-name">${element.name}</div>
<div class="character-description">${element.description}</div>
</div>`;
});
} catch (error) {
console.error('Error fetching data:', error);
}
});

window.onload = async () => {
if (input.value.trim().length > 0) {
await getResult();
}
};

async function getResult() {
if (input.value.trim().length < 1) {
alert("Input cannot be blank");
return;
}
showContainer.innerHTML = "";
const url = `https://gateway.marvel.com:443/v1/public/characters?ts=${ts}&apikey=${publicKey}&hash=${hash}&name=${input.value}`;

try {
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const jsonData = await response.json();

jsonData.data["results"].forEach((element) => {
showContainer.innerHTML += `
<div class="card-container">
<div class="container-character-image">
<img src="${element.thumbnail.path}.${element.thumbnail.extension}" alt="${element.name}" />
</div>
<div class="character-name">${element.name}</div>
<div class="character-description">${element.description}</div>
</div>`;
});
} catch (error) {
console.error('Error fetching data:', error);
}
}
101 changes: 101 additions & 0 deletions Existing_API_Collection/Marvel_API/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}

body {
background-color: #313030;
}

.container {
background-color: #ee130b;
width: 90%;
max-width: 500px;
margin: 0 auto;
padding: 3em 2em;
position: absolute;
transform: translateX(-50%);
left: 50%;
right: 50%;
top: 1em;
border-radius: 1em;
}

.input-container {
display: grid;
grid-template-columns: 9fr 3fr;
gap: 1em;
}

input {
padding: 1em 0.5em;
background-color: #3a3939;
color: #ffffff;
border: none;
border-radius: 0.5em;
outline: none;
}

button {
outline: none;
border: none;
background-color: #f8f5f6;
color: black;
border-radius: 0.5em;
}

.display-container {
padding: 1em;
}

.container-character-image {
background-color: #ffffff;
padding: 0.5em;
height: 9.37em;
width: 9.37em;
display: block;
margin: auto;
border-radius: 50%;
}

.container-character-image img {
width: 100%;
position: relative;
width: block;
border-radius: 50%;
}

.character-name {
padding: 0.5em 0 0.8em 0;
text-align: center;
color: #ffffff;
text-transform: uppercase;
font-size: 1.2em;
font-weight: 600;
}

.character-description {
text-align: justify;
color: wheat;
line-height: 2em;
font-weight: 300;
}

.list {
position: absolute;
width: 64%;
background-color: #555;
color: #ffffff;
z-index: 1;
}

.autocomplete-items {
padding: 0.5em;
}

.autocomplete-items:hover {
background-color: #ddd;
color: #171821;
}

0 comments on commit 72635a4

Please sign in to comment.