-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
155 lines (125 loc) · 3.95 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
152
153
154
155
import express from 'express'
import bodyParser from 'body-parser'
import session from 'express-session'
import Promise from 'bluebird'
import _ from 'lodash'
import moment from 'moment'
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import {JssProvider, SheetsRegistry} from 'react-jss'
import deezer, { deezerRequest, deezerCreateSession } from './deezer'
import config from './config'
import { cacheGet, cacheSet } from './cache'
import AlbumList from './components/AlbumList'
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(session({
secret: _.get(config, ['session', 'secret'], ''),
resave: false,
saveUninitialized: true,
cookie: {
maxAge: _.get(config, ['session', 'cookie', 'maxAge']) || (60 * 60 * 24),
}
}))
app.get('/', async (req, res) => {
if (!req.session.deezerAccessToken) {
const redirectUrl = `http://${_.get(config, 'host')}:${_.get(config, 'port')}/deezerCallback`
const loginUrl = deezer.getLoginUrl(_.get(config, 'appId'), redirectUrl)
res.redirect(loginUrl)
return
}
try {
const sheets = new SheetsRegistry()
const releases = await getMyArtistsReleases(req.session.deezerAccessToken, _.get(req, ['query', 'days']))
const reactHtml = ReactDOMServer.renderToString(
<JssProvider registry={sheets}>
<AlbumList albumList={releases}/>
</JssProvider>
)
const html = `
<html>
<head>
<style type='text/css' id='server-side-styles'>
html, body {
margin: 0;
padding: 0;
background: black;
}
${sheets.toString()}
</style>
</head>
<body>
${reactHtml}
</body>
</html>
`
res.send(html)
} catch (error) {
console.error(error)
res.send(error.toString())
}
})
app.get('/deezerCallback', async (req, res) => {
const code = _.get(req, ['query', 'code'])
if (!code) {
const error = _.get(req, ['query', 'error_reason'])
console.error(error)
res.send(error)
return
}
try {
const accessToken = await deezerCreateSession(_.get(config, 'appId'), _.get(config, 'appSecret'), code)
req.session.deezerAccessToken = accessToken
res.redirect('/')
} catch (error) {
console.error(error)
res.send(error.toString())
}
})
const getArtistReleases = async (accessToken, id) => {
const cacheKey = `artist_${id}_releases`
let releases
releases = await cacheGet(cacheKey)
if (!releases) {
releases = await deezerRequest(accessToken, `artist/${id}/albums`, { limit: 9999 })
await cacheSet(cacheKey, releases)
}
return releases
}
const getMyArtists = accessToken => deezerRequest(accessToken, 'user/me/artists', { limit: 9999 })
const getMyArtistsReleases = async (accessToken, days) => {
const artists = await getMyArtists(accessToken)
console.log(`Found ${artists.length} artists`)
let releases = await Promise.map(
artists,
async (artist, index, length) => {
//console.log(`Getting releases for ${artist.name} (${index + 1}/${length})`)
const releases = await getArtistReleases(accessToken, artist.id)
_.each(releases, release => {
release.artist_id = artist.id
release.artist_name = artist.name
})
return releases
},
{ concurrency: _.get(config, 'concurrency')},
)
releases = _.flatten(releases)
console.log(`Found ${releases.length} before filtering`)
releases = _.filter(
releases,
(album) => moment(album.release_date).isAfter(moment().subtract(days || _.get(config, 'days') || 21, 'days'))
)
console.log(`Found ${releases.length} after filtering`)
releases = releases.sort((album1, album2) => {
const date1 = moment(album1.release_date)
const date2 = moment(album2.release_date)
if (date1.isBefore(date2)) {
return 1
} else {
return -1
}
})
return releases
}
app.listen(_.get(config, 'port'))