[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

top 10 last played mp3's

Robin Wagenaar

10/13/2007 9:33:00 PM

Hello everyone,

My level of ruby-experience has just slightly passed hello-world, I'm
quite new to this programming language; I hope I don't bother all of you
too much by posting my low-level question here.

I would like to make a program that iterates recursively trough my music
directory, and selects the top-10 of most recently played tracks.

I try to use File.atime(path) to check if the current file is accessed
recently enough to get into my top-10, but my problem is that I don't
know how to (correctly) compare a date to the return value of
File.atime.

I'm sure you can give me some simple hints, or a small push in the right
direction. Thanks in advance.



this is what I have so far:

require 'find'
require 'date'

dir = "/share/music"

Find.find(dir) do |path|

if File.directory?(path)
next
else
if path.match(".*mp3") != nil
print File.atime(path)," : "
print path,"\n"
end
end

end
--
Posted via http://www.ruby-....

22 Answers

Andreas S.

10/13/2007 9:49:00 PM

0

Robin Wagenaar wrote:
> My level of ruby-experience has just slightly passed hello-world, I'm
> quite new to this programming language; I hope I don't bother all of you
> too much by posting my low-level question here.
>
> I would like to make a program that iterates recursively trough my music
> directory, and selects the top-10 of most recently played tracks.
>
> I try to use File.atime(path) to check if the current file is accessed
> recently enough to get into my top-10, but my problem is that I don't
> know how to (correctly) compare a date to the return value of
> File.atime.

File.atime returns a Time object, you can compare it to another Time
object, for example:

if File.atime(path) > (Time.now - 60*60)
# file has been accessed in the last hout
end

But you can also solve the whole problem much easier:

Dir['/share/music/**/*.mp3', '/share/music/*.mp3'].sort_by{|f|
File.atime}.reverse[0,10]
--
Posted via http://www.ruby-....

Konrad Meyer

10/13/2007 11:06:00 PM

0

Quoth Andreas S.:
> Robin Wagenaar wrote:
> > My level of ruby-experience has just slightly passed hello-world, I'm
> > quite new to this programming language; I hope I don't bother all of you
> > too much by posting my low-level question here.
> >
> > I would like to make a program that iterates recursively trough my music
> > directory, and selects the top-10 of most recently played tracks.
> >
> > I try to use File.atime(path) to check if the current file is accessed
> > recently enough to get into my top-10, but my problem is that I don't
> > know how to (correctly) compare a date to the return value of
> > File.atime.
>
> File.atime returns a Time object, you can compare it to another Time
> object, for example:
>
> if File.atime(path) > (Time.now - 60*60)
> # file has been accessed in the last hout
> end
>
> But you can also solve the whole problem much easier:
>
> Dir['/share/music/**/*.mp3', '/share/music/*.mp3'].sort_by{|f|
> File.atime}.reverse[0,10]

Or, if it's deeper than two levels:

require 'find'

Find.find("/share/music/").sort_by { |f| f.atime }.reverse[0...10]

I'm glad you (the OP) are interested in learning more about ruby.

Regards,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

SpringFlowers AutumnMoon

10/13/2007 11:27:00 PM

0

Andreas S. wrote:
>
> Dir['/share/music/**/*.mp3', '/share/music/*.mp3'].sort_by{|f|
> File.atime}.reverse[0,10]

this one seems to work

a = Dir['E:/Music/*.mp3'].sort_by{|f| File.atime(f)}.reverse[0,10]
a.each {|f| p f, File.atime(f)}

except on Windows Vista, I noticed 2 things:

1) The filenames with internation characters come out as ?????.mp3
and therefore File.atime(f) will fails afterwards.

2) The access time of .mp3 or .txt or .txt is unchanged even after the
file is played, or run (such as by ruby test_dir.rb), or looked at by
Notepad.




--
Posted via http://www.ruby-....

Justin Collins

10/13/2007 11:43:00 PM

0

Robin Wagenaar wrote:
> Hello everyone,
>
> My level of ruby-experience has just slightly passed hello-world, I'm
> quite new to this programming language; I hope I don't bother all of you
> too much by posting my low-level question here.
>
> I would like to make a program that iterates recursively trough my music
> directory, and selects the top-10 of most recently played tracks.
>
> I try to use File.atime(path) to check if the current file is accessed
> recently enough to get into my top-10, but my problem is that I don't
> know how to (correctly) compare a date to the return value of
> File.atime.
>
> I'm sure you can give me some simple hints, or a small push in the right
> direction. Thanks in advance.
>
>
>
> this is what I have so far:
>
> require 'find'
> require 'date'
>
> dir = "/share/music"
>
> Find.find(dir) do |path|
>
> if File.directory?(path)
> next
> else
> if path.match(".*mp3") != nil
> print File.atime(path)," : "
> print path,"\n"
> end
> end
>
> end
>

Your original question has been answered already, but just a small note:
puts and string interpolation is generally a lot simpler to use for output:

puts "#{File.atime(path)} : #{path}"

That will also add the "\n" or "\r\n" as appropriate, so you do not need to.

-Justin

Brian Adkins

