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

Marvel API #341

Open
wants to merge 6 commits into
base: master
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
49 changes: 49 additions & 0 deletions Existing_API_Collection/Marvel_API/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Marvel API App

Marvel API App is a simple web application that allows users to search for Marvel characters and view detailed information about them using the Marvel API.

## Features

- Search Marvel Characters: Type in the name of a Marvel character to search for matching results.
- Autocomplete Suggestions: Get autocomplete suggestions as you type in the search box.
- Character Details: View detailed information about each character, including their name, image, and description.

## Screenshots

![Marvel API App Screenshot](./image.png)

## Technologies Used

- HTML
- CSS
- JavaScript
- [Marvel API](https://developer.marvel.com/)

## Getting Started

### Prerequisites

To run this project locally, you will need:

- A modern web browser (Chrome, Firefox, Safari, etc.)
- Internet connection

### Installation

1. Clone the repository:

```sh
git clone https://github.com/your-username/marvel-api-app.git
cd marvel-api-app
Open index.html in your browser to view the application.
```

### Configuration

- Sign up for an API key at Marvel Developer Portal.
- Get your public and private keys from your Marvel Developer account.
- Replace the placeholder keys in the script.js file with your actual keys:
```
const publicKey = 'your_public_key';
const privateKey = 'your_private_key';
```
Binary file added Existing_API_Collection/Marvel_API/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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;
}