Skip to content

About

Fanfou SDK for JavaScript

Topics

Resources

Stars

35 stars

Watchers

1 watching

Forks

Repository files navigation

fanfou-sdk-node

Fanfou SDK for JavaScript

Install

npm i fanfou-sdk

Usage

import Fanfou from 'fanfou-sdk';

OAuth

const ff = new Fanfou({
	consumerKey: '',
	consumerSecret: '',
	oauthToken: '',
	oauthTokenSecret: '',
});

const timeline = await ff.getHomeTimeline();

XAuth

const ff = new Fanfou({
	consumerKey: '',
	consumerSecret: '',
	username: '',
	password: '',
});

await ff.xauth();

const timeline = await ff.getPublicTimeline({count: 10});
const status = await ff.createStatus({status: 'Hi Fanfou'});

Options

  • consumerKey: The consumer key
  • consumerSecret: The consumer secret
  • oauthToken: The OAuth token
  • oauthTokenSecret: The OAuth token secret
  • username: The Fanfou username
  • password: The Fanfou password
  • protocol: Set the prototol, default is https:
  • apiDomain: Set the API domain, default is api.fanfou.com
  • oauthDomain: Set the OAuth domain, default is fanfou.com
  • hooks: Hooks allow modifications with OAuth

For more Fanfou API docs, see the Fanfou API doc.

API

For full SDK API, please refer to the documentation.

Request parameters and response fields use camelCase. The SDK converts parameters like sinceId to since_id for Fanfou, and response fields like created_at to createdAt, including nested objects like user.screenName.

Examples

// Get request token
const token = await ff.getRequestToken();

// Get access token
const token = await ff.getAccessToken(token);

// Get timeline
const timeline = await ff.getHomeTimeline();

// Post status
const status = await ff.createStatus({status: 'post test'});

// Upload photo
const result = await ff.uploadPhoto({
	photo: fs.createReadStream(path),
	status: 'unicorn',
});

// Get user
const user = await ff.getUser({id: 'lito'});

// Request Fanfou API by passing the URI
const timeline = await ff.get('/statuses/home_timeline', {count: 10});
const status = await ff.post('/statuses/update', {status: 'Hi Fanfou'});

Tips

Use hooks for your reverse-proxy server

const ff = new Fanfou({
	consumerKey: '',
	consumerSecret: '',
	oauthToken: '',
	oauthTokenSecret: '',
	apiDomain: 'api.example.com',
	oauthDomain: 'example.com',
	hooks: {
		baseString: (str) => {
			return str.replace('example.com', 'fanfou.com');
		},
	},
});

Utilities

Status text

Use format: 'html' when fetching statuses. getEntities splits the HTML text into text, mentions (at), topics (tag) and links, so you can render each part in your client.

import {getEntities, getPlainText} from 'fanfou-sdk';

const status = await ff.getStatus({id: 'statusId', format: 'html'});
const entities = getEntities(status.text);
const text = getPlainText(entities);

getEntities('Hi @<a href="https://fanfou.com/lito">Lito</a>');
// [
//   {type: 'text', text: 'Hi '},
//   {type: 'at', text: '@Lito', name: 'Lito', id: 'lito'},
// ]

Entities include boldTexts for highlighted text, such as search matches. getPlainText joins the entities into plain text and decodes HTML entities.

Bold text

Check for bold tags, remove them, or split text into regular and bold parts.

import {hasBold, removeBoldTag, getBoldTexts} from 'fanfou-sdk';

const text = 'Hi <b>Fanfou</b>';

hasBold(text); // true
removeBoldTag(text); // "Hi Fanfou"
getBoldTexts(text);
// [
//   {text: "Hi ", isBold: false},
//   {text: "Fanfou", isBold: true},
// ]

Status types

import {getType, isReply, isRepost, isOrigin, isOriginRepost} from 'fanfou-sdk';

const status = await ff.getStatus({id: 'statusId'});

getType(status); // "reply", "repost" or "origin"
isReply(status); // Has a reply status or user ID
isRepost(status); // Has a repost status ID
isOrigin(status); // Neither a reply nor a repost
isOriginRepost(status); // An original status containing "转@"

Status source

Extract the name and URL from a status's source field.

import {getSourceName, getSourceUrl} from 'fanfou-sdk';

const source = '<a href="https://fanfou.com" target="_blank">Fanfou</a>';

getSourceName(source); // "Fanfou"
getSourceUrl(source); // "https://fanfou.com"

For a source without a link, getSourceName returns the source text and getSourceUrl returns an empty string.

Related

License

MIT

About

Fanfou SDK for JavaScript

Topics

Resources

Stars

35 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages