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

Recording a voice note using Asterisk and ProjectX web services

The script below stores the voice note in a micro-blog (group_dynarex project called 'voicenotes').

   1  
   2  #!/usr/bin/ruby
   3  #file: voicenote.rb
   4  
   5  require 'open-uri'
   6  require 'ftools'
   7  require 'rexml/document'
   8  include REXML
   9  
  10  file = ARGV[0] # eg. tasksgeneral
  11  
  12  url = 'http://justsomewebsite.com.com/p/convert_audio/wav2ogg/wav_url=http://192.168.1.107/temp/tasks.wav'
  13  buffer = open(url, "UserAgent" => "RVoiceMgr").read
  14  doc = Document.new(buffer)
  15  node_result = doc.root.attribute('method')
  16  
  17  unless node_result.nil?
  18    audio_filepath = 'audio/n' + Time.now.strftime("%y%m%d%H%M%S") + '.ogg'
  19    File.copy('/var/www/temp/tasks.ogg', '/var/www/' + audio_filepath)
  20    url_create = "http://justsomewebsite.com.com/p/group_dynarex/create/path=voicenotes&file=#{file}&audio=" + audio_filepath
  21    puts url_create
  22  
  23    buffer_create = open(url_create, "UserAgent" => "RVoiceMgr").read
  24    doc_create = Document.new(buffer_create)
  25  
  26    node_result_create = doc_create.root.attribute('method')
  27    puts 'success' unless node_result_create.nil?
  28  
  29  end


References:
- Run the Apache web server as an Asterisk user [dzone.com]
- Creating a new file with Group_DynareX [dzone.com]
- Using a RewriteRule to simplify a ProjectX action [dzone.com]
- Recording audio using Asterisk [dzone.com]
- Executing a Ruby script from Asterisk [dzone.com]
- Creating a clean url for a ProjectX method [dzone.com]
- Simplify a ProjectX request using projectx-helper.cgi [dzone.com]
- Jott.com | Voice to Text Notes & To Dos. Email & Text Message Reminders. ...[jott.com]
- Copy a file from one web server to another [dzone.com]
- Convert a WAV to OGG [dzone.com]

Creating a bucket in Amazon S3 through an irb session

1) Log into an irb session, and enter your S3 login details.
   1  
   2  require 'rubygems'
   3  require 'aws/s3'
   4  
   5    AWS::S3::Base.establish_connection!(
   6      :access_key_id     => 'REPLACE_ME',
   7      :secret_access_key => 'REPLACE_ME'
   8    )

output:
=> #<AWS::S3::Connection:0xb75e0594 @http=#<Net::HTTP s3.amazonaws.com:80 open=false>, @secret_access_key="", @options={:server=>"s3.amazonaws.com", :access_key_id=>"", :port=>80, :secret_access_key=>"", :persistent=>true}, @access_key_id="19S45GYAGWK8DC2B8VG2">

2) Browse the existing buckets.
   1  AWS::S3::Service.buckets

output:
=> [#<AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=>"ogg.twitteraudio.com", "creation_date"=>Sat Apr 26 10:40:16 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=>"t1000", "creation_date"=>Fri Apr 25 21:35:21 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=>"t2000", "creation_date"=>Fri Apr 25 21:53:15 UTC 2008}>]

3) Browse the buckets in a programmatical way.
   1  AWS::S3::Service.buckets.each {|b| puts b.name}

output:
ogg.twitteraudio.com
t1000
t2000


4) Add a new bucket called t3000.
   1  AWS::S3::Bucket.create('t3000')

output:
=> true

5) Observe adding the bucket again doesn't cause an error.
   1  AWS::S3::Bucket.create('t3000')

output:
=> true

6) View the buckets again.
   1  AWS::S3::Service.buckets

output:
=> [#<AWS::S3::Bucket:0xb75cc850 @object_cache=[], @attributes={"name"=>"ogg.twitteraudio.com", "creation_date"=>Sat Apr 26 10:40:16 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc83c @object_cache=[], @attributes={"name"=>"t1000", "creation_date"=>Fri Apr 25 21:35:21 UTC 2008}>, #<AWS::S3::Bucket:0xb75cc814 @object_cache=[], @attributes={"name"=>"t2000", "creation_date"=>Fri Apr 25 21:53:15 UTC 2008}>]

Note: You would expect t3000 to be in there however it didn't appear possibly because of the bucket permissions.

7) Let's then look for bucket t3000.
   1  t3000 = AWS::S3::Bucket.find('t3000')

