-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.js
executable file
·138 lines (119 loc) · 3.61 KB
/
extract.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
#!/usr/bin/env node
const mysql = require('promise-mysql')
const config = require('./db_config')
const moment = require('moment')
const fs = require('fs')
const Jetty = require('jetty')
const jetty = new Jetty(process.stdout)
jetty.clear()
const RESULT_FILENAME = 'result.json'
async function main() {
const finalStructure = []
const fresh = []
try {
var connection = await mysql.createConnection(config)
const results = await getResults()
var counter = 0
const limit = process.argv[2] || false
for (let result of results) {
const parsedQuery = {
ylinks: [],
glinks: []
}
parsedQuery.rep = result.ball
parsedQuery.stress = result.stress_ball
const date = new Date(result.updated_at)
parsedQuery.date = `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}`
const query = await getQueryByID(result.query_id)
parsedQuery.text = query.name
parsedQuery.color = query.color
const owner = await getUserByID(query.user_id)
parsedQuery.owner = owner.email
const region = await getRegionByID(query.region_id)
parsedQuery.region = region.id
var ycover = 0
var gcover = 0
const positions = await getPositionsByID(result.id)
for (let position of positions) {
if (position.site_stats_id) {
var siteStats = await getSiteStatsByID(position.site_stats_id)
}
const link = {
link: position.url,
ton: mapTons(position.tonality),
}
const coverage = siteStats && siteStats.visits || undefined
if (typeof coverage !== 'undefined') {
link.cover = coverage
}
if (position.system === 'ya') {
if (coverage) {
ycover += coverage
}
parsedQuery.ylinks.push(link)
}
if (position.system === 'google') {
if (coverage) {
gcover += coverage
}
parsedQuery.glinks.push(link)
}
}
parsedQuery.ycover = ycover
parsedQuery.gcover = gcover
counter++
jetty.clearLine()
jetty.moveTo([0, 0])
jetty.text('Work in progress - ' + (counter / results.length * 100).toFixed(2) + '%')
if (moment(date).isSame(Date.now(), 'day')) {
fresh.push(parsedQuery)
} else {
finalStructure.push(parsedQuery)
}
if (limit && counter >= limit) break
}
fs.writeFileSync(RESULT_FILENAME, JSON.stringify({
fresh,
old: finalStructure
}))
console.log('done')
process.exit(1)
}
catch (e) {
console.log(e)
process.abort()
}
async function getPositionsByID(resultID) {
return await connection.query('SELECT * FROM positions WHERE `result_id`=' + resultID)
}
async function getResults() {
return await connection.query('SELECT * FROM results')
}
async function getQueryByID(id) {
const result = await connection.query('SELECT * FROM queries WHERE `id`=' + id)
return result[0]
}
async function getUserByID(id) {
const result = await connection.query('SELECT * FROM users WHERE `id`=' + id)
return result[0]
}
async function getRegionByID(id) {
const result = await connection.query('SELECT * FROM regions WHERE `id`=' + id)
return result[0]
}
async function getSiteStatsByID(id) {
const result = await connection.query('SELECT * FROM site_stats WHERE `id`=' + id)
return result[0]
}
}
function mapTons(ton) {
const mapper = {
owner: 'sobst',
neutral: 'neit',
positive: 'pos',
negative: 'neg'
}
if (!mapper.hasOwnProperty(ton)) return ton
return mapper[ton]
}
main()