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

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

A Ruby script for MPD

Install MPD along with phpMp and control your music media centre from a Ruby script.

   1  
   2  #!/usr/bin/ruby
   3  #file: mp_control.rb
   4  
   5  require 'open-uri'
   6  cmd = ARGV[0]
   7      open("http://192.168.1.107/phpMp/playlist.php?hide=1&command=#{cmd}", 'User-Agent' => 'Ruby-MPClient').read
   8  


Example: mp_control.rb play

Ruby equivalent of a simple Wget

Reads the file contents from a URL. I used this code to work around the problem with the Ruby RSS reader which couldn't read the RSS file from digg.com. The reason being that the website would not allow files to be downloaded without supplying the User-Agent string.

   1  
   2  require 'open-uri'
   3  puts open('http://digg.com/rss/index.xml',
   4       'User-Agent' => 'Ruby-Wget').read

Download recent flickr pictures with ruby and the flickr api

// To make this work, you need to get your own flickr api key.
// Get one here: http://www.flickr.com/services/api/misc.api_keys.html
// Other than that, just plug and chug and have fun!
// The "b" in "wb" in the second open method may not be necessary in
// non-windows environments.

   1  
   2  require 'open-uri'
   3  require 'rexml/document'
   4  
   5  open('http://www.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=YOUR_KEY_HERE') { |f|
   6      doc = REXML::Document.new f.read
   7      i = 0
   8      doc.elements.each("rsp/photos/photo") { |element|
   9          if i < 3
  10              open("images/file" << i.to_s << ".jpg", "wb").
  11                  write(open("http://static.flickr.com/" << \
  12                  element.attributes["server"] << "/" << \
  13                  element.attributes["id"] << "_" << \
  14                  element.attributes["secret"] << "_o.jpg").read)
  15          else
  16              break
  17          end
  18          i = i + 1
  19      }
  20  }
  21  
  22  puts "Done!"

Load a Web page in Ruby and print information

Found at http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/

   1  
   2  require 'open-uri'
   3  require 'pp'
   4  
   5  open('http://www.juretta.com/') do |f|
   6    # hash with meta information
   7    pp  f.meta
   8     
   9    # 
  10    pp "Content-Type: " + f.content_type
  11    pp "last modified" + f.last_modified.to_s
  12    
  13    no = 1
  14    # print the first three lines
  15    f.each do |line|
  16      print "#{no}: #{line}"
  17      no += 1
  18      break if no > 4
  19    end
  20  end
  21  

Snippet to grab historical data for stocks

This is a quick little snippet I'm whipping up to import some historical data in for a graphing app we're building for stock data. I figured I'd post this snippet before I maul it into something application-specific...

It downloads a csv file through yahoo's finance site and then parses it and prints out the date and adjusted close for each business day that has data.

   1  
   2  require 'open-uri'
   3  require 'csv'
   4  
   5  def get_adjusted_close stock_symbol
   6    puts "-- #{stock_symbol} Adjusted Close - Historical --"
   7    url = "http://ichart.finance.yahoo.com/table.csv?s=#{stock_symbol}&d=7&e=1&f=2006&g=d&a=2&b=26&c=1990&ignore=.csv"
   8    puts "Connecting to #{url}\n"
   9  
  10    csv = CSV.parse(open(url).read)
  11  
  12    csv.each{|row|
  13      puts "#{row[0]} - #{row.last}"
  14    }
  15    puts "---------------------------------"
  16  end
  17  
  18  example_stocks = "CSCO GOOG"
  19  print "Enter a series of stock symbols separated by spaces (example: #{example_stocks}) to retrieve the historical adjusted close.\n"
  20  stock_symbols = gets
  21  stock_symbols ||= example_stocks
  22  
  23  stock_symbols.split.each{|symbol|
  24    get_adjusted_close(symbol)
  25  }
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS