diyanet 0.0.0 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/diyanet.rb +118 -116
- metadata +1 -1
    
        data/lib/diyanet.rb
    CHANGED
    
    | @@ -3,151 +3,153 @@ require 'date' | |
| 3 3 | 
             
            require 'net/http'
         | 
| 4 4 | 
             
            require 'nokogiri'
         | 
| 5 5 |  | 
| 6 | 
            -
            module  | 
| 7 | 
            -
               | 
| 8 | 
            -
                 | 
| 9 | 
            -
                   | 
| 10 | 
            -
             | 
| 11 | 
            -
                     | 
| 12 | 
            -
             | 
| 13 | 
            -
                     | 
| 6 | 
            +
            module Diyanet
         | 
| 7 | 
            +
              class Namaz < Array
         | 
| 8 | 
            +
                def initialize *args
         | 
| 9 | 
            +
                  if args.length == 1
         | 
| 10 | 
            +
                    fname = args[0]
         | 
| 11 | 
            +
                    if (File.size? fname).nil?
         | 
| 12 | 
            +
                      "File not found or empty!"
         | 
| 13 | 
            +
                    else
         | 
| 14 | 
            +
                      oku(fname)
         | 
| 15 | 
            +
                    end
         | 
| 16 | 
            +
                  elsif args.length == 2
         | 
| 17 | 
            +
                    ulke = args[0]
         | 
| 18 | 
            +
                    sehir = args[1]
         | 
| 19 | 
            +
                    @fname = File.dirname(__FILE__) + "/" + ulke + "-" + sehir + ".dat"
         | 
| 20 | 
            +
                    baglan(ulke, sehir)
         | 
| 14 21 | 
             
                  end
         | 
| 15 | 
            -
                elsif args.length == 2
         | 
| 16 | 
            -
                  ulke = args[0]
         | 
| 17 | 
            -
                  sehir = args[1]
         | 
| 18 | 
            -
                  @fname = File.dirname(__FILE__) + "/" + ulke + "-" + sehir + ".dat"
         | 
| 19 | 
            -
                  baglan(ulke, sehir)
         | 
| 20 22 | 
             
                end
         | 
| 21 | 
            -
              end
         | 
| 22 23 |  | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 24 | 
            +
                def self.oku(fname)
         | 
| 25 | 
            +
                  return self.new.oku(fname)
         | 
| 26 | 
            +
                end
         | 
| 26 27 |  | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 28 | 
            +
                def oku(fname=@fname)
         | 
| 29 | 
            +
                  @fname=fname
         | 
| 30 | 
            +
                  raise "File not found" unless File.exists? fname
         | 
| 31 | 
            +
                  File.open(fname).each_line do |line|
         | 
| 32 | 
            +
                    vakit = line.split
         | 
| 33 | 
            +
                    if current_time < vakit[5].to_i # + 86400
         | 
| 34 | 
            +
                      self << vakit.map{|v| DateTime.strptime(v, '%s').to_time.utc} 
         | 
| 35 | 
            +
                    end
         | 
| 34 36 | 
             
                  end
         | 
| 37 | 
            +
                  return finalize
         | 
| 35 38 | 
             
                end
         | 
| 36 | 
            -
                return finalize
         | 
| 37 | 
            -
              end
         | 
| 38 39 |  | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 40 | 
            +
                def yaz(fname=@fname)
         | 
| 41 | 
            +
                  begin
         | 
| 42 | 
            +
                    File.open(fname, 'w') do |f|
         | 
| 43 | 
            +
                      self.each do |vakitler|
         | 
| 44 | 
            +
                        f.puts vakitler.map{|v| v.strftime('%s')}.join(" ")
         | 
| 45 | 
            +
                      end
         | 
| 46 | 
            +
                    end 
         | 
| 47 | 
            +
                    return true
         | 
| 48 | 
            +
                  rescue
         | 
| 49 | 
            +
                    return false
         | 
| 50 | 
            +
                  end
         | 
