-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.ts
162 lines (147 loc) · 4.51 KB
/
shared.ts
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
158
159
160
161
162
import { Generated, sql } from "kysely";
import { Kysely, PostgresDialect } from "kysely"
import pkg from 'pg';
import Cursor from 'pg-cursor'
const { Pool } = pkg;
import 'dotenv/config'
import { IPv4 } from "ip-num/IPNumber.js";
import { parse } from 'csv-parse';
import { createReadStream } from 'fs'
import { dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
interface LtcBase {
lettermessage: string;
lettertags: string;
letterup: number;
letterlevel: number;
letterlanguage: string;
senderip: string;
sendercountry: string;
senderregion?: string;
sendercity?: string;
lettercomments: number
tofacebookuid?: number
fromfacebookuid?: number
}
export interface LtcTable extends LtcBase {
id: Generated<number>
letterpostdate: Date;
hidden: Generated<boolean>
lmts: "tsquery"
}
export interface LtcJson extends LtcBase {
Id: number
letterPostDate: string
}
export interface LtcCommentBase {
commentmessage: string
commentername: string
letterid: number
sendemail?: boolean
hearts: number
commenteremail: string
commenterguid?: string
level?: number
commenterip: string
}
export interface LtcCommentJSON extends LtcCommentBase {
Id: number
commentdate: string
}
export interface LtcCommentTable extends LtcCommentBase {
id: Generated<number>
commentdate: Date
viadisqus?: true
extradisqusmetadata: any
}
interface Database {
ltc: LtcTable
ltccomments: LtcCommentTable
}
export const pool = new Pool({
database: process.env.db,
host: process.env.host || "localhost",
user: process.env.user,
password: process.env.pass
})
export const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: pool,
cursor: Cursor
})
})
export async function writeLettersToDB(letters: LtcTable[]) {
letters = letters.map((letter) => {
// @ts-expect-error
if (letter.letterlevel == -1) letter.hidden = true
return letter
})
return await db
.insertInto('ltc')
.values(letters)
.onConflict(oc => {
return oc
.column('id')
.doUpdateSet({
letterup: sql`excluded.letterup`,
letterlevel: sql`excluded.letterlevel`,
lettercomments: sql`excluded.lettercomments`,
lettermessage: sql`excluded.lettermessage`
})
})
.execute()
}
export function patchJson(json: LtcJson[]): LtcTable[] {
return json.map(letter => {
// @ts-expect-error
const newLetter: LtcTable = Object.fromEntries(
Object.entries(letter).map(([k, v]) => [k.toLowerCase(), v])
);
newLetter.lettermessage = newLetter.lettermessage.replaceAll(/[\x00]/g, '')
newLetter.letterpostdate = new Date(Number(letter.letterPostDate.split('/Date(')[1].split(')/')[0]))
return newLetter
})
}
export async function writeCommentsToDB(comments: LtcCommentTable[]) {
return await db
.insertInto('ltccomments')
.values(comments)
.onConflict(oc => {
return oc
.column('id')
.doUpdateSet({
hearts: sql`excluded.hearts`
})
})
.execute()
}
export function patchCommentJson(json: LtcCommentJSON[]): LtcCommentTable[] {
return json.map(comment => {
// @ts-expect-error
const newComment: LtcCommentTable = Object.fromEntries(
Object.entries(comment).map(([k, v]) => [k.toLowerCase(), v])
);
newComment.commentmessage = newComment.commentmessage.replaceAll(/[\x00]/g, '')
// @ts-expect-error
newComment.commentdate = new Date(Number(newComment.commentdate.split('/Date(')[1].split(')/')[0]))
return newComment
})
}
const IpDB: [number, number, string][] = await new Promise(function (resolve, reject) {
const result: [number, number, string][] = [];
createReadStream(__dirname + '/node_modules/@ip-location-db/asn-country/asn-country-ipv4-num.csv')
.pipe(parse())
.on("data", (data) => {
result.push([Number(data[0]), Number(data[1]), data[2]]);
})
.on('end', () => resolve(result))
.on('error', reject)
})
export function getLocation(ip: IPv4) {
for (const entry of IpDB) {
if (entry[0] < ip.getValue() && ip.getValue() < entry[1]) {
return entry[2]
}
}
}