Back to Home
Gistify Snippet
Public
zerochan.js
Imported from GitHub Gist: zerochan.js
#github-gist#javascript#scraper#gist:45e5174c082516452b529d6191627e85
6
Rawzerochan.jsjavascript
79 lines| 1 | import axios from 'axios' |
| 2 | import * as cheerio from 'cheerio' |
| 3 | |
| 4 | const BASE_URL = 'https://www.zerochan.net' |
| 5 | const UA = 'MithraEX - kyiov' |
| 6 | |
| 7 | function absolutize(url) { |
| 8 | if (!url) return null |
| 9 | if (/^https?:\/\//i.test(url)) return url |
| 10 | return `${BASE_URL}${url.startsWith('/') ? '' : '/'}${url}` |
| 11 | } |
| 12 | |
| 13 | export async function zerochanSearch(query, page = 1) { |
| 14 | if (!query) throw new Error('Query is required.') |
| 15 | |
| 16 | const url = `${BASE_URL}/search?q=${encodeURIComponent(query)}&p=${page}` |
| 17 | const { data } = await axios.get(url, { |
| 18 | headers: { |
| 19 | 'User-Agent': UA, |
| 20 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,webp,*/*;q=0.8' |
| 21 | }, |
| 22 | validateStatus: (status) => status === 200 || status === 404, |
| 23 | timeout: 20000 |
| 24 | }) |
| 25 | |
| 26 | const $ = cheerio.load(data) |
| 27 | const results = [] |
| 28 | |
| 29 | $('#thumbs2 li, #thumbs li').each((_, el) => { |
| 30 | const a = $(el).find('a.thumb').first() |
| 31 | const img = $(el).find('img').first() |
| 32 | const title = img.attr('alt') || a.attr('title') || $(el).find('p a').text().trim() |
| 33 | const href = a.attr('href') |
| 34 | const cover = absolutize(img.attr('data-src') || img.attr('src')) |
| 35 | const id = $(el).attr('data-id') || (href ? href.replace(/[^\d]/g, '') : null) |
| 36 | |
| 37 | if (title && href) { |
| 38 | results.push({ |
| 39 | id, |
| 40 | title, |
| 41 | url: absolutize(href), |
| 42 | cover, |
| 43 | meta: img.attr('title') || null |
| 44 | }) |
| 45 | } |
| 46 | }) |
| 47 | |
| 48 | return results |
| 49 | } |
| 50 | |
| 51 | export async function zerochanDetail(urlOrId) { |
| 52 | if (!urlOrId) throw new Error('URL or ID is required.') |
| 53 | |
| 54 | const url = /^https?:\/\//i.test(urlOrId) ? urlOrId : `${BASE_URL}/${urlOrId}` |
| 55 | const { data } = await axios.get(url, { |
| 56 | headers: { |
| 57 | 'User-Agent': UA, |
| 58 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,webp,*/*;q=0.8' |
| 59 | }, |
| 60 | validateStatus: (status) => status === 200 || status === 404, |
| 61 | timeout: 20000 |
| 62 | }) |
| 63 | |
| 64 | const $ = cheerio.load(data) |
| 65 | const title = $('h1').first().text().trim() || $('.content h1').first().text().trim() || 'Zerochan Image' |
| 66 | const cover = absolutize($('img').first().attr('data-src') || $('img').first().attr('src')) |
| 67 | const metaText = $('img').first().attr('title') || $('.meta').text().trim() || '' |
| 68 | |
| 69 | return { |
| 70 | title, |
| 71 | cover, |
| 72 | source: url, |
| 73 | metaText |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | export default { zerochanSearch, zerochanDetail } |
| 78 | |
| 79 | |
Discussion & Comments0
No comments yet — be the first to leave feedback or start a code discussion.
Sign in with your GitHub account to write comments and join the discussion.