Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

a very much needed update

  • Loading branch information...
commit fef21164fdeec832c006e97a8400291f5502fa8e 1 parent 6034594
@ivolo authored
View
18 .gitignore
@@ -0,0 +1,18 @@
+
+# Extensions
+*.monitor
+*.pyc
+*.log
+
+logs/*
+# Sublime project files
+*.sublime-project
+*.sublime-workspace
+
+# OS generated files
+.DS_Store*
+ehthumbs.db
+Icon?
+Thumbs.db
+
+node_modules
View
93 controllers/animals.js
@@ -0,0 +1,93 @@
+
+var _ = require('underscore');
+
+var animals = require('../lib/animals');
+
+
+
+var getNumber = function (req, key) {
+
+ var offset = parseInt(req.param(key), 10);
+
+ if (_.isNaN(offset) || !_.isNumber(offset) || offset < 0 || offset > 1000)
+ offset = 0;
+
+ return offset;
+};
+
+var chooseUsingDimensions = function (req, index) {
+
+ var len = animals.getAll().length;
+
+ var maxWidth = getNumber(req, 'maxwidth');
+ var maxHeight = getNumber(req, 'maxheight');
+
+ if (maxWidth || maxHeight) {
+
+ for (var i = 0; i < len; i += 1) {
+
+ var dims = animals.dimensions(index);
+ var satisfied = true;
+
+ if (maxWidth && dims.width > maxWidth) {
+ satisfied = false;
+ }
+
+ if (maxHeight && dims.height > maxHeight) {
+ satisfied = false;
+ }
+
+ if (satisfied) break;
+
+ index = (index + 1) % len;
+ }
+ }
+
+ return index;
+};
+
+var getIndex = function (req) {
+
+ var index = parseInt(req.param('index'), 10);
+
+ var len = animals.getAll().length;
+
+ if (_.isNaN(index) || !_.isNumber(index) || index >= len || index < 0)
+ index = Math.floor(Math.random() * len);
+ else
+ index = Math.floor(index);
+
+ return index;
+};
+
+var getReverse = function (req) {
+
+ var reverse = req.param('reverse');
+
+ if (_.isString(reverse)) reverse = reverse.toLowerCase();
+
+ reverse = reverse === 'true';
+
+ return reverse;
+};
+
+
+exports.home = function (req, res, next) {
+
+ var index = chooseUsingDimensions(req, getIndex(req));
+
+ var offset = getNumber(req, 'offset');
+ var reverse = getReverse(req);
+
+ var animal = animals.get(index, offset, reverse);
+
+ res.set('Content-Type', 'text/plain; charset=utf-8');
+ res.send(animal);
+};
+
+exports.dimensions = function (req, res, next) {
+
+ var index = getIndex(req);
+
+ res.json(animals.dimensions(index));
+};
View
2  controllers/index.js
@@ -0,0 +1,2 @@
+
+exports.animals = require('./animals');
View
79 examples/progress.py
@@ -0,0 +1,79 @@
+
+import requests
+from random import randint
+from time import sleep
+
+
+def getTerminalSize():
+ import os
+ env = os.environ
+
+ def ioctl_GWINSZ(fd):
+ try:
+ import fcntl
+ import termios
+ import struct
+
+ cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
+ '1234'))
+ except:
+ return None
+ return cr
+ cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
+ if not cr:
+ try:
+ fd = os.open(os.ctermid(), os.O_RDONLY)
+ cr = ioctl_GWINSZ(fd)
+ os.close(fd)
+ except:
+ pass
+ if not cr:
+ try:
+ cr = (env['LINES'], env['COLUMNS'])
+ except:
+ cr = (25, 80)
+ return int(cr[1]), int(cr[0])
+
+
+ANIMAL_URL = 'http://localhost'
+
+
+def next_frame(index=0, offset=0, reverse=False, maxwidth=0, maxheight=0):
+
+ payload = {
+ "index": index,
+ "offset": offset,
+ "reverse": reverse,
+ "maxwidth": maxwidth,
+ "maxheight": maxheight
+ }
+
+ return '\033[2J' + requests.get(ANIMAL_URL, params=payload).text
+
+
+dims = getTerminalSize()
+
+index = randint(0, 500)
+
+counter = 0
+increment = 1
+offset = 0
+steps = 10
+reverse = False
+
+while True:
+ offset = (offset + increment)
+ if offset % steps == 0:
+ reverse = not reverse
+ increment = -1 * increment
+ dims = getTerminalSize()
+
+ animal = next_frame(index, offset, reverse,
+ maxwidth=dims[0], maxheight=dims[1])
+
+ animal_width = len(animal.split('\n')[0]) - offset
+ steps = dims[0] - animal_width
+
+ print animal
+
+ sleep(0.05)
View
97 lib/animals.js
@@ -0,0 +1,97 @@
+
+var fs = require('fs');
+var path = require('path');
+
+var _ = require('underscore');
+var winston = require('winston');
+
+
+var animals = ['almost there!'];
+var ANIMALS_PATH = path.join(__dirname, '../public/assets/animals.saved');
+var SEPERATOR = '===============++++SEPERATOR++++====================\n';
+
+winston.info('Loading animals file ...');
+
+var equalizePadding = function (animal) {
+
+ var lines = animal.replace(/\r/g, '').split('\n');
+
+ var max = _.max(_.map(lines, function (line) { return line.length; }));
+ lines = _.map(lines, function (line) {
+ var paddingLength = max - line.length;
+ var paddingStr = '';
+ _.each(_.range(paddingLength), function () { paddingStr += ' '; });
+ return line + paddingStr;
+ });
+
+ return lines.join('\n');
+};
+
+fs.readFile(ANIMALS_PATH, 'utf8', function (err, data) {
+ if (err) throw err;
+
+ winston.info('Loaded animals file.');
+
+ var animalStr = data.toString();
+
+ animals = [];
+
+ animals = _.map(animalStr.split(SEPERATOR), function (animal) {
+ return equalizePadding(animal);
+ });
+
+ winston.info('Loaded ' + animals.length + ' animals.');
+});
+
+var offsetAnimal = function (animal, offset) {
+
+ var offsetStr = '';
+ _.each(_.range(offset), function () { offsetStr += ' '; });
+
+ var lines = animal.replace(/\r/g, '').split('\n');
+ lines = _.map(lines, function (line) {
+ return offsetStr + line;
+ });
+
+ return lines.join('\n');
+};
+
+var reverseAnimal = function (animal) {
+
+ var lines = animal.replace(/\r/g, '').split('\n');
+
+ lines = _.map(lines, function (line) {
+ var str = line.split('').reverse().join('');
+ return str;
+ });
+
+ return lines.join('\n');
+};
+
+exports.getAll = function () {
+ return animals;
+};
+
+exports.get = function (index, offset, reverse) {
+
+ var animal = animals[index];
+
+ if (reverse) animal = reverseAnimal(animal);
+ if (offset > 0) animal = offsetAnimal(animal, offset);
+
+ return animal;
+};
+
+exports.dimensions = function (index) {
+
+ var animal = exports.get(index);
+
+ var lines = animal.replace(/\r/g, '').split('\n');
+
+ var width = _.max(_.map(lines, function (line) { return line.length; }));
+
+ return {
+ width: width,
+ height: lines.length
+ };
+};
View
14 package.json
@@ -0,0 +1,14 @@
+{
+ "name": "funny"
+ , "version": "0.0.1"
+ , "description": "Makes funny ascii animals"
+ , "keywords": ["funny", "ascii", "animal", "progress", "bar"]
+ , "author": "Ilya Volodarsky <ilya@segment.io>"
+ , "engines": { "node": ">= 0.6.0" }
+ , "dependencies":{
+ "winston": "*"
+ , "underscore": "*"
+ , "express": "3.X"
+
+ }
+}
View
10,067 public/assets/animals.saved
10,067 additions, 0 deletions not shown
View
16 routes.js
@@ -0,0 +1,16 @@
+
+var controllers = require('./controllers');
+
+// Shorthand vars for the routes
+var animals = controllers.animals;
+
+module.exports = function (app) {
+
+
+ app.get('/dimensions/?', animals.dimensions);
+ app.get('/?', animals.home);
+
+
+};
+
+
View
15 animals.py → scripts/animals.py
@@ -2,13 +2,14 @@
import BeautifulSoup as soup
from os.path import exists
-from pickle import load, dump
from random import uniform
SEPERATOR = '===============++++SEPERATOR++++====================\n'
DEFAULT_FILENAME = 'animals.saved'
+
def get_animals():
+
r = requests.get('http://www.heartnsoul.com/ascii_art/ascii_animals_indx.htm')
s = soup.BeautifulSoup(r.content)
animals = []
@@ -26,9 +27,10 @@ def get_animals():
else:
animal += line + '\n'
lines_added += 1
-
+
return animals
+
def save_animals(animals, filename=DEFAULT_FILENAME):
f = open(filename, 'w+')
for animal in animals:
@@ -36,6 +38,7 @@ def save_animals(animals, filename=DEFAULT_FILENAME):
f.write(SEPERATOR)
f.close()
+
def load_animals(filename=DEFAULT_FILENAME):
if exists(filename):
f = open(filename, 'r+')
@@ -49,10 +52,12 @@ def load_animals(filename=DEFAULT_FILENAME):
if animals == None:
animals = get_animals()
save_animals(animals)
-
+
import fish
+
+
class CustomLook(fish.MultiLineFishPrinter):
-
+
def __init__(self, s):
self.renderable = fish.docstring2lines(s)
@@ -60,6 +65,6 @@ def render(self, step, reverse=False):
return self.renderable
own_length = len(renderable[0])
-
+
random_animal = animals[int(uniform(0, len(animals)))]
View
10 asciify.py → scripts/asciify.py
@@ -12,14 +12,16 @@
def get_random_font():
return fonts[int(uniform(0, len(fonts)))]
+
def random_asciify(s, width):
font = get_random_font()
print font
- return asciify(s, font, width)
-
+ return asciify(s, font, width)
+
+
def asciify(s, font, width):
data = {'TEXT': s, 'x': 0, 'y': 0, 'FONT': font, 'RICH': 'no', 'FORM': 'left', 'STRE': 'no', 'WIDT': width}
- query_string = urlencode(data)
+ query_string = urlencode(data)
url = '{0}?{1}'.format('http://www.network-science.de/ascii/ascii.php', query_string)
r = requests.get(url)
if r.status_code != 200:
@@ -33,5 +35,3 @@ def asciify(s, font, width):
result = random_asciify('appropriete message', 80)
print result if result != None else 'Didnt work'
-
-
View
0  fish.py → scripts/fish.py
File renamed without changes
View
0  visualizer.py → scripts/visualizer.py
File renamed without changes
View
25 server.js
@@ -0,0 +1,25 @@
+var express = require('express');
+var http = require('http');
+
+var routes = require('./routes');
+var setup = require('./setup');
+
+// increase the max concurrent sockets that the agent can use
+http.globalAgent.maxSockets = 2000;
+
+// create the server
+var app = express();
+// create the http server
+var server = http.createServer(app);
+
+
+// setup global middleware, stylus, etc
+setup(app);
+
+// install the routes
+routes(app);
+
+server.port = 80;
+
+// export the server for UP
+module.exports = server;
View
23 setup.js
@@ -0,0 +1,23 @@
+
+var path = require('path');
+
+var express = require('express');
+
+
+module.exports = function (app) {
+
+ var publicDirectory = path.join(__dirname, 'public/');
+
+ app.configure('development', function() {
+ app.use(express.static(publicDirectory));
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ });
+
+ app.configure('production', function(){
+ var oneYear = 31557600000;
+ app.use(express.static(publicDirectory, { maxAge: oneYear }));
+ app.use(express.errorHandler());
+ });
+
+ app.use(app.router);
+};
View
7 simple.js
@@ -0,0 +1,7 @@
+
+var winston = require('winston');
+ server = require('./server.js');
+
+server.listen(server.port);
+
+winston.info('Site server set to port ' + server.port + '!');
Please sign in to comment.
Something went wrong with that request. Please try again.