Skip to content

Instantly share code, notes, and snippets.

View ranaroussi's full-sized avatar

Ran Aroussi ranaroussi

View GitHub Profile
@ranaroussi
ranaroussi / gist:3985816
Created October 31, 2012 08:17
extract meta keywords of a web page
function get_meta_keywords() {
var meta_keywords = '';
var metas = document.getElementsByTagName('meta');
if (metas) {
for (var x=0,y=metas.length; x<y; x++) {
if (metas[x].name.toLowerCase() == "keywords") { meta_keywords += metas[x].content; }
}
}
return meta_keywords;
@ranaroussi
ranaroussi / gist:3985821
Created October 31, 2012 08:18
prepare string for use in urls
function querify(str) {
str = encodeURIComponent(str.replace(/,/gi, ' ').replace(/\s{2,}/gi, ' ')).replace(/%20/gi, '+');
return str;
}
@ranaroussi
ranaroussi / roll_ipython_in_aws.md
Created October 15, 2015 05:23 — forked from iamatypeofwalrus/roll_ipython_in_aws.md
Create an iPython HTML Notebook on Amazon's AWS Free Tier from scratch.

What

Roll your own iPython Notebook server with Amazon Web Services (EC2) using their Free Tier.

What are we using? What do you need?

  • An active AWS account. First time sign-ups are eligible for the free tier for a year
  • One Micro Tier EC2 Instance
  • With AWS we will use the stock Ubuntu Server AMI and customize it.
  • Anaconda for Python.
  • Coffee/Beer/Time
@ranaroussi
ranaroussi / get_client_ip
Created May 26, 2013 19:36
Get client's real IP
function get_client_ip() {
$check_order = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
$reserved_ips = array(
array('0.0.0.0','2.255.255.255'),
array('10.0.0.0','10.255.255.255'),
array('127.0.0.0','127.255.255.255'),
array('169.254.0.0','169.254.255.255'),
array('172.16.0.0','172.31.255.255'),
array('192.0.2.0','192.0.2.255'),
array('192.168.0.0','192.168.255.255'),
@ranaroussi
ranaroussi / api.py
Created December 22, 2015 11:10 — forked from alanhamlett/api.py
Serialize SQLAlchemy Model to dictionary (for JSON output) and update Model from dictionary attributes.
import uuid
import wtforms_json
from sqlalchemy import not_
from sqlalchemy.dialects.postgresql import UUID
from wtforms import Form
from wtforms.fields import FormField, FieldList
from wtforms.validators import Length
from flask import current_app as app
from flask import request, json, jsonify, abort
@ranaroussi
ranaroussi / multi_mysqli.php
Created October 10, 2013 08:51
Connect to multiple mysql servers. $db = new multi_mysqli(array('host1', 'host2'), 'root', '123456', 'test');
<?php
/**
* Multi-Server MySQLi class
* Givan a list of MySQL hosts, the client will connect to the first available server
* $servers can be an string for single server, or, for multiple server, use an array or comma-separated list
*/
class multi_mysqli extends mysqli {
// example from: http://php.net/manual/en/mysqli.real-connect.php
public function __construct($servers=array(), $username='', $password='', $database='test') {
@ranaroussi
ranaroussi / bindEvent.js
Created October 10, 2013 08:52
cross browser addEventListener
// as prototype
Object.prototype.bindEvent = function(event, callback, bubble) {
var elem = this,
addListener = elem.addEventListener || elem.attachEvent,
removeListener = elem.removeEventListener || elem.detachEvent,
eventName;
// custom "event" for ready
if (event === 'ready') {
eventName = document.addEventListener ? "DOMContentLoaded" : "onreadystatechange";
@ranaroussi
ranaroussi / CLI color output
Created November 19, 2013 08:52
PHP function to output colorful text via CLI
function outputCLI($text, $color=null) {
$colors = array(
'BLACK' => "0;30m",
'GRAY' => "1;30m",
'LIGHTGRAY' => "0;37m",
'BLUE' => "0;34m",
'LIGHTBLUE' => "1;34m",
'GREEN' => "0;32m",
'LIGHTGREEN' => "1;32m",
'CYAN' => "0;36m",
@ranaroussi
ranaroussi / gist:7748105
Created December 2, 2013 11:16
js userAgent parser
navigator.agent = (function() {
var ua = navigator.userAgent, ver;
// browser
var browser = ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
if (browser && (ver = ua.match(/version\/([\.\d]+)/i)) !== null) browser[2] = ver[1];
var bver = browser[2].split('.');
// os
@ranaroussi
ranaroussi / gist:8684488
Created January 29, 2014 09:29
standalone document "ready" function
function docReady(callback, bubble) {
var addListener = this.addEventListener || this.attachEvent,
removeListener = this.removeEventListener || this.detachEvent
if (document.readyState === "complete" || document.readyState === "loaded" || document.readyState === "interactive") {
callback();
return;
}