Created
February 6, 2015 23:10
-
-
Save bake/193affd03ab95f1cd404 to your computer and use it in GitHub Desktop.
Wikia Lyrics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http = require 'http' | |
itunes = require 'playback' | |
entities = require 'entities' | |
size = process.stdout.getWindowSize() | |
api = 'http://lyrics.wikia.com/api.php' | |
itunes.on 'playing', (track) -> | |
search track.artist, track.name | |
search = (artist, track) -> | |
get api + '?artist=' + encodeURI(artist) + '&song=' + encodeURI(track) + '&fmt=realjson', (body) -> | |
body = JSON.parse body | |
console.log '' | |
console.log artist + ' - ' + track | |
console.log '===' | |
console.log '' | |
get body.url, parseLyrics | |
parseLyrics = (body) -> | |
body = body.match /<div cl?ass='lyricbox'>(.*)/ig | |
body = body[0] | |
body = body.replace /(<br( \/)?>)/ig, ' ' | |
body = body.replace /(<([^>]+)>)/ig, '' | |
body = body.match /(&#[0-9]+;)/ig | |
body = body.join '' | |
body = entities.decodeHTML body | |
body = body.trim() | |
if body is '' | |
console.log 'Not found :(' | |
else console.log body | |
get = (url, callback) -> | |
http.get url, (response) -> | |
body = '' | |
response.on 'data', (chunk) -> | |
body += chunk | |
response.on 'end', -> | |
callback body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment