-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (64 loc) · 2.23 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
import { readFileSync, existsSync, writeFileSync } from 'fs';
import { Contract } from 'crossbell.js'
import got from 'got';
import config from "./config.json" assert { type: 'json' };
const statusFileName = "status.json";
(async() => {
// Initialize contract
const contract = new Contract(config.ethereumPrivateKey);
// Collect feed
let feedData = [];
try {
const response = await got.get(config.feedLink).json();
feedData = response.items;
console.log("Collected feed counts", feedData.length);
} catch (e) {
console.error("Failed to collect feed, error", e);
return
}
// Read last run time
let status = {
lastRun: new Date(0), // From the beginning
};
if (existsSync(statusFileName)) {
try {
const statusBuf = await readFileSync(statusFileName);
status = JSON.parse(statusBuf.toString('utf8'));
console.log("Last run", status.lastRun);
} catch (e) {
console.warn("Failed to get last run data, error", e);
}
}
// Filter feed
const filteredFeed = feedData.filter(feed => new Date(feed.date_published) > status.lastRun);
console.log("Feed filtered, remain: ", filteredFeed.length);
// Sort feed
filteredFeed.sort((feedA, feedB) => new Date(feedA.date_published) - new Date(feedB.date_published))
console.log("Feed sorted");
// Parse feed ( https://docs.crossbell.io/docs/specs/metadata/note-metadata )
const parsedFeed = filteredFeed.map(feed => ({
title: feed.title,
content: feed.content_html,
sources: ["liteSync"],
date_published: feed.date_published,
}));
console.log("Feed parsed");
// Post to Crossbell
for (const feed of parsedFeed) {
try {
const postNoteResponse = await contract.postNote(
config.targetCharacter,
feed
);
console.log("Note posted on crossbell with id", postNoteResponse.data.noteId, "tx", postNoteResponse.transactionHash)
status.lastRun = feed.date_published; // Mark as last successfully posted note's published date
} catch (e) {
console.warn("Failed to post note since", feed, ", error", e);
break
}
}
// Save current run
console.log(`Saving status...`);
writeFileSync(statusFileName, JSON.stringify(status));
console.log("Sync finished!")
})();