Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

jensen

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

perl grep dir and filename

// description of your code here

my($directory, $filename) = $text =~ m/(.*\/)(.*)$/;
print "D=$directory, F=$filename\n

ruby Simple RSS Parsing

def fetch_rss_items(url, max_items = nil)
%w{open-uri rss/0.9 rss/1.0 rss/2.0 rss/parser}.each do |lib|
require(lib)
end
rss = RSS::Parser.parse(open(url).read)
rss.items[0...(max_items ? max_items : rss.items.length)]
end

items = fetch_rss_items('http://www.digg.com/rss/index.xml', 5)
items.collect { |item| item.title }
=> ["Understanding AJAX - A Beginner's Guide",
"Anti-cancer Compound In Beer", ...]

perl http post

use HTTP::Request::Common qw(POST);
use LWP::UserAgent;

my $user = "user";
my $pass = "pass";

my $browser = LWP::UserAgent->new();

my $responde = HTTP::Request->new(POST => "http://www.sito.com/index.php");
$responde->content_type("application/x-www-form-urlencoded");
$responde->content("user=" . $user . "&pass=" . $pass);

$browser->request($responde)->as_string

restart appache

// description of your code here

sudo apachectl restart
..

iTunes DB parser

// description of your code here
This script sifts through your iTunes DB
From:
http://blog.zenspider.com/archives/2007/03/that_stupid_thing_i_wrote_the_other_day_part_2.html

require 'time'

class Album < Array
  def age
    map { |track| track.age }.max
  end
  def score
    total / Math.log(age)
  end
  def total
    inject(0.0) do |total_score, track|
      total_score + track.score
    end
  end
  def self.parse(file)
    today = Time.now
    d = {}
    library = Hash.new { |h,k| h[k] = Album.new }

    IO.foreach(File.expand_path(file)) do |line|
      if line =~ /<key>(Name|Artist|Album|Date Added|Play Count|Rating)<\/key><.*?>(.*)<\/.*?>/ then
        key = $1.downcase.split.first
        val = $2
        d[key.intern] = val

        if d.size == 6 then
          date = d[:date].sub(/T.*/, '')
          key = "#{d[:album]} by #{d[:artist]}"
          age = ((today - Time.parse(date)) / 86400.0).to_i
          library[key] << Track.new(age, d[:play].to_i, d[:rating].to_i / 20)

          d.clear
        end
      end
    end
    library
  end
end

Track = Struct.new(:age, :count, :rating)
class Track
  def score
    rating * count.to_f
  end
end

max = (ARGV.shift || 10).to_i
file = ARGV.shift || "~/Music/iTunes/iTunes Music Library.xml"
library = Album.parse(file)

top = library.sort_by { |h,k| -k.score }[0...max]
top.each_with_index do |(artist_album, album), c|
  puts "%-3d = (%4d tot, %5.2f adj): %s" % [c+1, album.total, album.score, artist_album,]
  album.each do |t|
    puts "  #{t.age} days old, #{t.count} count, #{t.rating} rating = #{t.score}"
  end if $DEBUG
end

io read file by byte

// description of your code here

f = File.new("c\:\\test.txt")
checksum = 0
f.each_byte {|x| 
checksum = checksum+x 
puts checksum
}

install soap4r

// install soap4r with gem , soap webservice lib

gem install soap4r --source http://dev.ctor.org/download/
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS