-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
126 lines (107 loc) · 3.61 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
import {JSDOM} from 'jsdom'
import {select} from 'd3-selection'
import * as fs from 'fs'
import cohost from 'cohost'
import {apdate} from 'journalize'
const WIDTH = 800;
(() => {
let colors = fs.readFileSync('data/rgb.txt',{encoding:'utf-8'})
colors = colors.split('\n').map(c => {
return c.split(',')
})
let adjectives = fs.readFileSync('data/adjectives.txt',{encoding:'utf-8'})
adjectives = adjectives.split('\r\n')
const layout = [
[3,2,5],
[4,2,4],
[2,2,6],
[3,1,6],
[4,1,5]
][Math.floor(Math.random(5)) * 5]
let scheme = layout.map(d => {
return {
height:WIDTH/10*d,
color:colors[Math.floor(Math.random(colors.length) * colors.length)],
adjective:adjectives[Math.floor(Math.random(adjectives.length) * adjectives.length)]
}
})
const dom = new JSDOM(`
<div class="color-names"></div>
<div class="color-container"></div>
`,{
runScripts:'outside-only',
pretendToBeVisual:true
})
const doc = select(dom.window.document)
const svg = doc.select('.color-container')
.append('svg')
.attr('width',WIDTH)
.attr('height',WIDTH)
const nameContainer = doc.select('.color-names')
for(let i = 0; i < 3; i++){
svg.append('rect')
.attr('y',() => {
switch(i){
case 0:{
return 0
}
case 1:{
return scheme[0].height
}
case 2:{
return scheme[0].height + scheme[1].height
}
}
})
.attr('width',800)
.attr('height',scheme[i].height)
.attr('fill',scheme[i].color[1])
}
const rotation = Math.floor(Math.random(3)*3)
if([1,2].includes(rotation)){
scheme = scheme.reverse()
}
for(let i = 0; i < 3; i ++){
let details = nameContainer.append('details')
details.append('summary')
.html(`${scheme[i].adjective} ${scheme[i].color[0]}`)
details.append('span')
.style('font-family','monospace')
.html(scheme[i].color[1])
}
const XMLSerializer = dom.window.XMLSerializer
let serializer = new XMLSerializer()
let dataURL = `url("data:image/svg+xml;base64,${dom.window.btoa(serializer.serializeToString(dom.window.document.querySelector('svg')))}")`
doc.select('.color-container')
.style('width','100%')
.style('max-width','800px')
.style('padding-bottom','100%')
.style('margin-top','1rem')
.style('background-image',dataURL)
.style('background-size','contain')
.style('background-repeat','no-repeat')
.style('transform',`rotate(${rotation * 90}deg)`)
svg.remove()
const chost = dom.window.document.body.innerHTML;
(async () => {
let user = new cohost.User()
await user.login(process.env.COHOST_USER,process.env.COHOST_PW)
const proj = await user.getProjects()
const account = proj.filter(d => d.handle === 'colorschemez')[0]
await cohost.Post.create(account,{
postState: 1,
headline:apdate(new Date()),
adultContent:chost.includes['fuck','shit','sex'] ? true : false,
blocks:[
{
type:'markdown',
markdown:{
content:chost
}
}
],
cws:[],
tags:['bot']
})
})()
})();