| 49 51 | 
             
                end
         | 
| 50 | 
            -
              end
         | 
| 51 52 |  | 
| 52 | 
            -
             | 
| 53 | 
            +
                def baglan(ulke, sehir)
         | 
| 53 54 | 
             
                  ulke.upcase!
         | 
| 54 55 | 
             
                  sehir.upcase!
         | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 56 | 
            +
                  uri = URI.parse('http://www.diyanet.gov.tr/turkish/namazvakti/vakithes_namazsonuc.asp')
         | 
| 57 | 
            +
                  req = Net::HTTP::Post.new(uri.path)
         | 
| 58 | 
            +
                  req.body = "sehirler=#{sehir}&R1=AYLIK&buton=Hesapla&ulk=#{ulke}"
         | 
| 58 59 |  | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 60 | 
            +
                  res = Net::HTTP.new(uri.host, uri.port).start do |http|
         | 
| 61 | 
            +
                    http.request(req)
         | 
| 62 | 
            +
                  end
         | 
| 63 | 
            +
                  date = DateTime.now
         | 
| 64 | 
            +
                  rows = Nokogiri::HTML(res.body).xpath('//table[@border="1"]/tr')
         | 
| 65 | 
            +
                  rows.each do |row|
         | 
| 66 | 
            +
                    vakit = []
         | 
| 67 | 
            +
                    i = true 
         | 
| 68 | 
            +
                    row.xpath('./td').each do |cell|
         | 
| 69 | 
            +
                      if i 
         | 
| 70 | 
            +
                        date = DateTime.parse(cell.text)
         | 
| 71 | 
            +
                        i = false
         | 
| 72 | 
            +
                      else
         | 
| 73 | 
            +
                        time = cell.text.split
         | 
| 74 | 
            +
                        vakit.push((date + time[0].to_i/24.0 + time[1].to_i/1440.0).to_time.utc)
         | 
| 75 | 
            +
                      end
         | 
| 74 76 | 
             
                    end
         | 
| 77 | 
            +
                    self << vakit unless vakit.empty?  #and vakit[5] > @current_time
         | 
| 75 78 | 
             
                  end
         | 
| 76 | 
            -
                   | 
| 79 | 
            +
                  # return self
         | 
| 80 | 
            +
                  return finalize
         | 
| 77 81 | 
             
                end
         | 
| 78 | 
            -
                # return self
         | 
| 79 | 
            -
                return finalize
         | 
| 80 | 
            -
              end
         | 
| 81 82 |  | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 83 | 
            +
                def vakit
         | 
| 84 | 
            +
                  isim = %w(Yatsi Sabah Kusluk Ogle Ikindi Aksam)
         | 
| 85 | 
            +
                  i, j = index
         | 
| 86 | 
            +
                  return isim[j]
         | 
| 87 | 
            +
                end
         | 
| 87 88 |  | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 89 | 
            +
                def kalan(fmt=true)
         | 
| 90 | 
            +
                  j, i = index
         | 
| 91 | 
            +
                  ret = self[j][i].strftime('%s').to_i-current_time
         | 
| 92 | 
            +
                  if fmt
         | 
| 93 | 
            +
                    sa = ret/3600
         | 
| 94 | 
            +
                    dk = (ret%3600)/60
         | 
| 95 | 
            +
                    sa > 0 ? txt = "#{sa}sa" : txt=""
         | 
| 96 | 
            +
                    txt += "#{dk}dk"if dk > 0
         | 
| 97 | 
            +
                    return txt
         | 
| 98 | 
            +
                  else
         | 
| 99 | 
            +
                    return ret
         | 
| 100 | 
            +
                  end
         | 
| 99 101 | 
             
                end
         | 
| 100 | 
            -
              end
         | 
| 101 102 |  | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 103 | 
            +
                def to_hash
         | 
| 104 | 
            +
                  isim = [:imsak, :gunes, :ogle, :ikindi, :aksam, :yatsi]
         | 
| 105 | 
            +
                  hash = {}
         | 
