bash.im
Advanced tools
Comparing version 1.0.2 to 1.1.0
31
index.js
let fetch = require('node-fetch'); | ||
let iconv = require('iconv-lite'); | ||
let idRegexp = /<a href="\/quote\/.*" class="id">#(.*)<\/a>/i; | ||
let regexp = /<div class="text">([^]+?)<\/div>/i; | ||
/* RegExp */ | ||
let bodyRegexp = /<div class="quote__body">\n\s+(.*?)\n\s+?<\/div>/is; | ||
let idRegexp = /<a class="quote__header_permalink" href="\/quote\/\d+">#(\d+)<\/a>/i; | ||
let dateRegexp = /<div class="quote__header_date">\n\s+(.*?)\s+в\s+(.*?)\n\s+<\/div>/i; | ||
let dateStringRegexp = /(\d+)\.(\d+)\.(\d+) (\d+):(\d+)/i; | ||
let votesRegexp = /<div class="quote__total" data-vote-counter>(\d+)<\/div>/is; | ||
async function bash() { | ||
let result = await (await fetch('https://bash.im/random')).buffer(); | ||
result = iconv.decode(result, 'win1251'); | ||
let result = await (await fetch('https://bash.im/random')).text(); | ||
let body = result.match(bodyRegexp)[1]; | ||
let id = +result.match(idRegexp)[1]; | ||
let text = result.match(regexp)[1]; | ||
text = text.replace(/<br(?:\s*\/)?>/ig, '\n') | ||
let dateSource = result.match(dateRegexp).slice(1).join(' '); | ||
let votes = +result.match(votesRegexp)[1]; | ||
let [, day, month, year, hour, minute] = dateSource.match(dateStringRegexp); | ||
let date = new Date(year, month, day, hour, minute); | ||
let createdAt = +date; | ||
body = body.replace(/<br(?:\s*\/)?>/ig, '\n') | ||
.replace(/"/g, '"') | ||
@@ -19,4 +31,7 @@ .replace(/</ig, '<') | ||
return { | ||
quote: text, | ||
body, | ||
id, | ||
date, | ||
createdAt, | ||
votes, | ||
link: `https://bash.im/quote/${id}`, | ||
@@ -23,0 +38,0 @@ }; |
{ | ||
"name": "bash.im", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A package to get random quote from bash.im", | ||
@@ -24,5 +24,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"iconv-lite": "^0.4.24", | ||
"node-fetch": "^2.3.0" | ||
} | ||
} |
@@ -11,8 +11,21 @@ # bash.im | ||
let { quote, id, link } = await bash(); | ||
let { body, id, link, votes, date, createdAt } = await bash(); | ||
console.log(`Цитата: "${quote}"\nID: ${id}\nLink: ${link}`); | ||
// Цитата: "xxx: Два раза в год военкомы собираются на шабаш и призывают духов" | ||
// ID: 406121 | ||
// Link: https://bash.im/quote/406121 | ||
let result = [ | ||
`Quote: <<${body}>>`, | ||
`ID: ${id}`, | ||
`Votes: ${votes}`, | ||
`Created at: ${createdAt}`, | ||
`Date: ${date}`, | ||
`Link: ${link}`, | ||
].join('\n'); | ||
console.log(result); | ||
// Quote: <<<montavista> Военрук молодец: "В случае землетрясения сразу выносите деньги, документы и кота">> | ||
// ID: 413268 | ||
// Votes: 15454 | ||
// Created at: 1318576260000 | ||
// Date: Fri Oct 14 2011 10:11:00 GMT+0300 (GMT+03:00) | ||
// Link: https://bash.im/quote/413268 | ||
``` |
2570
1
30
31
- Removediconv-lite@^0.4.24
- Removediconv-lite@0.4.24(transitive)
- Removedsafer-buffer@2.1.2(transitive)