output:
=> #<AWS::S3::Bucket:0xb76df724 @object_cache=[], @attributes={"prefix"=>nil, "name"=>"t3000", "marker"=>nil, "max_keys"=>1000, "is_truncated"=>false, "xmlns"=>"http://s3.amazonaws.com/doc/2006-03-01/"}>

8) Now that we've found the bucket let's upload a text file called works.txt.
   1  file = "works.txt"

output:
=> "works.txt"
   1  AWS::S3::S3Object.store(file, open(file), 't3000', :access => :public_read)

output:
=> #<AWS::S3::S3Object::Response:0x-608926458 200 OK>

9) Setting the file access to :public_read allows us to view the file from the http location http://t3000.s3.amazonaws.com/works.txt

References:
http://amazon.rubyforge.org/
upload_to_s3 - Ruby S3 upload client [dzone.com]

*update: 14:30 30 April 2008 *
I didn't use Bucket.objects(:reload) which is the reason why the bucket t3000 didn't show up with the statement Service.buckets

Reference: spatten design - Amazon S3, Ruby and Rails slides [spattendesign.com]

Persisting Application data using Hashtable and IsolatedStorage

// nice way to store application data and settings
// copied from: http://www.dotnetspider.com/kb/Article344.aspx

   1  
   2  using System;
   3  using System.Collections;
   4  using System.IO;
   5  using System.IO.IsolatedStorage;
   6  using System.Runtime.Serialization;
   7  using System.Runtime.Serialization.Formatters.Binary;
   8  
   9  namespace CustomStorage
  10  {
  11      [Serializable]
  12      public class ApplicationStorage : Hashtable
  13      {
  14          // File name. Let us use the entry assembly name with .dat as the extension.
  15          private string settingsFileName = 
  16                          System.Reflection.Assembly.GetEntryAssembly().GetName().Name + ".dat";
  17      
  18          // The default constructor.
  19          public ApplicationStorage()
  20          {
  21              LoadData();
  22          }
  23          
  24          // This constructor is required for deserializing our class from persistent storage.
  25          protected ApplicationStorage (SerializationInfo info, StreamingContext context)
  26              : base(info, context)
  27          {
  28          }
  29          
  30          private void LoadData()
  31          {
  32              IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore( IsolatedStorageScope.User 
  33                          | IsolatedStorageScope.Assembly, null, null );
  34              if ( isoStore.GetFileNames( settingsFileName ).Length == 0 )
  35              {
  36                  // File not exists. Let us NOT try to DeSerialize it.
  37                  return;
  38              }
  39              
  40              // Read the stream from Isolated Storage.
  41              Stream stream = new IsolatedStorageFileStream( settingsFileName, 
  42                          FileMode.OpenOrCreate, isoStore );
  43              if ( stream != null )
  44              {
  45                  try
  46                  {
  47                      // DeSerialize the Hashtable from stream.
  48                      IFormatter formatter = new BinaryFormatter();
  49                      Hashtable appData = ( Hashtable ) formatter.Deserialize(stream);
  50                      
  51                      // Enumerate through the collection and load our base Hashtable.
  52                      IDictionaryEnumerator enumerator = appData.GetEnumerator();
  53                      while ( enumerator.MoveNext() )
  54                      {
  55                          this[enumerator.Key] = enumerator.Value;
  56                      }
  57                  }
  58                  finally
  59                  {
  60                      // We are done with it.
  61                      stream.Close();
  62                  }
  63              }
  64          }
  65          
  66          public void ReLoad()
  67          {
  68              LoadData();
  69          }
  70          
  71          // Saves the configuration data to the persistent storage.
  72          public void Save()
  73          {
  74              // Open the stream from the IsolatedStorage.
  75              IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore( IsolatedStorageScope.User 
  76                          | IsolatedStorageScope.Assembly, null, null );
  77              Stream stream = new IsolatedStorageFileStream( settingsFileName, 
  78                          FileMode.Create, isoStore );
  79          
  80              if ( stream != null )
  81              {
  82                  try
  83                  {
  84                      // Serialize the Hashtable into the IsolatedStorage.
  85                      IFormatter formatter = new BinaryFormatter();
  86                      formatter.Serialize( stream, (Hashtable)this );
  87                  }
  88                  finally
  89                  {
  90                      stream.Close();
  91                  }
  92              }
  93          }
  94      }
  95  }


//sample usage
   1  
   2  
   3  CustomStorage.ApplicationStorage storage = new CustomStorage.ApplicationStorage();
   4  
   5  storage["name"] = "john";
   6  storage["age"] = 23;
   7  storage["address"] = "#10, Vasant Nagar";
   8  
   9  storage.Save();
  10  
  11  string name = storage["name"].ToString();
  12  int age = int.parse(storage["age"].ToString());
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS