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-4 of 4 total  RSS 

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", ...]

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

   1  
   2  require 'time'
   3  
   4  class Album < Array
   5    def age
   6      map { |track| track.age }.max
   7    end
   8    def score
   9      total / Math.log(age)
  10    end
  11    def total
  12      inject(0.0) do |total_score, track|
  13        total_score + track.score
  14      end
  15    end
  16    def self.parse(file)
  17      today = Time.now
  18      d = {}
  19      library = Hash.new { |h,k| h[k] = Album.new }
  20  
  21      IO.foreach(File.expand_path(file)) do |line|
  22        if line =~ /<key>(Name|Artist|Album|Date Added|Play Count|Rating)<\/key><.*?>(.*)<\/.*?>/ then
  23          key = $1.downcase.split.first
  24          val = $2
  25          d[key.intern] = val
  26  
  27          if d.size == 6 then
  28            date = d[:date].sub(/T.*/, '')
  29            key = "#{d[:album]} by #{d[:artist]}"
  30            age = ((today - Time.parse(date)) / 86400.0).to_i
  31            library[key] << Track.new(age, d[:play].to_i, d[:rating].to_i / 20)
  32  
  33            d.clear
  34          end
  35        end
  36      end
  37      library
  38    end
  39  end
  40  
  41  Track = Struct.new(:age, :count, :rating)
  42  class Track
  43    def score
  44      rating * count.to_f
  45    end
  46  end
  47  
  48  max = (ARGV.shift || 10).to_i
  49  file = ARGV.shift || "~/Music/iTunes/iTunes Music Library.xml"
  50  library = Album.parse(file)
  51  
  52  top = library.sort_by { |h,k| -k.score }[0...max]
  53  top.each_with_index do |(artist_album, album), c|
  54    puts "%-3d = (%4d tot, %5.2f adj): %s" % [c+1, album.total, album.score, artist_album,]
  55    album.each do |t|
  56      puts "  #{t.age} days old, #{t.count} count, #{t.rating} rating = #{t.score}"
  57    end if $DEBUG
  58  end

io read file by byte

// description of your code here

   1  
   2  f = File.new("c\:\\test.txt")
   3  checksum = 0
   4  f.each_byte {|x| 
   5  checksum = checksum+x 
   6  puts checksum
   7  }

install soap4r

// install soap4r with gem , soap webservice lib

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