10/14/2007 12:34:00 AM

0

On Oct 13, 7:06 pm, Konrad Meyer <kon...@tylerc.org> wrote:
> require 'find'
>
> Find.find("/share/music/").sort_by { |f| f.atime }.reverse[0...10]

Doesn't Find.find() require a block? And doesn't it pass a string to
the block, not a File ?

Andreas S.

10/14/2007 12:36:00 AM

0

Konrad Meyer wrote:
> Quoth Andreas S.:
>> > know how to (correctly) compare a date to the return value of
>>
>> Dir['/share/music/**/*.mp3', '/share/music/*.mp3'].sort_by{|f|
>> File.atime}.reverse[0,10]
>
> Or, if it's deeper than two levels:

'**' takes care of that.
--
Posted via http://www.ruby-....

Brian Adkins

10/14/2007 1:26:00 AM

0

On Oct 13, 8:35 pm, "Andreas S." <x-ruby-forum....@andreas-s.net>
wrote:
> Konrad Meyer wrote:
> > Quoth Andreas S.:
> >> > know how to (correctly) compare a date to the return value of
>
> >> Dir['/share/music/**/*.mp3', '/share/music/*.mp3'].sort_by{|f|
> >> File.atime}.reverse[0,10]
>
> > Or, if it's deeper than two levels:
>
> '**' takes care of that.

Ah, you're right! I missed that also (even though the ** line is
highlighted in the pickaxe now that I bother to read Dir#glob :) ).

Before reading your explanation, I put the following together. It
might be a little friendlier for handling multiple extensions I
suppose, especially since the glob pattern isn't a real regex.

require 'pp'
require 'find'

def n_recent_files n = 1, path = '.', exts = nil
paths = []
Find.find(path) do |p|
if File.file?(p) &&
( exts.nil? ||
( (ext = File.extname(p)) &&
!ext.empty? &&
exts.include?(ext) ) )
paths << p
end
end
paths.sort! {|a,b| File.atime(b) <=> File.atime(a) }[0,n]
end

pp n_recent_files(10, '/home/brian/temp', '.mp3')
pp n_recent_files(10, '/home/brian/temp', ['.mp3', '.ogg'])

Some notes for the OP:
1) Welcome to Ruby!
2) Enumerable#sort_by can be slow in some cases, and by using sort (or
sort! in this case), the comparison can be reversed to avoid calling
Array#reverse on the entire array later.
3) It exhibits a benefit of 'duck typing' since both String and Array
define include? the caller can pass in a single string or an array of
strings for the extension parameter w/ no extra effort in the
function.
4) Although the if expression is complicated, it has the advantage of
only computing the file extension when necessary.

Brian

Brian Adkins

10/14/2007 1:51:00 AM

0

Hmm.. it just occurred to me that many of the solutions presented here
have the flaw of potentially calling File.atime() multiple times for
the same file which would require unnecessary calls to the operating
system to get the access time of the file.

Better to compute atime only once and then sort on the result. In the
case below, I then extract the file name back out of the resulting
array, but in actuality, there may be some value in providing that to
the caller in case they could make use of the access time values.

require 'pp'
require 'find'

def n_recent_files n = 1, path = '.', exts = nil
paths = []
Find.find(path) do |p|
if File.file?(p) &&
( exts.nil? ||
( (ext = File.extname(p)) &&
!ext.empty? &&
exts.include?(ext) ) )
paths << [ p, File.atime(p).to_i ]
end
end
paths.sort! {|a,b| b[1] <=> a[1] }[0,n].map {|x| x[0]}
end

pp n_recent_files(10, '/home/brian/temp', '.rb')
pp n_recent_files(10, '/home/brian/temp', ['.rb', '.txt'])

Konrad Meyer

10/14/2007 3:47:00 AM

0

Quoth Brian Adkins:
> On Oct 13, 7:06 pm, Konrad Meyer <kon...@tylerc.org> wrote:
> > require 'find'
> >
> > Find.find("/share/music/").sort_by { |f| f.atime }.reverse[0...10]
>
> Doesn't Find.find() require a block? And doesn't it pass a string to
> the block, not a File ?

Sorry, don't know anything about it, I was just going by the usage put forth
by the guy in front of me.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Robin Wagenaar

10/14/2007 9:58:00 AM

0

First of all: thank you all for your amazing amount of replies (in one
night!) and you warm welcome! You've definitely helped me solve my
problem, and made me view the problem from a different angle.

Top to bottom:

@Andreas S:
Your solution would indeed be quite a lot simpler than mine! Allthough
it might not be the most efficient, I will also implement your code,
just because it looks so darn easy! Thanks again!

@Spring Flowers:
Thanks for your concern, I don't know anything about Vista-behaviour. My
OS is Ubuntu, and when rhythmbox runs/opens a file, it's atime is set
=).

@Justin:
Thanks a lot for your string interpolation and puts idea! That really
makes life alot easier!

@Brian:
Whow, you really made my day. That was exactly what I was looking for!
Thanks a lot for the effort, code, and useful explanation! I don't
really know what to say. Thanks!
--
Posted via http://www.ruby-....