Skip to content

Commit 30949a9

Browse files
committed
Add Pixelfed client
1 parent b8f897b commit 30949a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4668
-6
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as readline from 'readline'
2+
import generator, { OAuth } from 'megalodon'
3+
4+
const rl: readline.ReadLine = readline.createInterface({
5+
input: process.stdin,
6+
output: process.stdout
7+
})
8+
9+
const SCOPES: Array<string> = ['read', 'write', 'follow']
10+
const BASE_URL: string = process.env.PIXELFED_URL!
11+
12+
let clientId: string
13+
let clientSecret: string
14+
15+
const client = generator('pixelfed', BASE_URL)
16+
17+
client
18+
.registerApp('Test App', {
19+
scopes: SCOPES
20+
})
21+
.then(appData => {
22+
clientId = appData.client_id
23+
clientSecret = appData.client_secret
24+
console.log('Authorization URL is generated.')
25+
console.log(appData.url)
26+
console.log()
27+
return new Promise<string>(resolve => {
28+
rl.question('Enter the authorization code from website: ', code => {
29+
resolve(code)
30+
rl.close()
31+
})
32+
})
33+
})
34+
.then((code: string) => {
35+
return client.fetchAccessToken(clientId, clientSecret, code)
36+
})
37+
.then((tokenData: OAuth.TokenData) => {
38+
console.log('\naccess_token:')
39+
console.log(tokenData.access_token)
40+
console.log('\nrefresh_token:')
41+
console.log(tokenData.refresh_token)
42+
console.log()
43+
})
44+
.catch((err: Error) => console.error(err))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import generator, { Entity, Response } from 'megalodon'
2+
3+
const BASE_URL: string = process.env.PIXELFED_URL!
4+
5+
const client = generator('pixelfed', BASE_URL)
6+
7+
client.getInstance().then((res: Response<Entity.Instance>) => {
8+
console.log(res.data)
9+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import generator, { MegalodonInterface, Entity, Response } from 'megalodon'
2+
3+
const BASE_URL: string = process.env.PIXELFED_URL!
4+
5+
const access_token: string = process.env.PIXELFED_ACCESS_TOKEN!
6+
7+
const client: MegalodonInterface = generator('pixelfed', BASE_URL, access_token)
8+
9+
client.getPublicTimeline().then((resp: Response<Array<Entity.Status>>) => {
10+
console.log(resp.data)
11+
})

megalodon/src/detector.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type Metadata = {
4747
* @param proxyConfig Proxy setting, or set false if don't use proxy.
4848
* @return SNS name.
4949
*/
50-
export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial'> => {
50+
export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial' | 'pixelfed'> => {
5151
const options: AxiosRequestConfig = {
5252
timeout: 20000
5353
}
@@ -73,6 +73,8 @@ export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'f
7373
return 'firefish'
7474
case 'mastodon':
7575
return 'mastodon'
76+
case 'pixelfed':
77+
return 'pixelfed'
7678
case 'pleroma':
7779
return 'pleroma'
7880
case 'sharkey':
@@ -101,6 +103,8 @@ export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'f
101103
return 'firefish'
102104
case 'mastodon':
103105
return 'mastodon'
106+
case 'pixelfed':
107+
return 'pixelfed'
104108
case 'pleroma':
105109
return 'pleroma'
106110
case 'sharkey':
@@ -129,6 +133,8 @@ export const detector = async (url: string): Promise<'mastodon' | 'pleroma' | 'f
129133
return 'firefish'
130134
case 'mastodon':
131135
return 'mastodon'
136+
case 'pixelfed':
137+
return 'pixelfed'
132138
case 'pleroma':
133139
return 'pleroma'
134140
case 'sharkey':

megalodon/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import generator, { MegalodonInterface, WebSocketInterface } from './megalodon'
55
import { detector } from './detector'
66
import Mastodon from './mastodon'
77
import Pleroma from './pleroma'
8+
import Pixelfed from './pixelfed'
89
import Firefish from './firefish'
910
import Gotosocial from './gotosocial'
1011
import Entity from './entity'
@@ -23,6 +24,7 @@ export {
2324
FilterContext,
2425
Mastodon,
2526
Pleroma,
27+
Pixelfed,
2628
Firefish,
2729
Gotosocial,
2830
Entity

megalodon/src/mastodon/api_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ namespace MastodonAPI {
368368
* Get connection and receive websocket connection for Pleroma API.
369369
*
370370
* @param url Streaming url.
371-
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
371+
* @param stream Stream name
372372
* @returns WebSocket, which inherits from EventEmitter
373373
*/
374374
public socket(url: string, stream: string, params?: string): Streaming {

megalodon/src/mastodon/web_socket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export default class Streaming extends EventEmitter implements WebSocketInterfac
2727
private _pongWaiting: boolean = false
2828

2929
/**
30-
* @param url Full url of websocket: e.g. https://pleroma.io/api/v1/streaming
31-
* @param stream Stream name, please refer: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/web/mastodon_api/mastodon_socket.ex#L19-28
30+
* @param url Full url of websocket: e.g. https://mastodon.social/api/v1/streaming
31+
* @param stream Stream name
3232
* @param accessToken The access token.
3333
* @param userAgent The specified User Agent.
3434
*/

megalodon/src/megalodon.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Entity from './entity'
66
import Friendica from './friendica'
77
import Firefish from './firefish'
88
import Gotosocial from './gotosocial'
9+
import Pixelfed from './pixelfed'
910

1011
export interface WebSocketInterface {
1112
start(): void
@@ -1436,14 +1437,14 @@ export class NodeinfoError extends Error {
14361437
/**
14371438
* Get client for each SNS according to megalodon interface.
14381439
*
1439-
* @param sns Name of your SNS, `mastodon`, `pleroma`, `firefish`, or `gotosocial`.
1440+
* @param sns Name of your SNS, `mastodon`, `pleroma`, `firefish`, `gotosocial`, or `pixelfed`.
14401441
* @param baseUrl hostname or base URL.
14411442
* @param accessToken access token from OAuth2 authorization
14421443
* @param userAgent UserAgent is specified in header on request.
14431444
* @return Client instance for each SNS you specified.
14441445
*/
14451446
const generator = (
1446-
sns: 'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial',
1447+
sns: 'mastodon' | 'pleroma' | 'friendica' | 'firefish' | 'gotosocial' | 'pixelfed',
14471448
baseUrl: string,
14481449
accessToken: string | null = null,
14491450
userAgent: string | null = null
@@ -1469,6 +1470,10 @@ const generator = (
14691470
const gotosocial = new Gotosocial(baseUrl, accessToken, userAgent)
14701471
return gotosocial
14711472
}
1473+
case 'pixelfed': {
1474+
const pixelfed = new Pixelfed(baseUrl, accessToken, userAgent)
1475+
return pixelfed
1476+
}
14721477
}
14731478
}
14741479

0 commit comments

Comments
 (0)