Skip to content

Instantly share code, notes, and snippets.

View amoilanen's full-sized avatar
💭
"Simplicity is prerequisite for reliability." Edsger W. Dijkstra

Anton Moilanen amoilanen

💭
"Simplicity is prerequisite for reliability." Edsger W. Dijkstra
View GitHub Profile
@amoilanen
amoilanen / webcrawler.js
Last active March 24, 2022 03:14
Simple PhantomJS-based web crawler library
//PhantomJS http://phantomjs.org/ based web crawler Anton Ivanov anton.al.ivanov@gmail.com 2012
//UPDATE: This gist has been made into a Node.js module and now can be installed with "npm install js-crawler"
//the Node.js version does not use Phantom.JS, but the API available to the client is similar to the present gist
(function(host) {
function Crawler() {
this.visitedURLs = {};
};
@amoilanen
amoilanen / replace_tabs.rb
Created November 24, 2012 19:16
Replacing tabs in source code automatically
#Utility replaces all tabs in the source files
#Usage: ruby replace_tabs.rb /home/anton/github_workspace/jfunk/src java
require 'find'
$source_file_default_ext = "java"
$tab_default_replacement = " " * 4
def replace_tabs(dir, ext, replacement)
Find.find(dir) do |file_path|
@amoilanen
amoilanen / grab_jsjabber_episodes.rb
Created February 16, 2014 21:55
Simple script that downloads all the JavaScript Jabber http://javascriptjabber.com/ episodes
#Downloads all the mp3 episode recordings from JavaScript Jabber http://javascriptjabber.com
#Assumes that the system 'wget' command is available which is the case on Linux
#Episodes will be downloaded to /home/user/JSJabber
require 'net/http'
def get(uri)
response = Net::HTTP.get_response(URI.parse(uri))
if response.code == "301"
response = Net::HTTP.get_response(URI.parse(response.header['location']))
end
@amoilanen
amoilanen / couchrest_api_example.rb
Created February 23, 2014 19:14
CouchDB example using couchrest and couchrest_model
require 'rubygems'
require 'couchrest'
require 'couchrest_model'
require 'json'
server = CouchRest.new
class JobOpening < CouchRest::Model::Base
use_database "job_openings"
@amoilanen
amoilanen / function.name.js
Last active August 29, 2015 13:59
Simple function to determine the name of a JavaScript function in case https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name is not defined
function functionName(func) {
var match = func.toString().match(/function\s+([^(?:\()\s]*)/);
return match ? match[1] : "";
}
/*
* Tests
*/
var func1 = function(x, y) {
@amoilanen
amoilanen / running.command.node.js
Created May 9, 2014 20:39
Running command from Node.js by spawning a new process
var spawn = require('child_process').spawn;
function spawnProcess(dir, cmd) {
return (process.platform.toLowerCase().indexOf("win") >= 0)
? spawnWindowsProcess(dir, cmd)
: spawnLinuxProcess(dir, cmd);
}
function spawnWindowsProcess(dir, cmd) {
return spawn("cmd.exe", ["/c", cmd], {cwd: dir});
@amoilanen
amoilanen / bulk_download_Coursera_videos.js
Last active May 21, 2022 02:18
Bulk download of Coursera videos with wget
/*
* Bulk download of Coursera videos with wget.
*
* Copyright (c) 2014 Anton Ivanov [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@amoilanen
amoilanen / promisify_require.js
Last active April 1, 2021 13:54
Enhances 'require' from RequireJS with Promises API while preserving its original semantics. Now 'require' calls can be chained.
/*
* Enhances 'require' from RequireJS with Promises API while preserving its original semantics.
*/
(function() {
if (!Promise || !require) {
return;
}
@amoilanen
amoilanen / map_as_method.js
Last active August 29, 2015 14:05
Mapping function as method
if (!Function.prototype.asMethod) {
Function.prototype.asMethod = function() {
var self = this;
return function(first) {
var rest = [].slice.call(arguments, 1);
return self.apply(first, rest);
}
};
@amoilanen
amoilanen / python_selenium_minimalistic_scenario.py
Created October 1, 2014 09:14
Minimalistic Selenium scenario for Firefox and Python, can be used to check if the latest Firefox has any problems with Selenium integration
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
driver.quit()