national_day_list 1.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: a4f0191a0d59500fb0fa9d2775d984cf265c12b1
4
+ data.tar.gz: 0c96fef51af4be800a46d7140f0655c62dd077d0
5
+ SHA512:
6
+ metadata.gz: 7d4062496798b0c38a334f0f2ea5c2ce077ef7ec305f0fe7e7562e3ea647a8c464379d62a6d2e2103cc5e4e65bfbbd675e394cea30843a81a60162c25b27df47
7
+ data.tar.gz: 6f77acbdfb55dfc875eeb79ee7257bba824afcfabb45c464774378574e1d57d13e7c279cf900e6307373d8503f8b2c32ff408af55a51b9512572c6640039c047
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Patrick Sattelberger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # National Day List
2
+ A simple api & cli for getting a list of national days from 'www.nationaldaycalendar.com'.
3
+
4
+ ## Dependencies
5
+ * Ruby
6
+ * Ruby gems
7
+
8
+ *Ruby dev may also be required*
9
+
10
+ ## Installation
11
+
12
+ gem install national_day_list
13
+
14
+ ## Running
15
+ nationaldaylist
16
+
17
+ ## Api Usage
18
+
19
+ require 'nationaldaylist'
20
+
21
+ #Create the api with caching
22
+ api = NationalDayList.new
23
+ #Create the api without caching
24
+ api = NationalDayList.new(false)
25
+
26
+ #Get a month. You can alse specify its number ("3") or index (2)
27
+ month = api.get_month("march")
28
+
29
+ #Returns true if the month is cached, teks the same perameters as #get_month
30
+ cached = api.cached?("march")
31
+
32
+ #get_month will cache the month when called and return the cached data if available
33
+ #Clear the cache
34
+ api.clear_cache()
35
+
36
+ #Get the first day of the month
37
+ day = month[0]
38
+ #Get a readable title from the day
39
+ title = day.title
40
+
41
+ #Get the first national day at this date
42
+ nday = day.days[0]
43
+ #get National Day name
44
+ nday_name = nday.name
45
+ #Get summary
46
+ #Fetch details if we don't already have them
47
+ nday.add_details unless nday.has_details
48
+ nday_summary = nday.summary
49
+
50
+ ## Contributing
51
+ Pull requests welcome
52
+ * Try to follow existing coding styles and patterns.
53
+ * Write new tests for new api changes (Adding information or features).
54
+ * Make sure all tests pass before submitting.
55
+
56
+ If you have questions or feedback you can contact me at sattelbergerp@gmail.com.
57
+ *Yes I used two spaces for indentation; no I don't like it either.*
58
+
59
+ ## License
60
+ MIT
61
+ See LICENSE.txt
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'nationaldaylist'
3
+
4
+ CLI.new.run
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'colorize'
5
+ require 'launchy'
6
+ require 'require_all'
7
+ Bundler.require
8
+
9
+ require_all 'lib'
data/lib/cli.rb ADDED
@@ -0,0 +1,110 @@
1
+ require_relative '../config/enviroment'
2
+
3
+ class CLI
4
+
5
+ MONTHS= ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
6
+
7
+ attr_reader :api
8
+
9
+ def initialize()
10
+ @cache = {}
11
+ @api = NationalDayList.new()
12
+ end
13
+
14
+ def run
15
+ puts "CLI for 'www.nationaldaycalendar.com'. Type 'help' for usage."
16
+ loop do
17
+ print '>'
18
+ cmd = gets.chomp.split(" ")
19
+ case cmd[0]
20
+ when "exit"
21
+ return;
22
+ when "today"
23
+ date = Date.today
24
+ parseDateCommand([date.month.to_s, date.mday.to_s] + cmd[1..-1])
25
+ when 'help'
26
+ puts "Type '1' or 'january' to view all national days for january."
27
+ puts "Type '1 2' or 'january' first to view all national days for january second."
28
+ puts "Type '1 2 4' to view the fourth national day for january second."
29
+ puts "Month and day of month can be replaced with 'today' in all commands."
30
+ puts "Type 'exit' to exit"
31
+ else
32
+ parseDateCommand(cmd);
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+ def parseDateCommand(cmd)
39
+ puts "Loading Month Information..." unless api.cached?(cmd[0])
40
+ month = api.get_month(cmd[0])
41
+ if month
42
+ if cmd.count > 1
43
+ parseDayCommand(cmd, month)
44
+ else
45
+ month.count.times {|index| printDay(month[index], cmd[0], index+1)}
46
+ end
47
+ else
48
+ puts "Unknown month or command".colorize(:light_red)
49
+ end
50
+ end
51
+
52
+ def parseDayCommand(cmd, month)
53
+ day_index = cmd[1].to_i-1
54
+ day = month[day_index]
55
+ if day && day_index > -1
56
+ if cmd.count>2
57
+ parseDayDetailsCommand(cmd, month, day)
58
+ else
59
+ printDay(day, cmd[0], cmd[1])
60
+ end
61
+ else
62
+ puts "Could not find day in month".colorize(:light_red)
63
+ end
64
+ end
65
+
66
+ def parseDayDetailsCommand(cmd, month, day)
67
+ nday_index = cmd[2].to_i-1
68
+ nday = day.days[nday_index]
69
+ if nday && nday_index>-1
70
+ if cmd.count > 3
71
+ parseDayOperationCommand(nday, cmd)
72
+ else
73
+ printDayDetails(nday, cmd[0], cmd[1], cmd[2])
74
+ end
75
+ else
76
+ puts "Can't find national day on specified date".colorize(:light_red)
77
+ end
78
+ end
79
+
80
+ def parseDayOperationCommand(nday, cmd)
81
+ case cmd[3]
82
+ when "open"
83
+ Launchy.open(nday.url)
84
+ else
85
+ puts "Unknown command for national day".colorize(:light_red)
86
+ end
87
+ end
88
+
89
+ #takes a day of month
90
+ def printDay(day_of_month, month_name, day_name)
91
+ puts "#{day_of_month.title}".colorize(:light_yellow)
92
+ day_of_month.days.each_with_index do |nday, index|
93
+ puts " #{month_name} #{day_name} #{index+1}".colorize(:light_blue)+". #{nday.name}"
94
+ end
95
+ puts ""
96
+ end
97
+
98
+ def printDayDetails(national_day, month_name, day_name, nday_name)
99
+ if !national_day.has_details
100
+ puts "Loading National Day Information..."
101
+ national_day.add_details
102
+ end
103
+ puts "#{national_day.url}"
104
+ puts "#{national_day.name}".colorize(:light_yellow)
105
+ puts "#{national_day.summary}" if national_day.summary!=nil && !national_day.summary.empty?
106
+ puts ""
107
+ puts "Type '#{month_name} #{day_name} #{nday_name} open' to view in your default browser."
108
+ end
109
+
110
+ end
data/lib/dayofmonth.rb ADDED
@@ -0,0 +1,18 @@
1
+ class DayOfMonth
2
+ attr_accessor :title, :days
3
+
4
+ #utility method to take the array of hashes returned by the scraper and turn them into instances of this object
5
+ def self.array_from_hash_array(hash_array)
6
+ hash_array.collect {|hash|
7
+ self.new(hash)
8
+ }
9
+ end
10
+
11
+ def initialize(hash)
12
+ hash.each do |k,v|
13
+ self.send("#{k}=",v)
14
+ end
15
+ @days = @days.collect {|day| NationalDay.new(day)}
16
+ end
17
+
18
+ end
@@ -0,0 +1,44 @@
1
+ require_relative '../config/enviroment'
2
+
3
+ class NationalDayList
4
+
5
+ attr_accessor :use_cache
6
+ MONTHS ||= ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
7
+ URL_BASE ||= "http://www.nationaldaycalendar.com/"
8
+ VERSION ||= '1.0.1'
9
+
10
+ def initialize(use_cache=true)
11
+ @use_cache = use_cache
12
+ @cache = {}
13
+ end
14
+
15
+ def get_month(input)
16
+ id = get_month_id(input)
17
+ month = @cache[id] if use_cache
18
+ month = DayOfMonth.array_from_hash_array(Scraper.scrape_month("#{URL_BASE}#{id}/")) if @cache[id]==nil || !use_cache
19
+ @cache[id] = month if use_cache
20
+ month
21
+ end
22
+
23
+ def cached?(input)
24
+ @cache[get_month_id(input)]!=nil&&use_cache
25
+ end
26
+
27
+ def clear_cache
28
+ @cache = {}
29
+ end
30
+
31
+ private
32
+ def get_month_id(input)
33
+ index = get_month_index(input)
34
+ return MONTHS[index] unless index < 0
35
+ nil
36
+ end
37
+
38
+ def get_month_index(input)
39
+ return input if input.class==Fixnum
40
+ return input.to_i()-1 unless input.to_i()==0
41
+ MONTHS.find_index(input.downcase)
42
+ end
43
+
44
+ end
data/lib/scraper.rb ADDED
@@ -0,0 +1,35 @@
1
+
2
+ class Scraper
3
+
4
+ #Used in openuri requests to prevent 403
5
+ #Coppied from chrome on windows 10
6
+ USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
7
+
8
+ def self.scrape_month(full_url)
9
+ doc = Nokogiri::HTML(open(full_url, 'User-Agent'=>USER_AGENT))
10
+ days_of_month = []
11
+ tiles = doc.css('div.et_pb_blurb_container')
12
+ tiles.each do |tile|
13
+ day_of_month = {}
14
+ day_of_month[:title] = tile.css('h4').text
15
+ day_of_month[:days] = tile.css('li a').collect do |day_link|
16
+ {name: day_link.text, url: day_link.attr('href')}
17
+ end
18
+ days_of_month << day_of_month
19
+ end
20
+ days_of_month
21
+ end
22
+
23
+ def self.scrape_day_details(full_url)
24
+ doc = Nokogiri::HTML(open(full_url, 'User-Agent'=>USER_AGENT))
25
+ details = {}
26
+ doc.css(".post-content > p").each do |item|
27
+ if !item.text.strip.empty?
28
+ details[:summary] = item.text.gsub("\u00A0", ' ')
29
+ break
30
+ end
31
+ end
32
+ details
33
+ end
34
+
35
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: national_day_list
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Sattelberger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: require_all
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: launchy
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: colorize
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email: sattelbergerp@gmail.com
113
+ executables:
114
+ - nationaldaylist
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - LICENSE.txt
119
+ - README.md
120
+ - bin/nationaldaylist
121
+ - config/enviroment.rb
122
+ - lib/cli.rb
123
+ - lib/dayofmonth.rb
124
+ - lib/nationaldaylist.rb
125
+ - lib/scraper.rb
126
+ homepage: https://github.com/sattelbergerp/national-day-list
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.4.5.2
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: A simple api & cli for getting a list of national days from 'www.nationaldaycalendar.com'.
150
+ test_files: []