Giter Club home page Giter Club logo

megalodon's Introduction

Megalodon

Test NPM Version GitHub release npm NPM

A Fediverse API Client library for node.js and browser. It provides REST API and streaming methods. By using this library, you can take Mastodon, Pleroma, Friendica, and Misskey with the same interface.

The Rust version is megalodon-rs.

Supporting

  • Mastodon
  • Pleroma
  • Friendica
  • Misskey

Features

  • REST API
  • Admin API
  • WebSocket for Streamings
  • Promisified methods
  • Proxy support
  • Support node.js and browser
  • Written in typescript

Install

$ npm install -S megalodon

or

$ yarn add megalodon

Build for browser

Important: In browser, you can not use proxy.

If you want to build for browser, please use Webpack and set empty value for some libraries which are not supported in Node.js. Here is example Webpack configuration.

Usage

I prepared examples, and please refer documents about each methods.

I explain some typical methods. At first, please get your access token for a fediverse server. If you don't have access token, or you want to register applications and get access token programmably, please refer Authorization section.

Home timeline

import generator, { Entity, Response } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'

const client = generator('mastodon', BASE_URL, access_token)
client.getHomeTimeline()
  .then((res: Response<Array<Entity.Status>>) => {
    console.log(res.data)
  })

Post toot

import generator, { Entity, Response } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'
const toot: string = 'test toot'

const client = generator('mastodon', BASE_URL, access_token)
client.postStatus(toot)
  .then((res: Response<Entity.Status>) => {
    console.log(res.data)
  })

Post medias

Please provide a file to the argument.

import generator, { Entity, Response } from 'megalodon'
import fs from 'fs'

const BASE_URL: string = 'https://mastodon.social'
const access_token: string = '...'
const image = fs.readFileSync("test.image")

const client = generator('mastodon', BASE_URL, access_token)
client.uploadMedia(image)
  .then((res: Response<Entity.Attachment>) => {
    console.log(res.data)
  })

WebSocket streaming

Mastodon, Pleroma and Misskey provide WebSocket for streaming.

import generator, { Entity, WebSocketInterface } from 'megalodon'

const BASE_URL: string = 'wss://pleroma.io'
const access_token: string = '...'

const client = generator('pleroma', BASE_URL, access_token)
const stream: WebSocketInterface = client.userSocket()

stream.on('connect', () => {
  console.log('connect')
})

stream.on('update', (status: Entity.Status) => {
  console.log(status)
})

stream.on('notification', (notification: Entity.Notification) => {
  console.log(notification)
})

stream.on('delete', (id: number) => {
  console.log(id)
})

stream.on('error', (err: Error) => {
  console.error(err)
})

stream.on('heartbeat', () => {
  console.log('thump.')
})

stream.on('close', () => {
  console.log('close')
})

stream.on('parser-error', (err: Error) => {
  console.error(err)
})

Authorization

You can register applications, and get access tokens to use this method.

import generator, { OAuth } from 'megalodon'

const BASE_URL: string = 'https://mastodon.social'

let clientId: string
let clientSecret: string

const client = generator('mastodon', BASE_URL)

client.registerApp('Test App')
  .then(appData => {
    clientId = appData.clientId
    clientSecret = appData.clientSecret
    console.log('Authorization URL is generated.')
    console.log(appData.url)
  })

Please open Autorhization URL in your browser, and authorize this app. In this time, you can get authorization code.

After that, get an access token.

const code = '...' // Authorization code

client.fetchAccessToken(clientId, clientSecret, code)
})
  .then((tokenData: OAuth.TokenData) => {
    console.log(tokenData.accessToken)
    console.log(tokenData.refreshToken)
  })
  .catch((err: Error) => console.error(err))

Detect each SNS

You have to provide SNS name mastodon, pleroma or misskey to generator function. But when you only know the URL and not the SNS, detector function can detect the SNS.

import { detector } from 'megalodon'

const URL = 'https://misskey.io'

const sns = await detector(URL)
console.log(sns)

License

The software is available as open source under the terms of the MIT License.

megalodon's People

Contributors

h3poteto avatar dependabot[bot] avatar dependabot-preview[bot] avatar fasiha avatar mogita avatar potproject avatar cpluspatch avatar dermike avatar samtherapy avatar kphrx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.