Skip to content

Instantly share code, notes, and snippets.

@crhan
crhan / gist:2558511
Created April 30, 2012 13:50
用 ruby 算一下去 '第二届中国推荐系统大会-TopGeek和GTUG联合主办' 的公司组成
# -*- coding: utf-8 -*-
require 'nokogiri'
require 'open-uri'
require 'pry'
doc = Nokogiri::HTML(open('http://resys.51qiangzuo.com/'))
a = doc.xpath('//table[@class="attendee_list"]/tr/td').grep(/公司/).map{|a| a.text.gsub(/公司:\s*/,"")}
num = {}
list = {
"土豆" => "土豆网",
@crhan
crhan / bot.rb
Created May 4, 2012 15:41
无聊抓百度贴吧上的小说更新, 原理是贴吧更新小说都会置顶, 所以只要在置顶里面判断是否符合 '第几章节' 即可, 最后用各种手段推送就行了. 这里使用了飞信和Gtalk.
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{$0} -r JID -m content"
opts.on('-m', '--message MESSAGE','Message to be sent') do |m|
options[:message] = m
end
@crhan
crhan / site.yml
Created September 12, 2012 11:14
Ruhoh Sitemap Generator
plugins:
sitemap_generator: # all optional
file_name: sitemap.xml
exclude_id:
- search.html
- pages.html
change_frequency_custom_name: changefreq
priority_custom_variable_name: priority
@crhan
crhan / apple.rb
Last active December 21, 2015 19:39
Apple~~
# -*- encoding : utf-8 -*-
##
# Apple 类, 只有一个属性, 叫做重量 (@weight)
# Apple 还有一个类属性, 叫做苹果数量 (@@count)
class Apple
@@count = 0
##
# 我是类方法, 直接在 Apple 上面调用我就行了
def self.count
@crhan
crhan / dhcp_mac_ip.py
Created September 12, 2013 16:24
print DHCP leases ip and mac pair that are not expire
#!/usr/bin/env python
# coding: UTF-8
import re
import time
DHCPD_LEASES_FILE="/var/lib/dhcpd/dhcpd.leases"
# read dhcpd_leases file and match all ip,mac pair, and convert to dict type to store
dhcp_leases = open(DHCPD_LEASES_FILE,"r").read()
@crhan
crhan / deploy.rb
Created December 19, 2013 09:40
make capistrano deploy code in a limited parallel
namespace :deploy do
## set max_hosts 20 or via env
set :max_hosts, ENV['MAXHOSTS'] || 20
## set max_hosts on update_code
task :update_code, :except => { :no_release => true }, :max_hosts => "#{max_hosts}" do
logger.info "\e[0;31;1mNOTICE:\e[0m Pulling repo from #{repository}: max_hosts => '#{max_hosts}'"
on_rollback { run "rm -rf #{release_path}; true" }
strategy.deploy!
finalize_update
PREFIX="/usr/local"
RBENV_PREFIX="$PREFIX/rbenv"
BIN_PATH="$PREFIX/bin"
RBENV_GROUP="rbenv"
umask 002
## Install rbenv, ruby-build and rbenv-gem-rehash
git clone git://github.com/sstephenson/rbenv.git $RBENV_PREFIX
@crhan
crhan / attributes.rb
Last active January 1, 2016 14:28
chef deploy rbenv and binary ruby
default["ops-oob"]["rbenv_tar"] = "rbenv_6u2.tar.gz"
@crhan
crhan / deploy.rb
Last active January 1, 2016 14:49
deploy rails app to remote server, which has a limited internet access.
# config/deploy.rb
namespace :deploy do
desc "check local vendor/cache"
task :bundled_package do
run_locally do
unless test "[[ -d #{File.expand_path("../../vendor/cache", __FILE__)} ]]"
error "please `bundle package --all` first!"
raise StandardError
end
@crhan
crhan / operating_system.rb
Last active September 12, 2016 15:10
rbenv.spec
module Gem
class << self
##
# Returns full path of previous but one directory of dir in path
# E.g. for '/usr/share/ruby', 'ruby', it returns '/usr'
def previous_but_one_dir_to(path, dir)
split_path = path.split(File::SEPARATOR)
File.join(split_path.take_while { |one_dir| one_dir !~ /^#{dir}$/ }[0..-2])