Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alexanderGugel
alexanderGugel / logger.js
Created June 16, 2014 03:54
Minimal Node.JS logger for 12-factor apps
// Usage:
// var logger = require('./logger');
// logger.debug('Debug message');
// logger.info('Info message');
// logger.warn('Warn message');
// logger.error('Error message');
// logger.addLevels({
// silly: 'white'
// });
// logger.silly('Silly message');
@alexanderGugel
alexanderGugel / index.html
Created July 10, 2014 05:44
Simple animated bar charts using D3 and data-attributes
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart {
height: 70px;
width: 300px;
border-bottom: 1px solid #ccc;
display: block;
margin: 1em;
}
@alexanderGugel
alexanderGugel / scraper.js
Created July 16, 2014 01:13
The Pirate Bay scraper
var request = require('request'),
_ = require('lodash');
// 100: Audio
// 200: Video
// 300: Applications
// 400: Games
// 500: Porn
// 600: Other
var categories = [100, 200, 300, 400, 500, 600];
@alexanderGugel
alexanderGugel / crawler.js
Created July 19, 2014 07:26
BitTorrent DHT Crawler
// This file is part of github.com/Trrnts/Trrnts - an upcoming alternative to The Pirate Bay.
var bencode = require('bencode'),
dgram = require('dgram'),
hat = require('hat'),
_ = require('lodash'),
redis = require('../redis')(),
geoip = require('geoip-lite');
// Put in a function. The returned function won't ever throw an error. This is
@alexanderGugel
alexanderGugel / crawl.js
Created July 19, 2014 21:22
BitTorrent DHT Crawler; No Redis DB required
var bencode = require('bencode'),
dgram = require('dgram'),
hat = require('hat'),
_ = require('lodash');
// Put in a function. The returned function won't ever throw an error. This is
// quite useful for malformed messages.
var makeSafe = function (fn, onFuckedUp) {
return function () {
try {
@alexanderGugel
alexanderGugel / linkedList.js
Last active August 29, 2015 14:04
Linked List without a single if-statement? - Challenge accepted!
// PoC: Implemented methods: addToTail(value), removeHead() #=> value, contains(value) #=> boolean
var tail = {
next: this,
removeHead: function () {
return null;
},
contains: function (value) {
return false;
},
var fs = require('fs');
var words = fs.readFileSync('/usr/share/dict/words', {
encoding: 'utf8'
}).split('\n');
var tlds = ['co', 'com', 'io', 'de', 'it'];
var results = [];
@alexanderGugel
alexanderGugel / gist:d473ccc6270eba16cdcf
Created November 5, 2014 16:20
Node.JS email to gravatar
var crypto = require('crypto');
var getGravatarImage = function (email) {
return ('//www.gravatar.com/avatar/' + md5(email)).trim();
};
var md5 = function (str) {
var hash = crypto.createHash('md5');
hash.update(str.toLowerCase().trim());
return hash.digest('hex');
@alexanderGugel
alexanderGugel / gist:aceb9d83c3769d113853
Created November 6, 2014 18:13
Recursively extract keys from nested object
var object = {
hello: 'world',
hallo: {
german: 'welt',
test: {
test: true,
test: false
}
}
};
@alexanderGugel
alexanderGugel / gist:f3e8e82b606dac9624b4
Created November 10, 2014 22:07
Node.JS Redis in production
// Port, host and auth token of Redis server might be defined as environment
// variables. If not, fall back to defaults.
var redisPort = process.env.REDIS_PORT || 6379,
redisHost = process.env.REDIS_HOST || '127.0.0.1',
redisAuth = process.env.REDIS_AUTH || null,
redis = require('redis');
// Since we are waiting for the error event, we don't have to check for errors
// individually after each Redis command.
var onError = function (error) {