Initial commit

This commit is contained in:
Caileb 2025-06-14 13:09:17 -05:00
commit 71667d81e4
3 changed files with 414 additions and 0 deletions

33
README.md Normal file
View file

@ -0,0 +1,33 @@
# MIME Type Reference
This project provides a quick and easy way to browse and search for official IANA-registered MIME types. It consists of two main parts:
1. A Python script (`fetcher.py`) that downloads the latest MIME type registration data from the official IANA website.
2. A self-contained HTML file (`index.html`) that displays the data in a clean, searchable, and user-friendly interface.
## Features
- **Up-to-date Data**: Fetches data directly from IANA's official sources.
- **Categorized View**: Organizes MIME types by category (text, image, application, etc.).
- **Obsolete/Deprecated Handling**: Separately categorizes and displays obsolete or deprecated types for clarity.
- **Live Search**: Instantly filter all categories with a single search bar.
- **Zero Dependencies**: The `index.html` file is completely self-contained and requires no external libraries or frameworks.
## How to Use
### 1. Fetch the Data
To get the latest MIME types, run the Python script. This will download the data from IANA and create a `mime_types.json` file in the same directory.
You will need `aiohttp` to run the script:
```bash
pip install aiohttp
python fetcher.py
```
### 2. View the Data
Simply open the `index.html` file in any modern web browser. It will automatically load the `mime_types.json` file and display the data.
You can now browse the categories or use the search bar to find specific MIME types.

102
fetcher.py Normal file
View file

@ -0,0 +1,102 @@
import asyncio
import aiohttp
import csv
import json
import re
from io import StringIO
from collections import OrderedDict, defaultdict
URLS = OrderedDict([
('text', 'https://www.iana.org/assignments/media-types/text.csv'),
('image', 'https://www.iana.org/assignments/media-types/image.csv'),
('audio', 'https://www.iana.org/assignments/media-types/audio.csv'),
('video', 'https://www.iana.org/assignments/media-types/video.csv'),
('application', 'https://www.iana.org/assignments/media-types/application.csv'),
('font', 'https://www.iana.org/assignments/media-types/font.csv'),
('model', 'https://www.iana.org/assignments/media-types/model.csv'),
('multipart', 'https://www.iana.org/assignments/media-types/multipart.csv'),
('message', 'https://www.iana.org/assignments/media-types/message.csv'),
('haptics', 'https://www.iana.org/assignments/media-types/haptics.csv')
])
OUTPUT_JSON_FILE = "mime_types.json"
REQUEST_TIMEOUT_SECONDS = 20
USER_AGENT = "MimeTypeFetcher/1.0.0"
OBSOLETE_DEPRECATED_PATTERN = re.compile(
r"\(obsolete(d)?([^\)]*)\)|"
r"\bobsolete(d)?\b|"
r"\(deprecated([^\)]*)\)|"
r"\bdeprecated\b",
re.IGNORECASE
)
def is_obsolete_or_deprecated(name_str):
return bool(OBSOLETE_DEPRECATED_PATTERN.search(name_str))
async def fetch_category_data(session, category_key, url):
print(f"Fetching: {category_key}...")
headers = {"User-Agent": USER_AGENT}
try:
async with session.get(url, timeout=REQUEST_TIMEOUT_SECONDS, headers=headers) as response:
response.raise_for_status()
text_content = await response.text(encoding="utf-8")
reader = csv.DictReader(StringIO(text_content))
return category_key, list(reader)
except Exception as e:
print(f"Error for {category_key} ({url}): {type(e).__name__} - {e}")
return category_key, []
async def process_all_mime_types():
final_data = OrderedDict()
# Pre-initialize obsolete_data to preserve category order
obsolete_data = OrderedDict((key, []) for key in URLS.keys())
print("Starting MIME type download...\n")
async with aiohttp.ClientSession() as session:
tasks = [fetch_category_data(session, key, url) for key, url in URLS.items()]
results = await asyncio.gather(*tasks)
print("\nProcessing downloaded data...")
total_processed_entries = 0
for category_key, raw_rows in results:
active_entries = []
if not raw_rows:
final_data[category_key] = []
continue
for row in raw_rows:
name = row.get('Name', '').strip()
template = row.get('Template', '').strip()
if name and template:
entry = {'name': name, 'template': template}
if is_obsolete_or_deprecated(name):
obsolete_data[category_key].append(entry)
else:
active_entries.append(entry)
final_data[category_key] = active_entries
total_processed_entries += len(active_entries)
print(f"Processed {len(active_entries)} active types for {category_key}.")
# Filter out obsolete categories with no entries and add to final_data
final_obsolete_data = OrderedDict([(k, v) for k, v in obsolete_data.items() if v])
if final_obsolete_data:
final_data["obsolete_deprecated"] = final_obsolete_data
obsolete_count = sum(len(v) for v in final_obsolete_data.values())
total_processed_entries += obsolete_count
print(f"Categorized {obsolete_count} obsolete/deprecated types by original category.")
print(f"\nTotal entries processed: {total_processed_entries}")
try:
with open(OUTPUT_JSON_FILE, 'w', encoding='utf-8') as f:
json.dump(final_data, f, indent=2, ensure_ascii=False)
print(f"Data saved to {OUTPUT_JSON_FILE}")
except IOError as e:
print(f"Error writing JSON: {e}")
if __name__ == "__main__":
asyncio.run(process_all_mime_types())

