-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
151 lines (132 loc) · 4.54 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
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
import getMetaData from 'metadata-scraper';
import fs from "fs";
import converter from 'json-2-csv';
import fetch from "node-fetch";
import {Client} from "@notionhq/client";
import extractUrls from "extract-urls";
import {} from 'dotenv/config';
import { readFile } from 'fs/promises';
import isUp from 'is-up';
const categories = JSON.parse(await readFile(new URL('./categories.json', import.meta.url)));
(async () => {
const notion = new Client({ auth: process.env.NOTION})
const databaseId = process.env.DB
let results = [];
let response = await notion.databases.query({
database_id: databaseId,
sorts: [
{
property: 'Category',
direction: 'ascending',
},
{
property: 'Title',
direction: 'ascending',
},
],
});
results = [...response.results];
while (response.has_more) {
response = await notion.databases.query({
database_id: databaseId,
start_cursor: response.next_cursor,
sorts: [
{
property: 'Category',
direction: 'ascending',
},
{
property: 'Title',
direction: 'ascending',
},
],
});
results = [...results, ...response.results];
}
console.log('Results:',results.length)
let md=''
let cate=''
let csv=[]
for (let cate in categories){
md+='## '+cate+'\n';
md+=categories[cate]+'\n<br />\n<br />'
console.log(cate)
for(let i in results){
let row=results[i]
let category=row.properties.Category?.select?.name
if(category!=cate)continue
let url=row.properties.URL?.url
let title=row.properties.Title?.title[0].plain_text
let curated=row.properties.Curated?.checkbox
let partners=row.properties.Partners?.checkbox
let type=row.properties.Type?.select?.name
let hackaton=row.properties.Hackaton?.select?.name
let description=row.properties.Description?.rich_text[0]?.plain_text
let cc=row.properties.cc?.rich_text[0]?.plain_text
let launch_date=row.properties.Launch?.date?.start
csv.push({title:title,launch_date:launch_date,description:description,cc:cc,type:type,curated:curated,hackaton:hackaton,category:category})
if(curated && type!='Partner'){
md+='**['+title+']('+url+')**'+'<br />';
md+=(description)+'<br />';
cc=cc?.split(',')
/*let cc2='cc '
for(let i in cc) cc2+='@'+cc[i]+' '
md+=(cc?cc2:'cc undefined')*/
md+=('<br />\n')
//updateMeta(notion,row)
}
//getTwitter()
}
}
converter.json2csv(md, (err, csv) => {fs.writeFileSync('README.md', md) })
converter.json2csv(csv, (err, csv) => {fs.writeFileSync('map.csv', csv) })
})();
async function getTwitter(url){
let twitter=''
try{
let text=await fetch(url).then((result) => { return result.text(); })
if(text){
let urls = extractUrls(text);
if(urls){
for (var j = 0; j < urls.length; j++) {
if (urls[j].includes("twitter.com")) {
twitter=urls[j].split('?')[0]
twitter.replace('https://twitter.com/','@')
//break;
break
}
}
}
}
if(twitter){
console.log('tw',twitter)
const response = await notion.pages.update({
page_id: row.id ,
properties: { 'twitter': { url:twitter }, },
});
console.log(response)
}
}catch(e){
}
}
async function updateMeta(notion,row){
console.log(row)
let url=row.properties.URL?.url
let url_icon=row.icon
let url_cover=row.cover
if(!url_icon?.external?.url || !url_cover?.external?.url){
let data=await getMetaData(url).then((data) => {return data})
console.log(data)
//csv[i]['Metadesc']=(data.description==undefined?'':data.description)
if(data.icon!=undefined)url_icon=data.icon
if(data.cover!=undefined)url_cover=data.image
let obj={page_id: row.id }
if(url_cover)obj.cover={external:{url:url_cover}}
if(url_icon)obj.icon={external:{url:url_icon}}
if(url_icon)obj.twitter={external:{url:url_icon}}
if(obj.cover?.external || obj.icon?.external){
const response2 = await notion.pages.update(obj)
console.log(response2.properties.Title.title[0].plain_text)
}
}
}