-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·157 lines (126 loc) · 3.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
import { exit } from 'node:process';
import { parseArgs } from 'node:util';
import { mkdir } from 'node:fs/promises';
import { lookupAccount } from './api/lookup-account.js';
import { config } from './lib/config.js';
import { getPackageData } from './lib/get-package-data.js';
import { readTimelineFile } from './lib/timeline/read-timeline-file.js';
import { writeTimelineFile } from './lib/timeline/write-timeline-file.js';
import { dropOld } from './lib/timeline/drop-old.js';
import { groupByAccount } from './lib/timeline/group-by-account.js';
import { fetchHomeTimelineUntilId } from './lib/timeline/fetch-home-timeline-until-id.js';
import { getFullAccountName } from './lib/timeline/get-full-account-name.js';
import { renderRssFeed } from './lib/rss/render-rss-feed.js';
import { renderRssChannel } from './lib/rss/render-rss-channel.js';
import { writeRssFile } from './lib/rss/write-rss-file.js';
import { fetchAllFollowing } from './lib/following/fetch-all-following.js';
import { registerApp } from './auth/register-app.js';
import { login } from './auth/login.js';
async function makeRssDirectory()
{
await mkdir(
config.rssDirectoryPath,
{ recursive: true },
);
}
async function runLogin()
{
console.log( 'Running login command...' );
if ( !config.clientId || !config.clientSecret )
{
await registerApp();
}
await login();
console.log( '✓ Successfully logged in.' );
}
async function runGenerate()
{
console.log( 'Running generate command...' );
const previousTimeline = await readTimelineFile();
const lastId = previousTimeline[0]?.id;
const nextTimeline = await fetchHomeTimelineUntilId( lastId );
if ( nextTimeline.length === 0 )
{
return;
}
const timeline = [...nextTimeline, ...previousTimeline];
dropOld( timeline );
const groups = groupByAccount( timeline, lastId );
await makeRssDirectory();
for ( const [accountName, statuses] of groups )
{
await writeRssFile( accountName, renderRssFeed( statuses ) );
console.log( accountName, statuses.length );
}
await writeTimelineFile( timeline );
}
/**
* @param { string } acct
*/
async function runPrepareFollowing( acct )
{
console.log( `Running RSS channels generation for ${acct} following...` );
const targetAccount = await lookupAccount( { acct } );
const followingAccounts = await fetchAllFollowing( targetAccount.id );
await makeRssDirectory();
for ( const account of followingAccounts )
{
const name = getFullAccountName( account );
await writeRssFile( name, renderRssChannel( account, '' ), true );
console.log( name );
}
}
async function main()
{
const { values } = parseArgs({
options: {
config: {
type: 'string',
short: 'c',
},
instance: {
type: 'string',
short: 'i',
},
login: {
type: 'boolean',
},
'prepare-following-for': {
type: 'string',
},
},
});
await config.load( values.config );
if ( values.instance )
{
await config.set({
instance: values.instance,
});
}
const packageData = getPackageData();
console.log(
`Starting ${packageData.name} ${packageData.version}
with config: ${config.configPath}
for instance: ${config.instance}`,
);
if ( values.login )
{
await runLogin();
}
else if ( values['prepare-following-for'] )
{
await runPrepareFollowing( values['prepare-following-for'] );
}
else
{
await runGenerate();
}
}
main().catch(
( error ) =>
{
console.error( error );
exit( 1 );
},
);