• Stars
    star
    243
  • Rank 166,489 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fediverse API client library for node.js and browser

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.

More Repositories

1

whalebird-desktop

Single-column Fediverse client for desktop
TypeScript
894
star
2

fedistar

Multi-column Fediverse client for desktop
TypeScript
128
star
3

megalodon-rs

Fediverse API client library for Rust
Rust
107
star
4

kube-job

Run one off job on kubernetes from the command line tool
Go
86
star
5

fluentd-sidecar-injector

Webhook server for kubernetes admission webhook to inject fluentd as sidecar
Go
38
star
6

ecs-goploy

re-implementation of ecs-deploy in Golang
Go
33
star
7

ecs-task

Run a task on Amazon ECS and stream the output to stdout
Go
27
star
8

aws-global-accelerator-controller

A Kubernetes controller for Global Accelerators and Route53
Go
26
star
9

logist

A json logger for Rails
Ruby
26
star
10

electron-mock-ipc

Mock Electron's ipcMain, and ipcRenderer
TypeScript
23
star
11

fascia

[Not maintained]
Go
13
star
12

istio-grpc-example

gRPC server on Istio on kubernetes
12
star
13

whalebird-ios

[EOL][DEPRECATED] Twitter client for iOS
Swift
10
star
14

yadockeri

Yadockeri - Yet Another Docker Infra
Go
10
star
15

zsh-ec2ssh

zsh plugin to list up EC2 instances and ssh login the instances.
Shell
10
star
16

kms-secrets

A Kubernetes controller to apply AWS KMS encrypted secrets as it is
Go
7
star
17

exception_notification-shoryuken

Exception Notifier Plugin for Rails with Shoryuken http://smartinez87.github.com/exception_notification
Ruby
5
star
18

pleroma.io

Pleroma.io
Elixir
5
star
19

grpc_example

Python
5
star
20

pongo2echo

Package pongo2echo provides renderer for labstack's echo framework
Go
4
star
21

node-memcached-client

Memcached client for nodejs using typescript
TypeScript
4
star
22

guess_paging

Ruby
3
star
23

slack-rage

Slack bot to notify excited channels using Event API
Go
3
star
24

py_masuda_stream

Python
3
star
25

go-grpc-example

Go
3
star
26

whalebird.server

API server for Whalebird is twitter client
Ruby
3
star
27

WakeMusic

Swift
2
star
28

ec2-reserved-checker

Go
2
star
29

niconico_search

A gem for niconico snapshot search api v2
Ruby
2
star
30

vue3-i18next

Internationalization for Vue3. Using the i18next i18n ecosystem.
TypeScript
2
star
31

whalebird.social.rails

Ruby
2
star
32

hanazawabot

Go
1
star
33

seiyu_watch

Elixir
1
star
34

playground-echo

Dockerfile
1
star
35

ex_slack_logger

A Slack logger backend for Elixir using :gen_event
Elixir
1
star
36

gitconnect

Ruby
1
star
37

envoy-grpc-example

gRPC server on envoy on kubernetes
1
star
38

charts

Helm charts created by h3poteto
Smarty
1
star
39

docker-rails

Dockerfile
1
star
40

dotfiles

Shell
1
star
41

livecamera

Rust
1
star
42

masuda_stream

Elixir
1
star
43

asumibot

阿澄病患者のための治療薬
JavaScript
1
star
44

sagisame

C
1
star
45

rollbar-shoryuken

Rollbar Plugin for Rails with Shoryuken
Ruby
1
star
46

fedistar.net

TypeScript
1
star
47

play-grpc-example

Scala
1
star
48

k8s-services

My kubernetes services definitions
1
star