Skip to content

Instantly share code, notes, and snippets.

@skojin
skojin / backup_bookmarks.rb
Last active April 10, 2017 07:58
backup imhonet.com
COOKIE = ARGV[0]
TO = ARGV[1] || 'json/bookmarks'
`mkdir -p #{TO}`
%w{films serials books games person}.each do |type|
print "#{type} "
(1..99).each do |page|
json = `curl 'http://#{type}.imhonet.ru/web.php?path=favorites/all/&domain=#{type}&page=#{page}' --silent -H '#{COOKIE}' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' -H 'Accept: application/json' -H 'Referer: http://films.imhonet.ru/favorites/all/' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' --compressed`
break unless json[0] == '{' # if content is not json, then pager ended
print '.'
@skojin
skojin / deep_freeze.rb
Created August 19, 2016 13:19
ruby deep freeze without monkey patch
# recursive freeze of hash, array
module DeepFreeze
extend self
def freeze(obj_or_enumerable)
if obj_or_enumerable.respond_to?(:each)
obj_or_enumerable.each { |v| DeepFreeze.freeze(v) }
end
obj_or_enumerable.freeze
obj_or_enumerable
SELECT CONCAT(table_schema, '.', table_name), CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows, CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA, CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx, CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size, ROUND(index_length / data_length, 2) idxfrac FROM information_schema.TABLES ORDER BY data_length + index_length DESC LIMIT 20;
@skojin
skojin / multi_hosts_reverse_proxy
Last active August 29, 2015 14:08
Nginx reverse proxy
server {
listen 80 default_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
location / {
proxy_pass http://desthost.com;
}
}
module UpdateIgnore
def update_ignore(updates, conditions)
set = updates.map{|k,v| "#{connection.quote_column_name(k)} = #{connection.quote v}" }.join(', ')
connection.update "UPDATE IGNORE #{quoted_table_name} SET #{set} WHERE #{self.where(conditions).where_clauses.join('AND')}"
end
end
module ActiveRecordSelectAttributesExtension
# Returns an array of arrays containing the field values.
# Order is the same as that returned by +columns+.
# select_rows("id, type") => [[1, 'one'], [2, 'one'], [3, 'two']]
def select_rows(fields = nil)
scope = fields ? self.scoped.select(fields) : self.scoped
connection.select_rows(scope.to_sql)
end
@skojin
skojin / application_controller.rb
Created October 18, 2012 13:55
make p and puts in controller actions output into console and color it to make more bright
class ApplicationController < ActionController::Base
include ColorizedPrint if Rails.env.development?
end
@skojin
skojin / youtube_download.rb
Created February 21, 2012 13:31
download video from youtube subscription
# download video from youtube subscription
# depends: youtube-dl
# OS: windows
require 'open-uri'
require 'hpricot'
USERNAME = ''
YOUTUBE_DL_PATH = "python C:\\programs\\Video\\youtube\\youtube-dl\\youtube-dl.py"
YOUTUBE_DL_PARAMS = "--retries=30 --max-quality=22 -c -i -w"
@skojin
skojin / v1
Created January 26, 2012 01:53
Get public IP address and copy to clipboard
curl -silent http://checkip.dyndns.org | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | pbcopy
@skojin
skojin / navigation_helper.rb
Created November 15, 2011 14:40
Rails Helper to build 'active' links
module NavigationHelper
# get navigation 'active' css class if rule match
def nav_class(match_rules)
navigation_url_active?(nil, match_rules) ? 'active' : nil
end
# @param match_rules if :resource symbol, then match to url that starts withs specified url
# @param match_rules if :same symbol, then match exactly to url
# @param match_rules if hash {:controller, :action, :path, :method} and same yes with _not suffix (like :controller_not)