Skip to content

Commit

Permalink
Merge pull request #59 from AtlasTheBot/dev
Browse files Browse the repository at this point in the history
Dev 2.4.0
  • Loading branch information
D. Plaza committed Nov 4, 2020
2 parents a381c71 + 3e7406b commit 3ce5370
Show file tree
Hide file tree
Showing 11 changed files with 970 additions and 2,381 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# booru Changelog

## 2.3.3 [Latest]
## 2.4.0

- Removed furry.booru.org since they have CloudFlare browser verification enabled.
- As far as I know, there's no (intended) way to bypass this if you're not a browser.
- Added CloudFlare-specific error message if this happens in the future
- Add fix for Paheal changing their API response format
- Make example.js only specify the "cat" default tag if you don't specify a site
- Change from `tsc` -> `typescript` for package.json scripts, since `tsc` is deprecated.
- Update dependencies

## 2.3.3

- Fix Paheal failing to provide a useful error message.
- Details: Paheal was returning an HTML error page instead of a JSON response. The previous way
Expand Down
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { BooruError, sites } = require('./dist')

const argTags = process.argv.slice(3)
const site = process.argv[2] || 'sb'
const tags = argTags.length > 0 ? argTags : ['cat']
const tags = process.argv[2] ? argTags : ['cat']

// Search with promises
Booru.search(site, tags, { limit: 1, random: false })
Expand Down
3,144 changes: 880 additions & 2,264 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "booru",
"version": "2.3.3",
"version": "2.4.0",
"description": "Search (and do other things) on a bunch of different boorus!",
"author": "AtlasTheBot (https://github.com/AtlasTheBot/)",
"license": "MIT",
Expand All @@ -20,8 +20,8 @@
"lint": "npx tslint --fix -p . -c ./tslint.json ./src/**/*.ts",
"clean": "npx rimraf ./dist",
"prebuild": "npm run-script clean && npm run-script lint",
"build": "npx tsc --sourceMap false",
"build-dev": "npx tsc --sourceMap",
"build": "npx typescript --sourceMap false",
"build-dev": "npx typescript --sourceMap",
"postbuild": "node ./minify.js",
"prepublishOnly": "npm run-script build"
},
Expand All @@ -48,20 +48,20 @@
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@babel/core": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/polyfill": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@types/jest": "^26.0.14",
"@types/node": "^14.11.8",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@types/node-fetch": "^2.5.7",
"jest": "^26.5.3",
"jest": "^26.6.3",
"rimraf": "^3.0.2",
"terser": "^5.3.5",
"tslint": "^6.1.2",
"terser": "^5.3.8",
"tslint": "^6.1.3",
"typedoc": "^0.19.2",
"typedoc-plugin-external-module-name": "^4.0.3",
"typescript": "^4.0.3"
"typescript": "^4.0.5"
},
"files": [
"dist/"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

## Features

- Search 16 different boorus (check [sites.json](./src/sites.json))
- Search 15 different boorus (check [sites.json](./src/sites.json))
- Normalizes all received data into `Post` objects that are consistent no matter which booru you use
- Access to the raw data received from the booru as well (transformed from XML to JSON, if applicable)
- Alias support for boorus (`sb` for `safebooru.org`)
Expand Down
1 change: 1 addition & 0 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export async function jsonfy(xml: string): Promise<object[]> {
}

if (data.posts.post) return data.posts.post
if (data.posts.tag) return Array.isArray(data.posts.tag) ? data.posts.tag : [data.posts.tag]
return []
}

Expand Down
16 changes: 15 additions & 1 deletion src/boorus/Booru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import SearchResults from '../structures/SearchResults'
import Site from '../structures/Site'

// Shut up the compiler
// This attempts to find and use the native browser fetch, if possible
// Fixes https://github.com/AtlasTheBot/booru/issues/51
declare const window: any
const resolvedFetch = typeof window !== 'undefined' ? window.fetch.bind(window) : fetch
const resolvedFetch: typeof fetch =
typeof window !== 'undefined' ? window.fetch.bind(window) : fetch

