Наверное самые полезные и выразительные мысли, прочитанные мной за год

писать код надо не для компиляторов, а для людей, которые будут читать это после вас.
в maintenance это неоспоримое и первостепенное правило (не убий) если можно так выразиться.
(лямкайте на название поста чтобы посмотреть мысли)

simple mp3 cover finder

very bad coding style, not finished
but i don't want to do it better.

1) find and download picture



require 'net/http'
require 'uri'
#require 'FileUtils'

def download_pic_by_str( str )
req_str = str.split(" ").join("+")

uri = "http://images.google.com/images?hl=en&client=firefox&rls=org.mozilla%3Aru%3Aofficial&um=1&sa=1&q=#{req_str}&btnG=Search+images&aq=f&oq=&aqi=g10&start=0ask_google.rb"
url = URI.parse( uri )
res = Net::HTTP.get(url)

ind = res.index('dyn.setResults([["/imgres?')
http_ind = res.index( "http", ind )
http_end = res.index( '\\' , http_ind )

uri = res[http_ind,http_end - http_ind]
puts "What image download: "+uri.to_s
file_name = uri[/([a-zA-Z0-9\-_])*\.[a-zA-Z0-9\-_]*$/]
puts "Result filename: "+file_name
url = URI.parse( uri )
res = Net::HTTP.get(url)
f = File.new("images/"+file_name,"wb")
f.print( res )
f.close
end

if __FILE__ == $0

if ARGV.size > 0
request_string = ARGV.join(" ")
else
request_string = "beatles let it be"
end
FileUtils.mkdir_p 'images'

download_pic_by_str request_string
end


2) this file should be runned, send search string to download function



require "get_mp3_tag"
require "ask_google_a_picture"
require "iconv"

ic_w_u = Iconv.new('WINDOWS-1251','CP866')
ic_w_u2 = Iconv.new('UTF-8','WINDOWS-1251')

mp3_name = "Give Me Some Love.mp32"
mp3_name = ic_w_u2.iconv( mp3_name )
puts "Mp3 filename :" + mp3_name

FileUtils.mkdir_p 'images'

request = mp3_name + get_str("mp3/#{mp3_name}.mp3")

download_pic_by_str( request )

3) generate search string phraze to mp3

require 'rubygems'
require 'id3lib'

def get_str( mp3_file_path )
tag = ID3Lib::Tag.new(mp3_file_path)
puts "Title: "+tag.title.to_s
tag.title.to_s+" "+tag.album.to_s+" "+tag.track.to_s
end


i paid for it about an hour.

Crimea Trip

Loading...