279
index.html Normal file
View file

@ -0,0 +1,279 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MIME Type Reference</title>
<style>
body, html {
margin: 0; padding: 0; font-family: "Helvetica Neue", Arial, sans-serif;
background-color: #f4f7f6; color: #333; font-size: 16px;
}
.page-header {
background-color: #2a3f54; color: #e1e1e1; padding: 20px 30px;
text-align: center; border-bottom: 3px solid #1a2c3a;
}
.page-header h1 { margin: 0; font-size: 2em; font-weight: 300; letter-spacing: 1px; }
.content-wrapper { max-width: 900px; margin: 30px auto; padding: 0 20px; }
.search-container {
margin-bottom: 30px; padding: 15px; background-color: #fff;
border-radius: 6px; box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.search-input {
width: 100%; padding: 12px 15px; font-size: 1.1em;
border: 1px solid #dce4ec; border-radius: 4px; box-sizing: border-box;
transition: border-color 0.3s, box-shadow 0.3s;
}
.search-input:focus {
outline: none; border-color: #5c97bf;
box-shadow: 0 0 0 3px rgba(92,151,191,0.2);
}
.status-text {
text-align: center; font-size: 1.2em; color: #777; padding: 40px 20px;
background-color: #fff; border-radius: 6px; margin-top: 20px;
}
.status-text.error {
color: #c0392b; background-color: #fdecea; border: 1px solid #f5c6cb;
}
.mime-category, .obsolete-main-section { /* Shared styling for main blocks */
margin-bottom: 35px; background-color: #fff; border-radius: 6px;
box-shadow: 0 2px 8px rgba(0,0,0,0.08); overflow: hidden;
}
.category-title-bar, .obsolete-main-title-bar { /* Shared for main titles */
background-color: #e8eff5; color: #3a506b; padding: 12px 20px;
border-bottom: 1px solid #dce4ec;
}
.obsolete-main-section .obsolete-main-title-bar { /* Specific for main obsolete title */
background-color: #fadbd8; color: #78281f; border-bottom-color: #f1c1bc;
}
.category-title-bar h2, .obsolete-main-title-bar h2 { margin: 0; font-size: 1.5em; font-weight: 400; }
/* Styling for sub-categories within Obsolete section */
.obsolete-subcategory {
margin: 10px 20px; /* Indent subcategories */
border: 1px solid #eef2f7; /* Lighter border for sub-items */
border-radius: 4px;
}
.obsolete-subcategory-header {
background-color: #f8f9fa; /* Very light grey for sub-headers */
color: #495057;
padding: 8px 15px;
border-bottom: 1px solid #eef2f7;
cursor: pointer;
display: flex;
justify-content: space-between;
align-items: center;
}
.obsolete-subcategory-header:hover { background-color: #f1f3f5; }
.obsolete-subcategory-header h3 { font-size: 1.1em; font-weight: 500; margin:0; }
.toggle-arrow { font-size: 0.9em; transition: transform 0.2s; }
.obsolete-subcategory.open .toggle-arrow { transform: rotate(90deg); }
.mime-item-list, .obsolete-subcategory-list { padding: 10px 20px 20px 20px; }
.obsolete-subcategory-list { display: none; padding: 10px 15px; } /* Collapsed by default */
.obsolete-subcategory.open .obsolete-subcategory-list { display: block; }
.mime-entry { padding: 12px 0; border-bottom: 1px solid #f0f3f5; }
.mime-entry:last-child { border-bottom: none; }
.mime-name {
font-size: 1.1em; font-weight: 500; color: #2c3e50; margin-bottom: 4px;
}
.mime-template {
font-family: "SFMono-Regular", Consolas, Menlo, Courier, monospace;
font-size: 0.95em; color: #555; background-color: #f8f9fa;
padding: 3px 6px; border-radius: 3px; display: inline-block;
word-break: break-all;
}
.hidden { display: none !important; }
@media (max-width: 768px) {
.page-header h1 { font-size: 1.6em; }
.content-wrapper { margin-top: 20px; padding: 0 15px; }
.category-title-bar h2, .obsolete-main-title-bar h2 { font-size: 1.3em; }
.obsolete-subcategory-header h3 { font-size: 1em; }
.mime-name { font-size: 1em; }
}
</style>
</head>
<body>
<header class="page-header"><h1>MIME Type Reference</h1></header>
<div class="content-wrapper">
<div class="search-container">
<input type="text" id="searchInput" class="search-input" placeholder="Search types (e.g., json, text/plain)...">
</div>
<div id="mimeDisplayArea">
<p id="loadingIndicator" class="status-text">Loading MIME types...</p>
<p id="noResultsIndicator" class="status-text hidden">No matching MIME types found.</p>
</div>
</div>
<script async>
document.addEventListener('DOMContentLoaded', () => {
const displayArea = document.getElementById('mimeDisplayArea');
const searchInput = document.getElementById('searchInput');
const loadingIndicator = document.getElementById('loadingIndicator');
const noResultsIndicator = document.getElementById('noResultsIndicator');
let searchTimeout;
let allMimeData = {};
const MAIN_CATEGORY_ORDER = [
"text", "image", "audio", "video", "application",
"font", "model", "multipart", "message", "haptics"
];
const OBSOLETE_DATA_KEY = "obsolete_deprecated";
function createMimeEntryHTML(type) {
return `
<div class="mime-entry">
<div class="mime-name">${type.name}</div>
<div class="mime-template">${type.template}</div>
</div>`;
}
function createCategorySectionDOM(categoryKey, categoryTitle, typesToDisplay) {
const sectionDiv = document.createElement('div');
sectionDiv.className = 'mime-category';
sectionDiv.innerHTML = `
<div class="category-title-bar">
<h2>${categoryTitle} (${typesToDisplay.length})</h2>
</div>
<div class="mime-item-list">
${typesToDisplay.map(createMimeEntryHTML).join('')}
</div>`;
return sectionDiv;
}
function createObsoleteSectionDOM(obsoleteData, searchTerm) {
const mainObsoleteSection = document.createElement('div');
mainObsoleteSection.className = 'obsolete-main-section';
let totalObsoleteCount = 0;
let subCategoryHTML = '';
// Iterate through original categories within obsolete_deprecated data
// The keys in obsoleteData are the original category names (e.g., "text")
for (const originalCatKey in obsoleteData) {
if (obsoleteData.hasOwnProperty(originalCatKey)) {
let typesForSubCat = obsoleteData[originalCatKey];
let filteredSubCatTypes = typesForSubCat;
if (searchTerm) {
filteredSubCatTypes = typesForSubCat.filter(type =>
type.name.toLowerCase().includes(searchTerm) ||
type.template.toLowerCase().includes(searchTerm)
);
}
if (filteredSubCatTypes.length > 0) {
totalObsoleteCount += filteredSubCatTypes.length;
const originalCatTitle = originalCatKey.charAt(0).toUpperCase() + originalCatKey.slice(1);
subCategoryHTML += `
<div class="obsolete-subcategory">
<div class="obsolete-subcategory-header">
<h3>From: ${originalCatTitle} (${filteredSubCatTypes.length})</h3>
<span class="toggle-arrow"></span>
</div>
<div class="obsolete-subcategory-list">
${filteredSubCatTypes.map(createMimeEntryHTML).join('')}
</div>
</div>`;
}
}
}
if (totalObsoleteCount === 0 && searchTerm) return null; // No matching obsolete items for search
if (totalObsoleteCount === 0 && !searchTerm) return null; // No obsolete items at all
mainObsoleteSection.innerHTML = `
<div class="obsolete-main-title-bar">
<h2>Obsolete / Deprecated (${totalObsoleteCount})</h2>
</div>
<div class="obsolete-subcategories-container">
${subCategoryHTML}
</div>`;
// Add event listeners for collapsible subcategories
mainObsoleteSection.querySelectorAll('.obsolete-subcategory-header').forEach(header => {
header.addEventListener('click', () => {
header.parentElement.classList.toggle('open');
});
});
return mainObsoleteSection;
}
function renderMimeData(data, searchTerm = "") {
displayArea.innerHTML = '';
let foundAnyContent = false;
const lowerSearchTerm = searchTerm.toLowerCase().trim();
// Render main categories
MAIN_CATEGORY_ORDER.forEach(categoryKey => {
if (data[categoryKey]) {
let typesToDisplay = data[categoryKey];
if (lowerSearchTerm) {
typesToDisplay = typesToDisplay.filter(type =>
type.name.toLowerCase().includes(lowerSearchTerm) ||
type.template.toLowerCase().includes(lowerSearchTerm)
);
}
if (typesToDisplay.length > 0) {
foundAnyContent = true;
const categoryTitle = categoryKey.charAt(0).toUpperCase() + categoryKey.slice(1);
displayArea.appendChild(
createCategorySectionDOM(categoryKey, categoryTitle, typesToDisplay)
);
}
}
});
// Render obsolete/deprecated section
if (data[OBSOLETE_DATA_KEY]) {
const obsoleteSectionElement = createObsoleteSectionDOM(data[OBSOLETE_DATA_KEY], lowerSearchTerm);
if (obsoleteSectionElement) {
displayArea.appendChild(obsoleteSectionElement);
foundAnyContent = true; // Count if obsolete section has content
}
}
loadingIndicator.classList.add('hidden');
noResultsIndicator.classList.toggle('hidden', foundAnyContent || !lowerSearchTerm);
if (!foundAnyContent && !lowerSearchTerm && Object.keys(data).length === 0) {
displayArea.innerHTML = '<p class="status-text error">MIME type data is empty or not loaded.</p>';
} else if (!foundAnyContent && !lowerSearchTerm && Object.keys(data).length > 0 && !data[OBSOLETE_DATA_KEY] && MAIN_CATEGORY_ORDER.every(k => !data[k] || data[k].length === 0)) {
displayArea.innerHTML = '<p class="status-text">No MIME type data available in active categories.</p>';
}
}
fetch('mime_types.json')
.then(response => {
if (!response.ok) throw new Error(`Failed to load mime_types.json: ${response.status} ${response.statusText}`);
return response.json();
})
.then(data => {
allMimeData = data;
renderMimeData(allMimeData);
})
.catch(error => {
console.error('Error fetching/processing MIME data:', error);
loadingIndicator.classList.add('hidden');
displayArea.innerHTML = `<p class="status-text error">Error: ${error.message}</p>`;
});
function debounce(func, delay) {
return function(...args) {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => func.apply(this, args), delay);
};
}
searchInput.addEventListener('input', debounce((e) => {
renderMimeData(allMimeData, e.target.value)
}, 250));
});
</script>
</body>
</html>