pyaru 0.0.1

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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/p.ya.ru +31 -0
  3. data/bin/pyaru +31 -0
  4. data/lib/pyaru.rb +156 -0
  5. metadata +81 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 005249b37adf86c7e698612c9331b7405e2d2d60
4
+ data.tar.gz: 167931a981da893055651d83609fe9771a20afc1
5
+ SHA512:
6
+ metadata.gz: a186f71dfa011f60b694de06e249b776ed0f74a983524c2e7e6168513f4eb9058b69945f0be26110f6691e3682f444604daa36508e3738f49aa76efb3f7dc8c9
7
+ data.tar.gz: 54454cb7a87352f3dbea6c5e65a8ae66880f965940e3b8d07aea48c380ad27b0a092114cfcf3dcb0f3311bbf15191e92832044d9416ae44af1d9e6d665ec7b43
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # ./p.ya.ru - console client for http://p.ya.ru
4
+ #
5
+ # Author:: Kirill Kouznetsov (<agon.smith@gmail.com>)
6
+ # Copyright:: Copyright (c) 2014 Kirill Kouznetsov.
7
+ # License:: The MIT License (MIT)
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ # of this software and associated documentation files (the "Software"), to deal
11
+ # in the Software without restriction, including without limitation the rights
12
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ # copies of the Software, and to permit persons to whom the Software is
14
+ # furnished to do so, subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in
17
+ # all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ # THE SOFTWARE.
26
+
27
+ require 'rubygems'
28
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
29
+ require 'pyaru'
30
+
31
+ Pyaru.new.run
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # ./p.ya.ru - console client for http://p.ya.ru
4
+ #
5
+ # Author:: Kirill Kouznetsov (<agon.smith@gmail.com>)
6
+ # Copyright:: Copyright (c) 2014 Kirill Kouznetsov.
7
+ # License:: The MIT License (MIT)
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ # of this software and associated documentation files (the "Software"), to deal
11
+ # in the Software without restriction, including without limitation the rights
12
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ # copies of the Software, and to permit persons to whom the Software is
14
+ # furnished to do so, subject to the following conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be included in
17
+ # all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ # THE SOFTWARE.
26
+
27
+ require 'rubygems'
28
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
29
+ require 'pyaru'
30
+
31
+ Pyaru.new.run
@@ -0,0 +1,156 @@
1
+ #
2
+ # Author:: Kirill Kouznetsov (<agon.smith@gmail.com>)
3
+ # Copyright:: Copyright (c) 2014 Kirill Kouznetsov.
4
+ # License:: The MIT License (MIT)
5
+ #
6
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ # of this software and associated documentation files (the "Software"), to deal
8
+ # in the Software without restriction, including without limitation the rights
9
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ # copies of the Software, and to permit persons to whom the Software is
11
+ # furnished to do so, subject to the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be included in
14
+ # all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ # THE SOFTWARE.
23
+
24
+ require 'nokogiri'
25
+ require 'open-uri'
26
+ require 'colorize'
27
+ require 'net/http'
28
+
29
+ class Pyaru
30
+ def temp_color_selector(temperature = 1)
31
+ if temperature >= 25
32
+ :red
33
+ elsif temperature < 25 && temperature >= 18
34
+ :light_red
35
+ elsif temperature < 18 && temperature >= 10
36
+ :light_yellow
37
+ elsif temperature < 10 && temperature > 0
38
+ :white
39
+ elsif temperature <= 0 && temperature >= -5
40
+ :light_cyan
41
+ else
42
+ :blue
43
+ end
44
+ end
45
+
46
+ def help
47
+ puts %q(Yandex weather (p.ya.ru) page crawler.
48
+
49
+ Delivers nice and clean weather info using p.ya.ru website right to your
50
+ console. Now a nice weather forecast is right under your fingertips.
51
+
52
+ Author: Kirill Kouznetsov <agon.smith@gmail.com>
53
+
54
+ This software is made just for fun so use it on your own risk.
55
+ )
56
+
57
+ exit 0
58
+ end
59
+
60
+ def get_default_city_name
61
+ # Request for http://p.ya.ru/ returns redirect to the city of your current location.
62
+ Net::HTTP.get_response('p.ya.ru', '/')['Location'].gsub(%r{^.*/([^/]+)$}, '\1')
63
+ end
64
+
65
+ def parse_options
66
+ if ARGV.size > 1
67
+ STDERR.puts 'Wrong number of arguments.'
68
+ STDERR.puts 'Please provide City name as in http://p.ya.ru/<CITY_NAME> url.'
69
+ STDERR.puts 'Or omit them at all for default selection.'
70
+ STDERR.puts
71
+ exit 1
72
+ elsif ARGV.size == 1
73
+ help if ARGV[0] == '-h' || ARGV[0] == '--help'
74
+ city = ARGV[0]
75
+ else
76
+ city = get_default_city_name
77
+ end
78
+
79
+ city.downcase
80
+ end
81
+
82
+ def open_weather_page(city)
83
+ Nokogiri::HTML(open("http://p.ya.ru/#{URI::encode(city)}"))
84
+ end
85
+
86
+ def get_page_city_name(page)
87
+ page.css('footer div[class=bigweather] a')[0]['href'].gsub(%r{^.*/([^/]+)/$}, '\1')
88
+ end
89
+
90
+ def get_current_temperature(page)
91
+ page.css('p[class=temperature-wrapper] span[class=temperature]')[0]['data-t']
92
+ end
93
+
94
+ def generate_header(city, temperature)
95
+ header = city.colorize(:light_red)
96
+ header << ', '
97
+ header << temperature.colorize(temp_color_selector(temperature.to_i))
98
+
99
+ header_shift = ((14 * 5) - header.uncolorize.size) / 2
100
+
101
+ "#{' ' * header_shift}#{header}"
102
+ end
103
+
104
+ def get_forecast_data(page)
105
+ t_list = page.css('div[class=temperatures] div[class=chart_wrapper]')
106
+
107
+ t_list.map do |point|
108
+ [
109
+ point.css('p[class=th]').text.to_i, # hour
110
+ point.css('div[class=chart]')[0]['data-t'].to_i, # temperature
111
+ point.css('span[class=icon_rain]')[0] ? true : false # rain status
112
+ ]
113
+ end
114
+ end
115
+
116
+ def print_forecast_grid(forecast)
117
+ # Hours line
118
+ forecast.each do |i|
119
+ printf('%5s'.colorize(:light_black), i[0].to_s)
120
+ end
121
+ printf("\n")
122
+
123
+ # temperatures line
124
+ forecast.each do |i|
125
+ printf('%5s'.colorize(temp_color_selector(i[1])), i[1])
126
+ end
127
+ printf("\n")
128
+
129
+ # Rain status line
130
+ forecast.each do |i|
131
+ if i[2]
132
+ printf('%5s', '☂')
133
+ else
134
+ printf('%5s', ' ')
135
+ end
136
+ end
137
+ printf("\n")
138
+ end
139
+
140
+ def run
141
+ city = parse_options
142
+ weather_page = open_weather_page(city)
143
+
144
+ resolved_city_name = get_page_city_name(weather_page)
145
+ puts "WARNING: City '#{city}' was not found. Falled back to: '#{resolved_city_name}'\n\n" if resolved_city_name.gsub('-', ' ') != city.gsub('-', ' ')
146
+ city = resolved_city_name.split(' ').map(&:capitalize).join(' ')
147
+
148
+ puts generate_header(city, get_current_temperature(weather_page))
149
+ puts
150
+
151
+ print_forecast_grid(get_forecast_data(weather_page))
152
+ puts
153
+ end
154
+ end
155
+
156
+
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pyaru
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kirill Kouznetsov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ description: "\n The weather in Moscow was terrible in that october. I was bored.
42
+ Caught\n myself opening http://p.ya.ru weather page every couple of hours. And
43
+ each\n time had to switch to the full-scale browser. But stop! I'm working in
44
+ the\n termimal most of the time! Why should you use a browser for such a simple\n
45
+ \ task?! I even call 'date' for current time. And so this simple gem emerged.\n
46
+ \ Just for fun.\n "
47
+ email: agon.smith@gmail.com
48
+ executables:
49
+ - p.ya.ru
50
+ - pyaru
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - bin/p.ya.ru
55
+ - bin/pyaru
56
+ - lib/pyaru.rb
57
+ homepage: http://github.com/dragonsmith/pyaru/
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: cli weather client.
81
+ test_files: []