-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (79 loc) · 2.81 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
79
80
81
82
83
84
85
86
87
88
const dotenv = require("dotenv")
const { Client } = require("@notionhq/client")
const moment = require("moment")
dotenv.config()
const notion = new Client({ auth: process.env.NOTION_API_KEY })
const databaseId = process.env.NOTION_DATABASE_ID
const ytApiKey = process.env.YOUTUBE_API_KEY
const fetchYtData = async (url) => {
const videoId = url.slice(32)
const res = await fetch(`https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id=${videoId}&key=${ytApiKey}`)
const data = await res.json()
return {
name: data.items[0].snippet.title,
author: data.items[0].snippet.channelTitle,
duration: moment.duration(data.items[0].contentDetails.duration).asSeconds(),
link: url,
channelId: data.items[0].snippet.channelId
}
}
const addConsumption = async ( { name, author, duration, link, channelId } ) => {
try{
const Now = new Date()
const After = new Date(Date.now() + duration * 1000)
const res = await notion.pages.create({
parent: {
'database_id': databaseId
},
properties: {
Name: {
type: 'title',
title: [{ type: 'text', text: { content: name, link: {url: link}} }]
},
Author: {
type: 'rich_text',
rich_text: [{ type: 'text', text: { content: author, link: {url: `https://www.youtube.com/channel/${channelId}`} } }]
},
Status: {
status: {
name: 'Done'
}
},
Rating: {
select: {
name: 'N/A'
}
},
Duration: {
type: "number",
number: Math.round(duration / 60)
// rich_text: [{ type: 'text', text: { content: Math.round(duration / 60).toString() } }]
},
Date: {
type: 'rich_text',
rich_text: [
{
type: 'mention',
mention: {
type: 'date',
date: {
time_zone: "America/Chicago",
start: Now.toISOString(),
end: After.toISOString()
}
}
}
]
},
}
})
console.log(res)
} catch (err) {
console.log(err)
}
}
const main = async () => {
const data = await fetchYtData(process.argv[2])
addConsumption(data)
}
main()