-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
243 lines (225 loc) · 8.76 KB
/
Copy pathserver.js
File metadata and controls
243 lines (225 loc) · 8.76 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const fs = require('fs')
const jsonxml = require('jsontoxml');
const escape = require('escape-html');
const express = require('express')
const { createClient } = require('lightrpc');
const app = express()
const port = process.env.PORT || 3000
const rootDomain = 'https://d.tube'
const lightrpc = createClient('https://api.steemit.com', {
timeout: 5000
});
const lightrpchive = createClient('https://anyx.io', {
timeout: 5000
});
const javalon = require('javalon')
let layouts = {}
app.get('*', function(req, res, next) {
// parsing the query
var reqPath = req.path
if (reqPath.startsWith('/oembed')) {
console.log(req.headers['user-agent'], req.query)
if (req.query.url && req.query.url.indexOf('/?_escaped_fragment_=') > -1)
req.query.url = req.query.url.replace('/?_escaped_fragment_=', '')
if (req.query.url && isValidDTubeUrl(req.query.url) ) {
getVideo(
req.query.url.split('/')[4],
req.query.url.split('/')[5],
function(err, html, pageTitle, description, url, snap, duration, snapHeight) {
if (err) {
if (err == 'Internal Error')
res.sendStatus(503)
else if (err.message.toString().startsWith('Request has timed out.'))
res.sendStatus(503)
else
res.sendStatus(404)
return;
}
var sizes = {
width: 480,
height: 270
}
if (req.query.maxwidth && req.query.maxheight) {
var userWidth = parseInt(req.query.maxwidth)
var userHeight = parseInt(req.query.maxheight)
if (convert169(userWidth) > userHeight) {
sizes.height = userHeight
sizes.width = convert169(sizes.height, false)
} else if (userWidth < convert169(userHeight, false)) {
sizes.width = userWidth
sizes.height = convert169(userWidth)
}
}
if (req.query.maxwidth && !req.query.maxheight) {
var userWidth = parseInt(req.query.maxwidth)
sizes.width = userWidth
sizes.height = convert169(userWidth)
}
if (!req.query.maxwidth && req.query.maxheight) {
var userHeight = parseInt(req.query.maxheight)
sizes.height = userHeight
sizes.width = convert169(sizes.height, false)
}
if (sizes.width<=200) {
sizes.width = 200
sizes.height = 113
}
var response = {
type: 'video',
version: '1.0',
provider_name: 'DTube',
provider_url: rootDomain,
title: pageTitle,
author_name: req.query.url.split('/')[4],
author_url: 'https://d.tube/#!/c/'+req.query.url.split('/')[4],
html: html,
width: sizes.width,
height: sizes.height,
duration: Math.round(duration),
description: description,
thumbnail_url: snap,
thumbnail_width: 210,
thumbnail_height: 118
}
if (snapHeight == 360) {
response.thumbnail_height = 360
response.thumbnail_width = 640
}
if (req.query.format == 'xml') {
res.set('Content-Type', 'text/xml');
response.html = escape(response.html)
var xml = jsonxml(response)
xml += '</oembed>'
xml = '<oembed>' + xml
xml = '<?xml version="1.0" encoding="utf-8"?>\n' + xml
res.send(xml)
}
else
res.send(response)
})
} else {
res.sendStatus(404);
}
} else {
res.sendStatus(404);
}
})
app.listen(port, () => console.log('oembed listening on port '+port))
function convert169(width, isWidth = true) {
if (isWidth)
return Math.round(width*9/16)
return Math.round(width*16/9)
}
function isValidDTubeUrl(url) {
var args = url.split('/')
if (args[2] != 'd.tube')
return false
if (args[3] != 'v')
return false
if (!args[4] || !args[5])
return false
return true
}
function error(err, next) {
if (err) {
console.log(err)
next()
return true
}
return false
}
function handleChainData(author, permlink, video, cb) {
var html = '<iframe width="480" height="270" src="https://emb.d.tube/#!/'+author+'/'+permlink+'" frameborder="0" allowfullscreen></iframe>'
var url = rootDomain+'/#!/v/'+author+'/'+permlink
var snap = null
var snapHeight = 118
if (video.info && video.info.snaphash)
snap = 'https://snap1.d.tube/ipfs/'+video.info.snaphash
if (video.json.ipfs && video.json.ipfs.snaphash)
snap = 'https://snap1.d.tube/ipfs/'+video.json.ipfs.snaphash
if (video.json.thumbnailUrl)
snap = video.json.thumbnailUrl
if (!snap && video.json.files) {
for (const key in video.json.files) {
if (video.json.files[key].img && video.json.files[key].img["118"])
snap = 'https://snap1.d.tube/ipfs/'+video.json.files[key].img["118"]
if (video.json.files[key].img && video.json.files[key].img["360"]) {
snap = 'https://snap1.d.tube/ipfs/'+video.json.files[key].img["360"]
snapHeight = 360
}
}
}
if (!snap && video.json.files && video.json.files.youtube) {
snap = 'https://i.ytimg.com/vi/'+video.json.files.youtube+'/hqdefault.jpg'
snapHeight = 360
}
var duration = video.json.duration || null
if (video.json.dur) duration = video.json.dur
var description = video.json.desc
if (!description && video.json.description) description = video.json.description
if (description) description = description.replace(/(?:\r\n|\r|\n)/g, ' ').substr(0, 300)
if (!description) description = ''
if (cb) {
cb(null, html, video.json.title, description, url, snap, duration, snapHeight)
cb = null
}
}
function getVideo(author, permlink, cb) {
javalon.getContent(author, permlink, function(err, video) {
if (err) {
lightrpc.send('get_state', [`/dtube/@${author}/${permlink}`], function(err, result) {
if (err || !result.content[author+'/'+permlink]) {
lightrpchive.send('get_state', [`/dtube/@${author}/${permlink}`], function(err, result) {
if (err) {
cb('Internal Error')
return
}
if (!result.content[author+'/'+permlink]) {
cb('Not found')
return
}
var video = parseVideo(result.content[author+'/'+permlink])
handleChainData(author, permlink, video, cb)
hasReplied = true
})
return
}
var video = parseVideo(result.content[author+'/'+permlink])
handleChainData(author, permlink, video, cb)
hasReplied = true
})
return
}
handleChainData(author, permlink, video, cb)
hasReplied = true
})
}
function parseVideo(video, isComment) {
try {
if (video && video.json_metadata)
var newVideo = {}
newVideo.json = JSON.parse(video.json_metadata).video
} catch(e) {
console.log(e)
}
if (!newVideo) newVideo = {}
// newVideo.active_votes = video.active_votes
newVideo.author = video.author
// newVideo.body = video.body
// newVideo.total_payout_value = video.total_payout_value
// newVideo.curator_payout_value = video.curator_payout_value
// newVideo.pending_payout_value = video.pending_payout_value
newVideo.permlink = video.permlink
newVideo.created = video.created
// newVideo.net_rshares = video.net_rshares
// newVideo.reblogged_by = video.reblogged_by
return newVideo;
}
function getRobotName(userAgent) {
for (let i = 0; i < crawlers.length; i++) {
var re = new RegExp(crawlers[i].pattern);
var isRobot = re.test(userAgent)
if (isRobot) return crawlers[i].pattern;
}
return;
}