/*
- new Booru
Expand Down Expand Up @@ -142,6 +145,17 @@ export class Booru {

try {
const response = await resolvedFetch(fetchuri, options)

// Check for CloudFlare ratelimiting
if (response.status === 503) {
const body = await response.clone().text()
if (body.includes('cf-browser-verification')) {
throw new BooruError(
'Received a CloudFlare browser verification request. Can\'t proceed.'
)
}
}

const data: Response = xml ? await response.text() : await response.json()
const posts = xml ? await jsonfy(data as unknown as string) : data

Expand Down
14 changes: 0 additions & 14 deletions src/sites.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,6 @@
"tagQuery": "q",
"random": "sf=random"
},
"furry.booru.org": {
"domain": "furry.booru.org",
"aliases": [
"fb",
"furrybooru"
],
"nsfw": true,
"api": {
"search": "/index.php?page=dapi&s=post&q=index&json=1&",
"postView": "/index.php?page=post&s=view&id="
},
"paginate": "pid",
"random": false
},
"realbooru.com": {
"domain": "realbooru.com",
"aliases": [
Expand Down
8 changes: 4 additions & 4 deletions src/structures/Site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class Site {
/** The domain of the Site (the "google.com" part of "https://google.com/foo") */
public domain: string
/** The type of this site (json/xml/derpi) */
public type: string
public type?: string
/** The aliases of this site */
public aliases: string[]
/** If this site serves NSFW posts or not */
Expand All @@ -28,11 +28,11 @@ export default class Site {
*/
public random: boolean | string
/** The url query param for tags */
public tagQuery: string
public tagQuery?: string
/** If this site supports only http:// */
public insecure: boolean
public insecure?: boolean
/** Tags to add to every request, if not included */
public defaultTags: string[]
public defaultTags?: string[]

constructor(data: SiteInfo) {
this.domain = data.domain
Expand Down
38 changes: 0 additions & 38 deletions test/furrybooru.spec.ts

This file was deleted.

94 changes: 47 additions & 47 deletions test/util.spec.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import path from 'path';
import { BooruClass, BooruError, resolveSite, sites } from '../src';
import Site from '../src/structures/Site';

let site: string;

beforeEach(() => {
site = 'db';
});

describe('Check resolveSite', () => {
it('should resolve alias to host', () => {
expect(resolveSite(site)).toBe('danbooru.donmai.us');
});
});

describe('check BooruError', () => {
it('should resolve to a BooruError', () => {
const booruError = new BooruError();
expect(booruError.name).toBe('BooruError');
expect(booruError.stack).toContain(path.basename(__filename));
});
});

describe('check BooruClass', () => {
it('should resolve to a BooruClass', () => {
const SiteData: Site = {
aliases: ['sb', 'safe', 'safebooru'],
api: {search: '/index.php?page=dapi&s=post&q=index&json=1&', postView: '/index.php?page=post&s=view&json=1&id='},
domain: 'safebooru.org',
nsfw: false,
random: false,
paginate: 'pid',
};
const booruClass = new BooruClass(SiteData);

expect(booruClass.domain).toBe('safebooru.org');
expect(booruClass.site).toMatchObject(SiteData);
});
});

describe('check sites', () => {
it('should support 16 sites', () => {
const map = sites;
expect(Object.keys(map)).toHaveLength(16);
});
});
import path from 'path'
import { BooruClass, BooruError, resolveSite, sites } from '../src'
import Site from '../src/structures/Site'

let site: string

beforeEach(() => {
site = 'db'
})

describe('Check resolveSite', () => {
it('should resolve alias to host', () => {
expect(resolveSite(site)).toBe('danbooru.donmai.us')
})
})

describe('check BooruError', () => {
it('should resolve to a BooruError', () => {
const booruError = new BooruError()
expect(booruError.name).toBe('BooruError')
expect(booruError.stack).toContain(path.basename(__filename))
})
})

describe('check BooruClass', () => {
it('should resolve to a BooruClass', () => {
const SiteData: Site = {
aliases: ['sb', 'safe', 'safebooru'],
api: {search: '/index.php?page=dapi&s=post&q=index&json=1&', postView: '/index.php?page=post&s=view&json=1&id='},
domain: 'safebooru.org',
nsfw: false,
random: false,
paginate: 'pid',
}
const booruClass = new BooruClass(SiteData)

expect(booruClass.domain).toBe('safebooru.org')
expect(booruClass.site).toMatchObject(SiteData)
})
})

describe('check sites', () => {
it('should support 15 sites', () => {
const map = sites
expect(Object.keys(map)).toHaveLength(15)
})
})

0 comments on commit 3ce5370

Please sign in to comment.