-
Notifications
You must be signed in to change notification settings - Fork 15
/
import.js
119 lines (111 loc) · 2.59 KB
/
import.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
const debug = require('debug')('import');
const axios = require('axios');
const env = require('common-env/withLogger')(console);
const config = env.getOrElseAll({
mastodon: {
api: {
key: {
$type: env.types.String,
},
basePath: {
$type: env.types.String,
}
}
},
twitter: {
excludeReplies: true,
year: new Date().getFullYear(),
tweetjs: {
filepath: {
$type: env.types.String
},
}
}
});
function getTweets() {
const vm = require('vm');
const fs = require('fs');
const _global = {
window: {
YTD: {
tweets: {
part0: {}
}
}
}
};
debug('Loading tweets...')
const script = new vm.Script(fs.readFileSync(config.twitter.tweetjs.filepath, 'utf-8'));
const context = vm.createContext(_global);
script.runInContext(context);
const tweets = Object.keys(_global.window.YTD.tweets.part0).reduce((m, key, i, obj) => {
return m.concat(_global.window.YTD.tweets.part0[key].tweet);
}, []).filter(_keepTweet)
debug('Loading %s tweets...', tweets.length);
function _keepTweet(tweet) {
if (!tweet.created_at.endsWith(config.twitter.year)) {
return false;
}
if (!config.twitter.excludeReplies) {
return true;
}
return !tweet.full_text.startsWith('@');
}
return tweets;
}
function importTweets(tweets) {
const progress = require('progressbar').create().step('Importing tweets');
const max = tweets.length;
progress.setTotal(max);
function next() {
const tweet = tweets.pop();
let current = 0;
if (!tweet) {
debug('Tweets import completed');
return;
}
createMastodonPost({
apiToken: config.mastodon.api.key,
baseURL: config.mastodon.api.basePath
},{
status: replaceTwitterUrls(tweet.full_text,tweet.entities.urls),
language: tweet.lang
}).then((mastodonPost) => {
debug('%s/%i Created post %s', current, max, mastodonPost.url);
progress.addTick();
next();
}).catch(err => {
console.log(err);
});
}
}
function replaceTwitterUrls(full_text, urls) {
urls.forEach(url => {
full_text=full_text.replace(url.url,url.expanded_url);
});
return full_text;
}
function createMastodonPost({
apiToken,
baseURL
}, {
status,
language
}) {
return axios({
url: '/api/v1/statuses',
baseURL: baseURL,
method: 'POST',
headers: {
'Authorization': 'Bearer ' + apiToken
},
data: {
status,
language,
visibility: "public"
}
}).then(function(response) {
return response.data
});
}
importTweets(getTweets());