Skip to content

Instantly share code, notes, and snippets.

@aliou
Last active December 29, 2015 17:29
Show Gist options
  • Save aliou/7704344 to your computer and use it in GitHub Desktop.
Save aliou/7704344 to your computer and use it in GitHub Desktop.
Tsundoku
.env
node_modules/
lastfmapi = require 'lastfmapi'
redis = require 'redis'
url = require('url').parse process.env.REDISTOGO_URL
db = redis.createClient url.port, url.hostname
db.auth url.auth.split(':')[1]
module.exports = (app) ->
app.get '/albums', (req, res) ->
db.lrange 'albums', 0, -1, (err, albums) ->
res.json {err: 'Error while retrieving the albums.'} if err
response = (JSON.parse album for album in albums)
res.json response
express = require 'express'
app = express()
PORT = process.env.PORT or 8000
app.configure ->
app.use (req, res, next) ->
res.header 'Access-Control-Allow-Origin', '*'
res.header 'Access-Control-Allow-Methods', 'GET,POST'
res.header 'Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'
next()
app.use express.logger()
app.use express.bodyParser()
require("#{__dirname}/books")(app)
require("#{__dirname}/albums")(app)
app.listen PORT, ->
console.log 'Listening on port ' + PORT
redis = require 'redis'
url = require('url').parse process.env.REDISTOGO_URL
db = redis.createClient url.port, url.hostname
db.auth url.auth.split(':')[1]
module.exports = (app) ->
app.get '/books', (req, res) ->
db.get 'books', (err, books) ->
res.send '{\'err\': \'Error while retrieving the books.\'}' if err
res.send books
app.post '/books', (req, res) ->
if not req.param 'KEY'
res.send 'Missing key.\n'
else if req.param 'KEY' != process.env.KEY
res.send 'Wrong key.\n'
book = {}
book.title = req.param 'book'
book.cover = req.param 'cover'
book.current = req.param 'current' ? req.param 'current' : false
db.get 'books', (err, reply) ->
res.send '{\'err\': \'Error while retrieving the books.\'}' if err
books = JSON.parse reply
if not books
books = []
books.push book
db.set 'books', JSON.stringify books
res.json book
redis = require 'redis'
LastfmAPI = require 'lastfmapi'
task 'fetch:album', 'Fetch the weekly albums', ->
url = require('url').parse process.env.REDISTOGO_URL
db = redis.createClient url.port, url.hostname
db.auth url.auth.split(':')[1]
db.del 'albums', redis.print
lfm = new LastfmAPI {
api_key: process.env.LF_API_KEY,
secret: process.env.LF_SECRET
}
lfm.user.getWeeklyAlbumChart {user: 'aliouftw'}, (err, chart) ->
console.log err if err
saveAlbum = (album) ->
lfm.album.getInfo {mbid: album.mbid}, (err, info) ->
return '{}' if err
obj = {
album: info.name,
artist: info.artist,
cover: info.image[4]['#text'],
playcount: album.playcount
}
db.lpush 'albums', JSON.stringify obj
return obj
saveAlbum(album) for album in chart.album[0..4] when album.mbid.length > 0
((ms, func) -> setTimeout func, ms) 30000, -> db.quit()
{
"name": "Tsundoku",
"description": "Book list.",
"author": "Aliou Diallo <[email protected]>",
"version": "0.0.1",
"private": true,
"engines": {
"node": "0.10.12",
"npm": "1.2.x"
},
"dependencies": {
"express": "3.4.0",
"redis": "0.8.6",
"coffee-script": "~1.6.3",
"lastfmapi": "0.0.5"
}
}
web: node server.js
require('coffee-script');
require('./app');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment