Posted by Dan Sosedoff
on February 15, 2009
As previous post was about fetching covers media from Amazon Web Services, this post will be about fetching covers from popular music site – Last.fm. API documentation page
#!/usr/bin/ruby
require 'rubygems'
require 'net/http'
require 'cgi'
require 'xmlsimple'
# key from API documentation
$lastfm_key = "b25b959554ed76058ac220b7b2e0a026"
$lastfm_host = "ws.audioscrobbler.com"
def fetch_cover(artist, album)
artist = CGI.escape(artist)
album = CGI.escape(album)
path = "/2.0/?method=album.getinfo&api_key=#{$lastfm_key}&artist=#{artist}&album=#{album}"
data = Net::HTTP.get($lastfm_host, path)
xml = XmlSimple.xml_in(data)
if xml['status'] == 'ok' then
album = xml['album'][0]
cover = {
:small => album['image'][1]['content'],
:medium => album['image'][2]['content'],
:big => album['image'][3]['content']
}
return cover
end
return nil
end
puts fetch_cover('Nickelback', 'Dark Horse').inspect
Download ruby script
Posted by Dan Sosedoff
on February 15, 2009
On my small project i was looking for web service to get media covers from. I found that i can use Amazon Web Services API. The documentation for this ECommerce Service is pretty old, but it still works.
More detailed information about API you can find here
#!/usr/bin/ruby
require 'rubygems'
require 'net/http'
require 'cgi'
require 'xmlsimple'
$amazon_key = "12DR2PGAQT303YTEWP02" # NOT MY KEY (FOUND ON INTERNET)
$amazon_host = "webservices.amazon.com"
def fetch_cover(artist, album)
artist = CGI.escape(artist)
album = CGI.escape(album)
path = "/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=#{$amazon_key}&Operation=ItemSearch&SearchIndex=Music&Artist=#{artist}&ResponseGroup=Images&Keywords=#{album}"
data = Net::HTTP.get($amazon_host, path)
xml = XmlSimple.xml_in(data)
if xml['Items'][0]['TotalResults'].to_s.to_i then
cover = {
:small => xml['Items'][0]['Item'][0]['SmallImage'][0]['URL'],
:medium => xml['Items'][0]['Item'][0]['MediumImage'][0]['URL'],
:big => xml['Items'][0]['Item'][0]['LargeImage'][0]['URL']
}
return cover
end
return nil
end
So, after execution of this function you will get array with 3 different images (small, medium, big).
I use XML-Simple gem for ruby. Can be installed this way
sudo gem install xml-simple
That`s it. Download script
Posted by Dan Sosedoff
on February 13, 2009
Since i started writing simple manuals about how to make system daemons i found bunch of interesting documents. For today, i just want to publish one of them instead of writing source code. This is manual originally written by Devin Watson, can be very useful for those how have no idea how to develop such system daemons. It`s only a basic information.
http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html
Posted by Dan Sosedoff
on February 06, 2009
Sometimes it`s necessary to delete all .svn directories that were created by subversion version control system. This shell command will work on all unix systems:
rm -rf `find . -name .svn`
That`s it!
Posted by Dan Sosedoff
on February 05, 2009
This is a simple piece of code which describing how to update special order field in table. For example, you have table ‘photos’:
+ --------------------------------- +
| ID | UserID | OrderID | ImgURL |
+ --------------------------------- +
| 1132 | 1 | 0 | ... |
| 2124 | 1 | 0 | ... |
| 3456 | 1 | 0 | ... |
.....................................
| N | 1 | 0 | ... |
+ --------------------------------- +
OrderID field value by default is 0. If some images has been deleted order lose its consistency.
To repair order we can use single transaction:
BEGIN;
SET @ORDER := 0;
UPDATE photos
SET order_id = (SELECT @ORDER := @ORDER + 1)
WHERE user_id = 1;
COMMIT;
After executing this on table ‘images’ we`ll get such result:
+ --------------------------------- +
| ID | UserID | OrderID | ImgURL |
+ --------------------------------- +
| 1132 | 1 | 1 | ... |
| 2124 | 1 | 2 | ... |
| 3456 | 1 | 3 | ... |
.....................................
| N | 1 | M | ... |
+ --------------------------------- +
Some developers instead of using resources of mysql script language trying to solve this problem by executing numerous call, such ‘UPDATE table SET order_id = [value] WHERE id = [id] LIMIT 1;’. It`s wrong. MySQL language have a lot of ways to make it easy. At least UDF`s.