0

I am currently playing with ruby and have come up with the following

require 'simple_youtube'

def video_search(term, maxresults, file)
  video_search = Youtube::Video.find(:params => {:q => "#{term}", :"max-results" => "#{maxresults}", :v => '2'})
  min = 0                                                           # => setting min variable = 0
  max = video_search.entry.size                                 # => setting max to #{maxresults}
  while min < max do
    export_results(file,video_search.entry[min].link[0].href)   # => outputs each of the results to the listed file
    min +=1
  end
  run = exec("~/Scripts/BASH/./music.sh #{file}")                   #=> Automatically downloads the items
end

def export_results(file, item)
  open(file, 'a') do |f|
    f.puts "#{item}\n"
  end
end

video_search(ARGV[0],ARGV[1],ARGV[2])                   # => Call the search with arguments
                                                        # => ARGV[0] - #{term}
                                                        # => ARGV[1] - #{maxresults}
                                                        # => ARGV[2] - #{file}

What I would like to do now is have a file of search terms and call the video_search with each item in the file

file='/users/Ondrovic/Desktop/music.txt'
  f = File.open(file, "r")
  f.each_line { |line|
    puts line
  }
  f.close

The above I can read each line just not sure how to incorporate the two properly.

/users/Ondrovic/Desktop/music.txt would contain something like the following

 Trapt Trapt
 Godsmack Shine Down
 etc

Then read each of them from the file and run the search

5
  • What is your question and what are you exactly trying to do? Commented Oct 26, 2014 at 17:08
  • @User089247 want to feed in a text file with search terms to the video_search function Commented Oct 26, 2014 at 17:11
  • What's with data in Test.txt and what's in /users/Ondrovic/Desktop/music.txt file? Commented Oct 26, 2014 at 17:13
  • so the data in /users/Ondrovic/Desktop/music.txt would be the terms I want to search for, so I just need to feed each item into the video_search function Commented Oct 26, 2014 at 17:15
  • You should really fix the code indentation. github.com/bbatsov/ruby-style-guide Commented Oct 26, 2014 at 17:15

1 Answer 1

1

NOTE: This is not a best practice since Ruby is all about OO programming.

But, here is how you can stitch your code to make it work:

require 'simple_youtube'

def video_search(term, maxresults, file) 
  video_search = Youtube::Video.find(:params => {:q => "#{term}", :"max-results" => "#{maxresults}", :v => '2'})
  min = 0                                                         # => setting min variable = 0
  max = video_search.entry.size                                   # => setting max to #{maxresults}
  while min < max do
    export_results(file,video_search.entry[min].link[0].href)   # => outputs each of the results to the listed file
    min +=1
  end

  run = exec("~/Scripts/BASH/./music.sh #{file}")                 #=> Automatically downloads the items
end

def export_results(file, item)
  open(file, 'a') do |f|
    f.puts "#{item}\n"
  end
end

# => Call the search with arguments
# => line    - term
# => ARGV[0] - maxresults
# => ARGV[1] - file
file = '/users/Ondrovic/Desktop/music.txt' # or your can use `ARGV[0]` for term's file path
maxresults = ARGV[0] || 10
if ARGV[1]
  f = File.open(file, "r")
  f.each_line { |line| video_search(line, maxresults, ARGV[1]) }
  f.close
else
  puts 'Please provide two arguments: first as maxresults and second the file path'
end
Sign up to request clarification or add additional context in comments.

2 Comments

what would be the best practice?
Note that you can always use Ruby in a functional programming style. But since Ruby is a pure object-oriented programming language. I'd encourage you to create a class and define attributes instead of arguments, instantiate object of class and pass those arguments as attributes while initializing the objects and doing the iteration. However, the above code should have worked, right? Please don't forget to accept answer of your question on SO if they solves your problem. Have a happy programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.