在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):h3poteto/megalodon开源软件地址(OpenSource Url):https://github.com/h3poteto/megalodon开源编程语言(OpenSource Language):TypeScript 99.4%开源软件介绍(OpenSource Introduction):MegalodonA 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.0There are some breaking changes, so you can not update megalodon out of the box. Please refer migration guide before you update megalodon version. Features
Install
or
Build for browserImportant: In browser, you can not use proxy. If you want to build for browser, please use Webpack and set empty value for these libraries.
node: {
net: 'empty',
tls: 'empty',
dns: 'empty'
} These libraries are for node.js, so can not use in browser. Here is example Webpack configuration. UsageI 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 timelineimport 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 tootimport 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 mediasPlease 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 streamingMastodon, 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 StreamingMastodon 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.')
}) AuthorizationYou 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 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 SNSYou have to provide SNS name import { detector } from 'megalodon'
const URL = 'https://misskey.io'
const sns = await detector(URL)
console.log(sns) LicenseThe software is available as open source under the terms of the MIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论