| 106 | 
            +
                  self.each do |s|
         | 
| 107 | 
            +
                    hash[s[0].strftime('%Y%m%d').to_i] = Hash[isim.zip(s)]
         | 
| 108 | 
            +
                  end
         | 
| 109 | 
            +
                  return hash
         | 
| 107 110 | 
             
                end
         | 
| 108 | 
            -
                return hash
         | 
| 109 | 
            -
              end
         | 
| 110 111 |  | 
| 111 | 
            -
             | 
| 112 | 
            -
             | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 115 | 
            -
             | 
| 116 | 
            -
             | 
| 112 | 
            +
                def to_s(n=self.length)
         | 
| 113 | 
            +
                  puts "  Tarih    Sabah Imsak Ogle Ikindi Aksam Yatsi Kible"
         | 
| 114 | 
            +
                  #      08-12-2012 05:36 07:09 12:03 14:22 16:44 18:11 10:01
         | 
| 115 | 
            +
                  n.to_i.times do |i|
         | 
| 116 | 
            +
                    print self[i][0].strftime('%d-%m-%Y') + " "
         | 
| 117 | 
            +
                    puts self[i].map{|v| v.strftime('%H:%M')}.join(' ')
         | 
| 118 | 
            +
                  end
         | 
| 119 | 
            +
                  return  true
         | 
| 117 120 | 
             
                end
         | 
| 118 | 
            -
                return  true
         | 
| 119 | 
            -
              end
         | 
| 120 121 |  | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 122 | 
            +
                def index
         | 
| 123 | 
            +
                  j = 0
         | 
| 124 | 
            +
                  self.each do |gun|
         | 
| 125 | 
            +
                    i = 0
         | 
| 126 | 
            +
                    gun.each do |vakit|
         | 
| 127 | 
            +
                      next if i == 6
         | 
| 128 | 
            +
                      return j, i if current_time < vakit.strftime('%s').to_i
         | 
| 129 | 
            +
                      i+=1
         | 
| 130 | 
            +
                    end
         | 
| 131 | 
            +
                    j+=1
         | 
| 129 132 | 
             
                  end
         | 
| 130 | 
            -
                   | 
| 133 | 
            +
                  return false, false
         | 
| 131 134 | 
             
                end
         | 
| 132 | 
            -
                return false, false
         | 
| 133 | 
            -
              end
         | 
| 134 135 |  | 
| 135 136 |  | 
| 136 | 
            -
             | 
| 137 | 
            +
                private
         | 
| 137 138 |  | 
| 138 | 
            -
             | 
| 139 | 
            -
             | 
| 140 | 
            -
             | 
| 141 | 
            -
             | 
| 142 | 
            -
             | 
| 139 | 
            +
                def finalize
         | 
| 140 | 
            +
                  if self.empty?
         | 
| 141 | 
            +
                    return false
         | 
| 142 | 
            +
                  else 
         | 
| 143 | 
            +
                    return true 
         | 
| 144 | 
            +
                  end
         | 
| 145 | 
            +
                end
         | 
| 146 | 
            +
                def current_time
         | 
| 147 | 
            +
                  return Time.utc(*(Time.now.to_a)).strftime('%s').to_i
         | 
| 143 148 | 
             
                end
         | 
| 144 | 
            -
              end
         | 
| 145 | 
            -
              def current_time
         | 
| 146 | 
            -
                return Time.utc(*(Time.now.to_a)).strftime('%s').to_i
         | 
| 147 | 
            -
              end
         | 
| 148 149 |  | 
| 149 | 
            -
            end
         | 
| 150 | 
            +
              end
         | 
| 150 151 |  | 
| 151 | 
            -
            def Namaz *args
         | 
| 152 | 
            -
             | 
| 152 | 
            +
              def self.Namaz *args
         | 
| 153 | 
            +
                return Namaz.new(*args)
         | 
| 154 | 
            +
              end
         | 
| 153 155 | 
             
            end
         |