Skip to content

Commit

Permalink
Switched to Supermarket storage
Browse files Browse the repository at this point in the history
  • Loading branch information
evl authored and simenbrekken committed Jan 22, 2011
1 parent c801f4c commit 0c27853
Showing 1 changed file with 124 additions and 149 deletions.
273 changes: 124 additions & 149 deletions oaz.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var jerk = require('jerk')
var request = require('request')
var db = require('chaos')('oaz.chaos')
var crypto = require('crypto')
var supermarket = require('supermarket')

var options = {
server: 'se.quakenet.org',
Expand All @@ -28,13 +28,6 @@ String.prototype.repeat = function(num) {
return new Array(++num).join(this)
}

/*
var Link = {
store: function()
updateCount:
}
*/

var Format = {
bold: function(value) {
return String.fromCharCode(2) + value + String.fromCharCode(15)
Expand All @@ -44,172 +37,154 @@ var Format = {
}
}

function resolve(uri, callback) {
var options = {uri: uri, method: 'HEAD'}

request(options, function(error, response, body) {
callback(error, options.uri.href)
})
}
supermarket({filename: 'oaz.sqlite', json: true}, function(error, db) {
if (error) throw error

jerk(function(bot) {
// Oäz
bot.watch_for(/http:\/\/\S+/, function(message) {
var unresolvedUrl = message.match_data[0]
jerk(function(bot) {
// Oäz
bot.watch_for(/http:\/\/\S+/, function(message) {
var options = {uri: message.match_data[0], method: 'HEAD'}

resolve(unresolvedUrl, function(error, url) {
if (!error) {
// Attempt to update existing link
db.get(url, function(error, value) {
var link = !error ? JSON.parse(value) : {entries: []}
var entry = {user: message.user, time: (new Date()).toString()}
link.entries.push(entry)
request(options, function(error, response, body) {
if (!error) {
var url = options.uri.href

// Attempt to update existing link
db.get(url, function(error, value) {
var link = !error ? value : {entries: []}
var entry = {user: message.user, time: (new Date()).toString()}
link.entries.push(entry)

db.set(url, JSON.stringify(link), function(error, value) {
console.log(error ? 'Failed to store' : 'Successfully stored', url)
})
db.set(url, link, function(error, value) {
console.log(error ? 'Failed to store' : 'Successfully stored', url)
})

// Store a reference to the last link that was posted
db.set('lastLink', url)
})
} else {
console.log('Couldn\'t resolve:', url)
}
// Store a reference to the last link that was posted
db.set('lastLink', url)
})
} else {
console.log('Couldn\'t resolve:', url)
}
})
})
})

bot.watch_for(/^[oO]+[äÄ]+[zZ]+/, function(message) {
// Fetch the last link
db.get('lastLink', function(error, value) {
if (!error) {
var url = value
bot.watch_for(/^[oO]+[äÄ]+[zZ]+/, function(message) {
// Fetch the last link
db.get('lastLink', function(error, value) {
if (!error) {
var url = value

// Get the link
db.get(url, function(error, value) {
if (!error) {
// Oäz that shit!
var link = JSON.parse(value)
var count = link.entries.length
var entry = link.entries[count - 1]
// Get the link
db.get(url, function(error, value) {
if (!error) {
// Oäz that shit!
var link = value
var count = link.entries.length
var entry = link.entries[count - 1]

if (!entry.processed) {
entry.processed = true
if (!entry.processed) {
entry.processed = true

db.set(url, JSON.stringify(link), function(error, value) {
console.log(error ? 'Failed to update' : 'Successfully updated', url)
db.set(url, link, function(error, value) {
console.log(error ? 'Failed to update' : 'Successfully updated', url)

var age = 'O{0}z!'.format('ä'.repeat(count))
var result = '{0}: {1}'.format(entry.user, Format.bold(age))
var age = 'O{0}z!'.format('ä'.repeat(count))
var result = '{0}: {1}'.format(entry.user, Format.bold(age))

if (count > 1) {
var firstEntry = link.entries[0]
if (count > 1) {
var firstEntry = link.entries[0]

result += ' (posted {0} times, first by {1})'.format(count, firstEntry.user)
}
result += ' (posted {0} times, first by {1})'.format(count, firstEntry.user)
}

message.say(result)
})
message.say(result)
})
}
}
}
})
} else {
console.log('We don\'t have a last link!')
}
})
})
} else {
console.log('We don\'t have a last link!')
}
})

})

// YouTube
bot.watch_for(/youtube.*v=([^#&\s]+)/, function(message) {
var id = message.match_data[1]
var options = {uri: 'http://gdata.youtube.com/feeds/api/videos/{0}?alt=json'.format(id)}

request(options, function(error, response, body) {
var data = JSON.parse(body)
var entry = data.entry

if (entry) {
message.say('{0}: {1}'.format(Format.bold('YouTube'), entry.title.$t))
}
})
})

// Vimeo
bot.watch_for(/vimeo\.com\/(\d+)/, function(message) {
var id = message.match_data[1]
var options = {uri: 'http://vimeo.com/api/v2/video/{0}.json'.format(id)}
// YouTube
bot.watch_for(/youtube.*v=([^#&\s]+)/, function(message) {
var id = message.match_data[1]
var options = {uri: 'http://gdata.youtube.com/feeds/api/videos/{0}?alt=json'.format(id)}

request(options, function(error, response, body) {
var data = JSON.parse(body)
request(options, function(error, response, body) {
var data = JSON.parse(body)
var entry = data.entry

if (data) {
message.say('{0}: {1}'.format(Format.bold('Vimeo'), data[0].title))
}
if (entry) {
message.say('{0}: {1}'.format(Format.bold('YouTube'), entry.title.$t))
}
})
})
})

// Spotify
bot.watch_for(/(http:\/\/open\.spotify\.com\/[\w\/]+)/, function(message) {
var id = message.match_data[1]
var options = {uri: 'http://ws.spotify.com/lookup/1/.json?uri={0}'.format(id)}
// Vimeo
bot.watch_for(/vimeo\.com\/(\d+)/, function(message) {
var id = message.match_data[1]
var options = {uri: 'http://vimeo.com/api/v2/video/{0}.json'.format(id)}

request(options, function(errorror, response, body) {
if (response.statusCode == 200) {
request(options, function(error, response, body) {
var data = JSON.parse(body)
var type
var title

switch (data.info.type) {
case 'artist':
type = 'Artist'
title = data.artist.name
break;
case 'album':
type = 'Album'
title = data.album.artist + ' - ' + data.album.name
break;
case 'track':
type = 'Track'
title = data.track.artists[0].name + ' - ' + data.track.name
break;

if (data) {
message.say('{0}: {1}'.format(Format.bold('Vimeo'), data[0].title))
}
})
})

// Spotify
bot.watch_for(/(http:\/\/open\.spotify\.com\/[\w\/]+)/, function(message) {
var id = message.match_data[1]
var options = {uri: 'http://ws.spotify.com/lookup/1/.json?uri={0}'.format(id)}

request(options, function(errorror, response, body) {
if (response.statusCode == 200) {
var data = JSON.parse(body)
var type
var title

switch (data.info.type) {
case 'artist':
type = 'Artist'
title = data.artist.name
break
case 'album':
type = 'Album'
title = data.album.artist + ' - ' + data.album.name
break
case 'track':
type = 'Track'
title = data.track.artists[0].name + ' - ' + data.track.name
break
}

message.say('{0}: {1}'.format(Format.bold('Spotify ' + type), title))
}
message.say('{0}: {1}'.format(Format.bold('Spotify ' + type), title))
}
})
})
})

// Weather
bot.watch_for(/^!(?:weather|temp|termo|temperature) (.+)/, function(message) {
var query = message.match_data[1]
var options = {uri: 'http://www.google.com/ig/api?weather={0}'.format(escape(query))}

request(options, function(error, response, body) {
// TODO: Might want to use city
var city = body.match(/<postal_code data="([^"]+)/)
var description = body.match(/<current_conditions>[\s]*<condition data="([^"]+)"/)
var temperature = body.match(/<temp_c data="([^"]+)"/)
var humidity = body.match(/<humidity data="Humidity: (\d+)/)

if (description) {
message.say('{0}: {1}°C {2}, {3}% Humidity'.format(Format.bold(city[1]), temperature[1], description[1], humidity[1]))
}
// Weather
bot.watch_for(/^!(?:weather|temp|termo|temperature) (.+)/, function(message) {
var query = message.match_data[1]
var options = {uri: 'http://www.google.com/ig/api?weather={0}'.format(escape(query))}

request(options, function(error, response, body) {
// TODO: Might want to use city
var city = body.match(/<postal_code data="([^"]+)/)
var description = body.match(/<current_conditions>[\s]*<condition data="([^"]+)"/)
var temperature = body.match(/<temp_c data="([^"]+)"/)
var humidity = body.match(/<humidity data="Humidity: (\d+)/)

if (description) {
message.say('{0}: {1}°C {2}, {3}% Humidity'.format(Format.bold(city[1]), temperature[1], description[1], humidity[1]))
}
})
})
})
}).connect(options)

/*
function resolveCallback(error, id, uri) {
console.log(error, hash, uri)
}
resolve('http://www.vg.no/nyheter/vaer/artikkel.php?artid=10013039', resolveCallback) // Direct
resolve('http://tinyurl.com/KindleWireless', resolveCallback) // Valid redirect
resolve('http://tinyurlz.no/KindleWireless', resolveCallback) // Invalid
resolve('http://www.youtube.com/watch?v=TAHHW9YYpBo', resolveCallback) // Youtube
resolve('http://www.youtube.com/watch?v=UYcKuvjhl6A&hd=1', resolveCallback) // Youtube HD
resolve('http://www.youtube.com/user/chaseandstatustv#p/a/u/0/TAHHW9YYpBo', resolveCallback) // Youtube video from channel
resolve('http://youtu.be/TAHHW9YYpBo', resolveCallback) // Youtube short
resolve('http://i.imgur.com/Jw2Fc.png', resolveCallback) // Image
*/
}).connect(options)
})

0 comments on commit 0c27853

Please sign in to comment.