This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter-app
executable file
·98 lines (82 loc) · 3.16 KB
/
twitter-app
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
#!/usr/bin/env node
'use strict';
// This is a proof of concept. The code below is ugly, inefficient and has no tests.
const Endpoint = require('../core/endpoint');
const fs = require('fs');
const path = require('path');
const PoWebSocketClient = require('../PoWebSocket/client');
const os = require('os');
const readline = require('readline');
const TwitterClient = require('../twitter-service/client');
const {Tweet, deserializeMessage, serializeMessage} = require('../twitter-service/service_messages');
require('dotenv').config();
const CERTS_DIR_PATH = path.join(path.dirname(__dirname), 'certs');
const CERT_PATH = path.join(CERTS_DIR_PATH, 'twitter-app.cert.pem');
const KEY_PATH = path.join(CERTS_DIR_PATH, 'twitter-app.key.pem');
const POWEBSOCKET_SOCKET_PATH = path.join(os.tmpdir(), 'user-gateway-data', 'powebsocket.sock');
const APP_FIRST_RUN_FILE_PATH = path.join(os.tmpdir(), 'twitter-app-first-run');
async function main() {
const pdcClient = new PoWebSocketClient(
`ws+unix://${POWEBSOCKET_SOCKET_PATH}`,
fs.readFileSync(CERT_PATH),
fs.readFileSync(KEY_PATH),
);
const endpoint = new Endpoint(CERT_PATH, KEY_PATH, pdcClient, serializeMessage, deserializeMessage);
const twitterClient = new TwitterClient(
process.env.TWITTER_ACCESS_TOKEN_KEY,
process.env.TWITTER_ACCESS_TOKEN_SECRET,
endpoint,
);
if (fs.existsSync(APP_FIRST_RUN_FILE_PATH)) {
console.log('You were previously subscribed to updates to your timeline.');
console.log(`Remove ${APP_FIRST_RUN_FILE_PATH} to resubscribe.`);
} else {
await twitterClient.subscribeToUpdates();
fs.writeFileSync(APP_FIRST_RUN_FILE_PATH, '');
console.log(`You'll be subscribed to updates in the next relay.`);
}
console.log();
await printCollectedMessages(endpoint.collectMessages());
const tweetStatus = await promptTweetStatus();
if (tweetStatus) {
const tweet = Tweet.create({status: tweetStatus});
await twitterClient.postTweet(tweet);
console.log('✔️ Tweet added to queue.')
}
endpoint.close();
}
function promptTweetStatus() {
const rlInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rlInterface.question('What would you like to tweet? (Press ENTER to skip)\n', (tweetStatus) => {
rlInterface.close();
resolve(tweetStatus || null);
});
});
}
/**
* @param {AsyncIterableIterator<Message>} messages
* @returns {Promise<void>}
*/
async function printCollectedMessages(messages) {
console.log('New Tweets:');
let anyTweet;
for await (const message of messages) {
if (message.$type.name !== 'TimelineUpdateBatch') {
console.warn(`${message} was received but it isn't a timeline update`);
break;
}
for (const tweetMsg of message.tweets) {
console.log(`- ${tweetMsg.author} said "${tweetMsg.status}"`);
anyTweet = true;
}
}
if (!anyTweet) {
console.log('(no new tweets)')
}
console.log('');
}
(async () => await main())();