Skip to content

Instantly share code, notes, and snippets.

@tsuzu
Last active October 19, 2016 15:12
Show Gist options
  • Save tsuzu/fb2580d6c64cc8361ff3c9bb332f6aea to your computer and use it in GitHub Desktop.
Save tsuzu/fb2580d6c64cc8361ff3c9bb332f6aea to your computer and use it in GitHub Desktop.
Get title and artist of MPGA stream
http = require('http')
execSync = require('child_process').execSync
title = ''
options = {
host: '', # Host
path: '/', # Path
port: 6110, # Port
method: 'GET',
headers: {
'Icy-MetaData': '1'
}
}
req = http.request options, (res)->
body = []
body.totalLength = 0
packSize = 0
if res.headers['icy-metaint']?
packSize = parseInt(res.headers['icy-metaint'], 10)
else
console.error 'unable to get headers \'icy-metaint\''
process.exit 1
res.on 'data', (chunk)->
body.push(chunk)
body.totalLength += chunk.length
if body.totalLength >= packSize # && body.toString('utf8', packSize, body.length)
buf = Buffer.concat(body, body.totalLength)
reg = /StreamTitle=\'([\S\s]+?)\';\0/g
match = reg.exec(buf.toString())
if match?
console.log('Title: ' + match[1])
newBody = buf[reg.lastIndex..]
body = [ newBody ]
body.totalLength = newBody.length
else
newBody = buf[16000..]
body = [ newBody ]
body.totalLength = newBody.length
res.on 'end', (res)->
req.on 'error', (e)->
console.error e.message
req.end()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment