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
export const untitled_lavenderGoat = (async () => {
/*
function PostTestToLemmy(
instance: string,
communityId: number,
content: string,
) {
}
*/
const { LemmyHttp } = await import("npm:lemmy-js-client@0.18.1");
let client = new LemmyHttp(`https://sh.itjust.works`, {
fetchFunction: fetch,
});
//client.createPost({
client.getCommunity({
//name: 'test_post',
id: 6805,
//url?: string,
//body?: 'test post',
//honeypot?: string,
//nsfw?: false,
//language_id?: LanguageId,
//auth: string,
});
/*client.createPost({
name: 'test_post',
community_id: 6805,
//url?: string,
body?: 'test post',
//honeypot?: string,
nsfw?: false,
language_id?: LanguageId,
auth: string,
});
*/
/*
async function syncCommentToLemmy(
instance: string,
redditUrl: string,
postId: number,
auth: string,
matcher?: (author: string, content: string) => boolean,
) {
function hashCode(str) {
let hash = 0;
for (let i = 0, len = str.length; i < len; i++) {
let chr = str.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
const { LemmyHttp } = await import("npm:lemmy-js-client@0.18.1");
let client = new LemmyHttp(`https://${instance}`, {
fetchFunction: fetch,
});
// (client as any)["#fetchFunction"] = fetch;
let comments = (await @me.fetchRss(redditUrl)).filter((i) =>
new Date(i.isoDate) >= new Date(Date.now() - 1000 * 60 * 60 * 24)
)
.filter((i) => matcher ? matcher(i.author, i.contentSnippet) : true);
const ret = [];
for (const comment of comments) {
const linkParts = comment.link.split("/");
const commentId = linkParts[linkParts.length - 2];
console.log("Found comment with id " + commentId);
const contentHash = hashCode(comment.contentSnippet);
const syncedContent =
`New comment from ${comment.author} [on Reddit](${comment.link}):\n\n ` +
comment.contentSnippet.replace(/\n/g, "\n\n") + `\n\n(This gets synced)`;
const existingStatus =
@pdebie.spacexLemmyDb.commentMap[commentId];
// New comment, let's create one
if (!existingStatus) {
// Step 1: write temp to DB so we don't try to recreate forever
@pdebie.spacexLemmyDb.commentMap[commentId] = {
lemmyId: undefined,
};
const ret = await client.createComment({
post_id: postId,
content: syncedContent,
auth,
});
// Step 2: Create comment on Lemmy
// Step 3: Update Database
@pdebie.spacexLemmyDb.commentMap[commentId] = {
lemmyId: ret.comment_view.comment.id,
contentHash,
};
}
else {
if (!existingStatus.lemmyId) {
console.log(
`Skipping comment ${commentId} because the previous run failed`,
);
continue;
}
if (existingStatus.contentHash !== contentHash) {
await client.editComment({