Skip to content

Commit

Permalink
Save Account info
Browse files Browse the repository at this point in the history
  • Loading branch information
SnO2WMaN committed Dec 19, 2023
1 parent 54a2edf commit f5e0bd2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# deps
node_modules/

# dumps
dumps/

# output
dist/

# env
.env
!.env.example
.envrc
28 changes: 28 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: otomadb-nicovideo-updater
version: "3.9"

services:
postgres:
image: postgres:15@sha256:bec340fb35711dd4a989146591b6adfaac53e6b0c02524ff956c43b054d117dd
healthcheck:
test: pg_isready
interval: 10s
timeout: 5s
retries: 5
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: test
ports:
- target: 5432
published: 25432

postgres-migration:
build:
context: ./prisma
dockerfile: Dockerfile
depends_on:
postgres:
condition: service_healthy
environment:
PRISMA_DATABASE_URL: "postgres://user:pass@postgres:5432/test"
2 changes: 1 addition & 1 deletion prisma
49 changes: 42 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,42 @@ const logger = winston.createLogger({

if (process.env.NODE_ENV !== "production") {
logger.add(
new winston.transports.Console({ format: winston.format.simple() }),
new winston.transports.Console({ format: winston.format.simple() })
);
}

const sources = await prisma.nicovideoVideoSource.findMany({
where: { registeredAt: null },
where: {
OR: [{ registeredAt: null }, { account: null }],
},
select: {
id: true,
sourceId: true,
},
take: 100,
});

logger.info({ message: "Remain missing data count", count: sources.length });
logger.info({ message: "Estimated update count", count: sources.length });

const tx: ReturnType<typeof prisma.nicovideoVideoSource.update>[] = [];
for (const { id, sourceId } of sources) {
const url = new URL(
`/api/watch/v3_guest/${sourceId}`,
"https://www.nicovideo.jp",
"https://www.nicovideo.jp"
);
url.searchParams.set("_frontendId", "6");
url.searchParams.set("_frontendVersion", "0");
url.searchParams.set("skips", "harmful");
url.searchParams.set(
"actionTrackId",
`${Math.random().toString(36).substring(2)}_${Date.now()}`,
`${Math.random().toString(36).substring(2)}_${Date.now()}`
);

try {
logger.debug({
message: "Fetching from Nicovideo api",
url: url.toString(),
});
const json = await ky
.get(url.toString(), {
timeout: 5000,
Expand All @@ -52,6 +59,7 @@ for (const { id, sourceId } of sources) {
const {
data: {
video: { registeredAt },
owner,
},
} = z
.object({
Expand All @@ -67,15 +75,42 @@ for (const { id, sourceId } of sources) {
return new Date(arg);
}, z.date()),
}),
owner: z
.object({
id: z.number(),
nickname: z.string(),
iconUrl: z.string().url(),
})
.nullable(),
}),
})
.parse(json);

tx.push(
prisma.nicovideoVideoSource.update({
where: { id },
data: { registeredAt: registeredAt },
}),
data: {
registeredAt: registeredAt,
account: owner
? {
connectOrCreate: {
where: {
accountId: owner.id.toString(10),
},
create: {
accountId: owner.id.toString(10),
name: owner.nickname,
},
/*
update: {
name: owner.nickname,
},
*/
},
}
: undefined,
},
})
);
logger.info({
message: "Fetch from Nicovideo api successfully",
Expand Down

0 comments on commit f5e0bd2

Please sign in to comment.