chore: cleanup trace.moe search

This commit is contained in:
ThaUnknown 2023-01-16 15:28:48 +01:00
parent efddc211fc
commit d9c9d6849f

View file

@ -10,34 +10,29 @@ import { view } from '@/App.svelte'
const torrentRx = /(^magnet:){1}|(^[A-F\d]{8,40}$){1}|(.*\.torrent$){1}/i const torrentRx = /(^magnet:){1}|(^[A-F\d]{8,40}$){1}|(.*\.torrent$){1}/i
const imageRx = /\.(jpeg|jpg|gif|png|webp)/i const imageRx = /\.(jpeg|jpg|gif|png|webp)/i
window.addEventListener('paste', async e => { // WAIT image lookup on paste, or add torrent on paste window.addEventListener('paste', ({ clipboardData }) => { // WAIT image lookup on paste, or add torrent on paste
const item = e.clipboardData.items[0] const item = clipboardData.items[0]
if (item?.type.indexOf('image') === 0) { if (!item) return
e.preventDefault() const { type } = item
traceAnime(item.getAsFile(), 'file') if (type.startsWith('image')) return traceAnime(item.getAsFile())
} else if (item?.type === 'text/plain') { if (!type.startsWith('text')) return
item.getAsString(text => { item.getAsString(text => {
if (torrentRx.exec(text)) { if (torrentRx.exec(text)) {
e.preventDefault() add(text)
add(text) media.set(null)
media.set(null) } else {
let src = null
if (type === 'text/html') {
src = DOMPARSER(text, 'text/html').querySelectorAll('img')[0]?.src
} else if (imageRx.exec(text)) { } else if (imageRx.exec(text)) {
e.preventDefault() src = text
traceAnime(text)
} }
}) if (src) traceAnime(src)
} else if (item && item.type === 'text/html') { }
item.getAsString(text => { })
const img = DOMPARSER(text, 'text/html').querySelectorAll('img')[0]
if (img) {
e.preventDefault()
traceAnime(img.src)
}
})
}
}) })
export function traceAnime (image, type) { // WAIT lookup logic export async function traceAnime (image) { // WAIT lookup logic
if (type === 'file') { if (image instanceof Blob) {
const reader = new FileReader() const reader = new FileReader()
reader.onload = e => { reader.onload = e => {
addToast({ addToast({
@ -54,27 +49,26 @@ export function traceAnime (image, type) { // WAIT lookup logic
} }
let options let options
let url = `https://api.trace.moe/search?cutBorders&url=${image}` let url = `https://api.trace.moe/search?cutBorders&url=${image}`
if (type === 'file') { if (image instanceof Blob) {
const formData = new FormData()
formData.append('image', image)
options = { options = {
method: 'POST', method: 'POST',
body: formData body: image,
headers: { 'Content-type': image.type }
} }
url = 'https://api.trace.moe/search' url = 'https://api.trace.moe/search'
} }
fetch(url, options).then(res => res.json()).then(async ({ result }) => { const res = await fetch(url, options)
if (result && result[0].similarity >= 0.85) { const { result } = await res.json()
const res = await alRequest({ method: 'SearchIDSingle', id: result[0].anilist }) if (result && result[0].similarity >= 0.85) {
view.set(res.data.Media) const res = await alRequest({ method: 'SearchIDSingle', id: result[0].anilist })
} else { view.set(res.data.Media)
addToast({ } else {
text: 'Couldn\'t find anime for specified image! Try to remove black bars, or use a more detailed image.', addToast({
title: 'Search Failed', text: 'Couldn\'t find anime for specified image! Try to remove black bars, or use a more detailed image.',
type: 'danger' title: 'Search Failed',
}) type: 'danger'
} })
}) }
} }
export function getMediaMaxEp (media, playable) { export function getMediaMaxEp (media, playable) {