Eine inoffizielle API für Royalroad.com.
npm i -s @fsoc/royalroadl-api
Dies ist ein Versuch, eine vorhersehbare und konsequente Wrapper um das Chaos zu schreiben, das RR ist. Since no official public API is exposed, this module scrapes all data straight from the HTML, which makes it very prone to spontaneous and horrible death.
Die aus Typedoc erzeugte Barebones-Dokumentation finden Sie unter fs-c.github.io/royalroad-api/. Sie können diese Dokumente auch selbst erstellen, indem Sie npm run docs in der Wurzel des Projekts ausführen.
Weitere Beispiele finden Sie im Verzeichnis /examples . Siehe auch die Tests in /test .
import { RoyalRoadAPI } from '@fsoc/royalroadl-api' ;
const api = new RoyalRoadAPI ( ) ;
const { data } = await api . fictions . getPopular ( ) ;
const titles = data . slice ( 10 ) . map ( ( fic ) => fic . title ) ;
console . log ( `The top 10 popular fictions are: ${ titles . join ( ', ' ) } ` ) ; Das Modul selbst exportiert nur eine RoyalRoadAPI -Klasse, die selbst keine Methoden hat. Alle Funktionen werden an Dienstklassen delegiert, die Eigenschaften der RoyalRoadAPI -Instanz sind.
All responses and errors are either an instance of a RoyalResponse or a RoyalError , which extends RoyalResponse . This is done to easily allow for meta information to be tacked onto responses, and to have a consistent interface between user and module. Note that the RoyalError acts similarly to the NodeJS Error object, in that it captures and returns a short stack trace.
For example, a call to RoyalRoadAPI#fiction.getFiction() might yield the following response on success:
RoyalResponse {
data :
{ type : 'Original' ,
tags : [ 'Action' , 'Adventure' , 'Sci-fi' , ... 3 more items ] ,
stats :
{ pages : 766 ,
ratings : 719 ,
followers : 2991 ,
favorites : 690 ,
views : [ Object ] ,
score : [ Object ] } ,
title : 'Paladin' ,
image :
'https://www.royalroadcdn.com/(...)' ,
status : 'HIATUS' ,
author :
{ name : 'Komikhan' ,
title : '' ,
avatar :
'https://www.royalroadcdn.com/(...)' ,
id : 66486 } ,
warnings : [ 'Gore' , 'Profanity' ] ,
chapters : [ [ Object ] , [ Object ] , [ Object ] , ... 71 more items ] ,
description :
'When the first derelict alien spacecraft fell to Earth, (...)' } ,
success : true ,
timestamp : 1528119296799 }...or on error:
RoyalError {
data :
{ message : 'Page Not Found' ,
stack :
[ 'Error' ,
' at new RoyalError' , ... 8 more items ] } ,
success : false ,
timestamp : 1528119381034 } Alle Serviceklassen verwenden dieselbe Instanz des Requester , der Klasse, die für die Erstellung von HTTP -Anfragen und die Rückgabe ihrer Antworten verantwortlich ist. By default, it will throw a RoyalError if it encounters a status code other than 200 (this can be disabled with the ignoreStatus option).
Since RR likes to return 200 even when the actual response should be a 404 or 304, the Requester will parse the HTML it has gotten (if it got any), and try to read an error from it. Wenn es Anzeichen findet, dass die Anfrage fehlgeschlagen ist, wird sie geworfen - dies kann mit der Option ignoreParser deaktiviert werden.
Das Hauptziel des Requester ist es, Cookies im Auge zu behalten und automatisch eine __ResponseVerificationToken zu holen, die häufig für Postanfragen als Teil von Anti -CSRF -Maßnahmen benötigt wird. Dieses Abrufen von Token ist standardmäßig deaktiviert und kann mit der Option fetchToken aktiviert werden.
All services are structured in a very similar way: with a <Type>Service exposing all relevant methods, and a <Type>Parser , which usually exposes a number of static methods used to parse HTML responses.
Ein kurzer Abschluss aller vorhandenen Dienste ist:
ChapterService enthält Methoden zum Abrufen und Veröffentlichen von Kapiteln und Kapitelkommentaren.FictionService , Daten und Bewertungen abrufen.FictionsService , Methoden zum Abrufen aller Arten von Belletristiklisten RRL -Angebote mit ihren jeweiligen Maßstäben an Fiktionsdetails.ProfileService , Handhabungsprofile, Rücksende von analysierten Benutzerprofilen.UserService , actions related to the logged-in user like logon, getting the users' fictions, bookmarks, or notifications.This uses cheerio to parse HTML, which is a very forgiving parser. This means that even if RRL were to make minor changes to their page layouts, large parts of the API (even those parts responsible for changed areas) would still remain functional.
Therefore, expect properties to be empty or null , and know that an error will not be thrown just because some values could not be parsed.