All Projects → h3poteto → Megalodon

h3poteto / Megalodon

Licence: mit
Mastodon, Pleroma and Misskey API client library for node.js and browser

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Megalodon

Pushpin
Proxy server for adding push to your API
Stars: ✭ 3,050 (+5765.38%)
Mutual labels:  proxy, websockets, streaming
Websocket Client
🔧 .NET/C# websocket client library
Stars: ✭ 297 (+471.15%)
Mutual labels:  websockets, client
Php Curl Class
PHP Curl Class makes it easy to send HTTP requests and integrate with web APIs
Stars: ✭ 2,903 (+5482.69%)
Mutual labels:  proxy, client
Graphql Ws
Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
Stars: ✭ 398 (+665.38%)
Mutual labels:  websockets, client
mastodon.js
Javascript Mastodon API for Web Browsers with streaming support
Stars: ✭ 32 (-38.46%)
Mutual labels:  streaming, mastodon
Geluid
Made with Electron. Streams audio from your soundcard to a browser in an easy way
Stars: ✭ 29 (-44.23%)
Mutual labels:  streaming, websockets
Tunneller
Allow internal services, running on localhost, to be accessed over the internet..
Stars: ✭ 346 (+565.38%)
Mutual labels:  proxy, websockets
Tooty
An alternative multi-accounts Web client for Mastodon.
Stars: ✭ 124 (+138.46%)
Mutual labels:  mastodon, client
Tor2web
Tor2web is an HTTP proxy software that enables access to Tor Hidden Services by mean of common web browsers
Stars: ✭ 531 (+921.15%)
Mutual labels:  proxy, streaming
Node Minecraft Protocol
Parse and serialize minecraft packets, plus authentication and encryption.
Stars: ✭ 697 (+1240.38%)
Mutual labels:  proxy, client
Spring Streaming
SPA on Spring Boot 1.x, WebSockets and React, gradle, nodejs, spring-boot, gradle multi project, spring-mvc, spring-data, gradle dependency update plugin, react-router
Stars: ✭ 6 (-88.46%)
Mutual labels:  websockets, streaming
live-cryptocurrency-streaming-flutter
A Flutter app with live cryptocurrency updates, powered by Ably
Stars: ✭ 26 (-50%)
Mutual labels:  streaming, websockets
Sengi
Mastodon & Pleroma Multi-account Desktop Client
Stars: ✭ 133 (+155.77%)
Mutual labels:  mastodon, client
Madonctl
CLI client for the Mastodon social network API
Stars: ✭ 129 (+148.08%)
Mutual labels:  mastodon, client
Gitter Api
[production-ready] Gitter API implementation for php 7.0+ allowing sync, async and streaming access.
Stars: ✭ 11 (-78.85%)
Mutual labels:  streaming, client
Sshy
HTML5 SSH Web Client
Stars: ✭ 334 (+542.31%)
Mutual labels:  websockets, client
Websocketd
Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.
Stars: ✭ 15,828 (+30338.46%)
Mutual labels:  proxy, websockets
Mastodon Api
Mastodon API Client Library
Stars: ✭ 89 (+71.15%)
Mutual labels:  mastodon, streaming
Swell
Swell: API development tool that enables developers to test endpoints served over streaming technologies including Server-Sent Events (SSE), WebSockets, HTTP2, GraphQL, and gRPC.
Stars: ✭ 517 (+894.23%)
Mutual labels:  websockets, streaming
Swaddle
Automagically create API clients/wrappers in JavaScript
Stars: ✭ 23 (-55.77%)
Mutual labels:  proxy, client

Megalodon

Test NPM Version GitHub release npm NPM

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

!!Migrate v2.x to v3.0.0

There are some breaking changes, so you can not update megalodon out of the box. Please refer migration guide before you update megalodon version.

Features

  • REST API
  • Streaming with Server-Sent Event
  • Streaming with WebSocket
  • 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 these libraries.

  • net
  • tls
  • dns
  node: {
    net: 'empty',
    tls: 'empty',
    dns: 'empty'
  }

These libraries are for node.js, so can not use in browser.

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)
})

HTTP Streaming

Mastodon provides HTTP streaming.

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

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

const client = generator('mastodon', BASE_URL, access_token)
const stream: StreamListenerInterface

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.')
})

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].