aemet 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 112ccf8efb06966833b07d716884902bc51f0c2a
4
+ data.tar.gz: 4bc953d1d5785cbff2731bdd688f7e7d464b6a7e
5
+ SHA512:
6
+ metadata.gz: cef7edef28068eb771778dc95ddebcbe34b4f63f74de1d59cd2776133be2ee9f12186c8354cfe100d1dd72f5145bee3272c939f23127f41e00a89fb810f22f34
7
+ data.tar.gz: ff00b94ce17f6137ba8ea072a3fe41409b6cb7cb3535aca473cef7e9430bd0af2a5a6842e986b6916429a022a6f01a947b7d6532b64e505ee922d89d709da06b
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aemet.gemspec
4
+ gemspec
5
+
6
+ gem 'pry'
7
+ gem 'pry-doc'
8
+ gem 'interactive_editor'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jorge Calás Lozano
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Aemet
2
+
3
+ Crawls information from http://www.aemet.es
4
+
5
+ Extracted from http://para.glid.es source code.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'aemet'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install aemet
20
+
21
+ ## Usage
22
+
23
+ Extracts AEMET data url for city:
24
+
25
+ city = Aemet::Crawler.new("A Coruña")
26
+
27
+ # When initialized the crawler searchs for the city and initializes it.
28
+ city.aemet_id # => "15030"
29
+
30
+ # XML url is constructed with `aemet_id`
31
+ city.xml_url # => http://www.aemet.es/xml/municipios/localidad_15030.xml
32
+
33
+ # Get current Weather data
34
+ city.weather
35
+
36
+ # Get the city page url (from xml response)
37
+ city.weather.origen.enlace # => http://www.aemet.es/es/eltiempo/prediccion/municipios/coruna-a-id15030
38
+
39
+ # The actual weather forecast
40
+ city.weather.prediccion.dia # => [ { "fecha" => "2013-03-23", "viento" => [] ...}, { "fecha" => ...} ]
41
+
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/aemet.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aemet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aemet"
8
+ spec.version = Aemet::VERSION
9
+ spec.authors = ["Joaquin Rivera Padron", "Jorge Dias", "Jorge Calás Lozano"]
10
+ spec.email = ["joahking@gmail.com", "jorge@mrdias.com", "calas@qvitta.net"]
11
+ spec.description = %q{Extracts information from http://www.aemet.es}
12
+ spec.summary = %q{Extracts information from http://www.aemet.es}
13
+ spec.homepage = "http://para.glid.es"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport'
22
+ spec.add_dependency 'nokogiri'
23
+ spec.add_dependency 'httparty'
24
+ spec.add_dependency 'hashie'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ end
data/bin/aemet ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'aemet'
@@ -0,0 +1,48 @@
1
+ require 'httparty'
2
+ require 'nokogiri'
3
+ require 'hashie'
4
+
5
+ module Aemet
6
+ BASE_URL = "http://www.aemet.es"
7
+
8
+ class Crawler
9
+
10
+ attr_reader :city
11
+ attr_accessor :aemet_id
12
+
13
+ def initialize(city)
14
+ @city = CGI.escape(city.parameterize(" "))
15
+ search_city
16
+ end
17
+
18
+ def search_city
19
+ city_pattern = ".resultados_busqueda li a:contains('Predicción por Municipios')"
20
+
21
+ search_results = Nokogiri::HTML(HTTParty.get(search_url)).search(city_pattern)
22
+
23
+ first_result = search_results.first
24
+
25
+ if first_result
26
+ first_result[:href] =~ /-id(.*)\Z/
27
+
28
+ self.aemet_id = $1
29
+
30
+ OpenStruct.new(:aemet_xml_url => xml_url)
31
+ else
32
+ raise "Not found"
33
+ end
34
+ end
35
+
36
+ def weather
37
+ @prediction ||= Hashie::Mash.new(HTTParty.get(xml_url)).root
38
+ end
39
+
40
+ def xml_url
41
+ "#{BASE_URL}/xml/municipios/localidad_#{aemet_id}.xml"
42
+ end
43
+
44
+ def search_url
45
+ "#{BASE_URL}/es/buscador?modo=and&orden=n&tipo=sta&str=#{city}"
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,37 @@
1
+ module Aemet
2
+ class Parser
3
+ attr_reader :document
4
+
5
+ def initialize(document)
6
+ @document = document
7
+ end
8
+
9
+ def result(date)
10
+ day = document.css("prediccion>dia[fecha='#{date}']")
11
+
12
+ results = {}
13
+
14
+ day.css('estado_cielo').each do |e|
15
+ period = e[:periodo]
16
+ results[period] ||= {}
17
+ results[period][:sky] = e.text
18
+ end
19
+
20
+ day.css('viento').each do |v|
21
+ period = v[:periodo]
22
+ results[period] ||= {}
23
+ results[period][:wind] ||= {}
24
+ results[period][:wind][:velocity] = v.css('velocidad').text
25
+ results[period][:wind][:direction] = v.css('direccion').text
26
+ end
27
+
28
+ day.css('prob_precipitacion').each do |p|
29
+ period = p[:periodo]
30
+ results[period] ||= {}
31
+ results[period][:rain] = p.text
32
+ end
33
+
34
+ results
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Aemet
2
+ VERSION = "0.0.1"
3
+ end
data/lib/aemet.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "active_support/all"
2
+ require "aemet/crawler"
3
+ require "aemet/version"
4
+
5
+ module Aemet
6
+ # Your code goes here...
7
+ end
data/lib/tasks/.keep ADDED
File without changes
@@ -0,0 +1,23 @@
1
+ namespace :aemet do
2
+ def spanish_sites
3
+ FlyingSite.where(:country_code => 'ES')
4
+ end
5
+
6
+ task :crawl => :environment do
7
+ Aemet::Crawler.crawl spanish_sites.where(:aemet_xml_url => nil, :aemet_page_url => nil)
8
+ end
9
+
10
+ task :import_xml => :environment do
11
+ require 'open-uri'
12
+ spanish_sites.find_each do |fs|
13
+ print "."
14
+
15
+ xml_io = open fs.aemet_xml_url
16
+
17
+ xml = Iconv.conv('utf8', 'ISO-8859-15', xml_io.read)
18
+
19
+ fs.aemet_data.create! :xml => xml
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Aemet do
4
+ it 'should have a version number' do
5
+ Aemet::VERSION.should_not be_nil
6
+ end
7
+
8
+ it 'should do something useful' do
9
+ false.should be_true
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'aemet'
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aemet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joaquin Rivera Padron
8
+ - Jorge Dias
9
+ - Jorge Calás Lozano
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-10-09 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: nokogiri
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: httparty
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ - !ruby/object:Gem::Dependency
58
+ name: hashie
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: bundler
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.3'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: '1.3'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rake
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: rspec
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ description: Extracts information from http://www.aemet.es
114
+ email:
115
+ - joahking@gmail.com
116
+ - jorge@mrdias.com
117
+ - calas@qvitta.net
118
+ executables:
119
+ - aemet
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - .gitignore
124
+ - .rspec
125
+ - .travis.yml
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - aemet.gemspec
131
+ - bin/aemet
132
+ - lib/aemet.rb
133
+ - lib/aemet/crawler.rb
134
+ - lib/aemet/parser.rb
135
+ - lib/aemet/version.rb
136
+ - lib/tasks/.keep
137
+ - lib/tasks/aemet.rake
138
+ - spec/aemet_spec.rb
139
+ - spec/spec_helper.rb
140
+ homepage: http://para.glid.es
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.0.3
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Extracts information from http://www.aemet.es
164
+ test_files:
165
+ - spec/aemet_spec.rb
166
+ - spec/spec_helper.rb
167
+ has_rdoc: