Show info about the imgur url on the random page

This commit is contained in:
reality 2013-04-16 16:42:57 +00:00
parent 37a37c97b9
commit 3b5ef9f55e
3 changed files with 25 additions and 15 deletions

View File

@ -9,12 +9,11 @@ var _ = require('underscore')._,
var imgur = function(dbot) {
this.internalAPI = {
'infoString': function(imgData) {
info = null;
info = '';
if(imgData && _.has(imgData, 'data')) {
imgData = imgData.data;
info = '[';
if(imgData.title) {
info += imgData.title + '][';
info += imgData.title + ' - ';
}
if(imgData.type) {
if(imgData.animated) {
@ -26,7 +25,7 @@ var imgur = function(dbot) {
info += 'an image with ';
}
info += imgData.views + ' views (';
info += imgData.width + 'x' + imgData.height + ')].';
info += imgData.width + 'x' + imgData.height + ')';
}
return info;
@ -55,6 +54,12 @@ var imgur = function(dbot) {
}.bind(this));
},
'getImageInfoString': function(slug, callback) {
this.api.getImageInfo(slug, function(imgData) {
callback(this.internalAPI.infoString(imgData));
}.bind(this));
},
'getImageInfo': function(slug, callback) {
request.get({
'url': 'https://api.imgur.com/3/image/' + slug + '.json',
@ -69,13 +74,15 @@ var imgur = function(dbot) {
};
this.api['getRandomImage'].external = true;
this.api['getRandomImage'].extMap = [ 'callback' ];
this.api['getImageInfoString'].external = true;
this.api['getImageInfoString'].extMap = [ 'slug', 'callback' ];
this.commands = {
'~ri': function(event) {
this.api.getRandomImage(function(link, slug) {
this.api.getImageInfo(slug, function(imgData) {
var info = this.internalAPI.infoString(imgData);
event.reply(event.user + ': ' + link + ' ' + info);
event.reply(event.user + ': ' + link + ' [' + info + ']');
}.bind(this));
}.bind(this));
}
@ -86,7 +93,7 @@ var imgur = function(dbot) {
if(matches[1]) {
this.api.getImageInfo(matches[1], function(imgData) {
var info = this.internalAPI.infoString(imgData);
if(info) event.reply(info);
if(info) event.reply('[' + info + ']');
}.bind(this));
}
}.bind(this);

View File

@ -13,7 +13,6 @@
border-bottom-right-radius:5px;
border-top-right-radius:5px;
color:#85bf25;
display:none;
font-family:sans-serif;
font-weight:700;
left:0;

View File

@ -7,15 +7,19 @@ html(lang='en')
title random imgur
script
var count = 0;
function wat() {
function getNewImage() {
count += 1;
$('#loading').text('Loading image ' + count);
$('#loading').show();
$('#image').load(function(){$('#loading').hide(); console.log('loaded')});
$.get("/api/imgur/getRandomImage", function(d) {$('#image').attr('src', d.data[0])}, "json");
};
$(wat());
$(document).on('keydown', function(e){(e.which==32)&&(wat()) });
$('#loading').text('Loading image ' + count + '...');
$('#image').load(function(){});
$.get("/api/imgur/getRandomImage", function(d) {
$('#image').attr('src', d.data[0]);
$.get("/api/imgur/getImageInfoString", { 'slug': d.data[1] }, function(info) {
$("#loading").text(count + ': ' + info.data[0]);
}, "json");
}, "json");
}
$(getNewImage());
$(document).on('keydown', function(e){(e.which==32)&&(getNewImage()) });
body
div#loading Loading Image
div#container