alvalaxia 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Rogério Vicente
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File without changes
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'rubygems'
5
+
6
+ # add lib to load path
7
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../lib'))
8
+
9
+ require 'alvalaxia'
10
+ Alvalaxia::Runner.new.start
@@ -0,0 +1,8 @@
1
+ module Alvalaxia
2
+ VERSION = '0.1'
3
+
4
+ autoload :Runner, File.dirname(__FILE__)+'/alvalaxia/runner'
5
+ autoload :Scrapper, File.dirname(__FILE__)+'/alvalaxia/scrapper'
6
+ autoload :Game, File.dirname(__FILE__)+'/alvalaxia/game'
7
+ autoload :Calendar, File.dirname(__FILE__)+'/alvalaxia/calendar'
8
+ end
@@ -0,0 +1,46 @@
1
+ require 'base64'
2
+ require 'gcal4ruby'
3
+
4
+ module Alvalaxia
5
+
6
+ # Gcal4Ruby wrapper class
7
+ class Calendar
8
+
9
+ def initialize
10
+ config = read_config
11
+ @service = GCal4Ruby::Service.new
12
+ @service.authenticate(config['credentials']['email'], config['credentials']['password'])
13
+ @calendar = GCal4Ruby::Calendar.find(@service, {:id => config['credentials']['email']})
14
+ end
15
+
16
+ def create_event(start_date)
17
+ event = GCal4Ruby::Event.new(@service)
18
+ event.calendar = @calendar
19
+ event.title = 'SCP Plays Home'
20
+ event.all_day = true
21
+ event.start_time = start_date
22
+ event.end_time = start_date
23
+ event.where = 'Alvalade XXI'
24
+ event.reminder = [{ :minutes => 24*60, :method => 'email' }]
25
+ event.save
26
+ end
27
+
28
+ def delete_all_events
29
+ current_events = GCal4Ruby::Event.find(@service, 'SCP Plays Home')
30
+ current_events.each do |ev|
31
+ ev.delete
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ # Reads the configuration file containing the google account
38
+ # (spare me the insecure bullshit, you should
39
+ # not be using your own google account/calendar.
40
+ # Just create a new one to use with this gem)
41
+ def read_config
42
+ YAML.load_file(File.expand_path('~/.alvalaxiarc'))
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ require 'time'
2
+
3
+ module Alvalaxia
4
+
5
+ # Contains needed info of a game
6
+ class Game
7
+
8
+ attr_accessor :date, :opponent, :competition
9
+
10
+ def initialize(date, opponent, competition)
11
+ @date = Time.parse(date.gsub('/', '-'))
12
+ @opponent = opponent
13
+ @competition = competition
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,98 @@
1
+ require 'cmdparse'
2
+ require 'fileutils'
3
+ require 'yaml'
4
+
5
+ module Alvalaxia
6
+
7
+ class Runner
8
+
9
+ CONFIG_FILE = File.expand_path('~/.alvalaxiarc')
10
+
11
+ def initialize
12
+ @cmd = CmdParse::CommandParser.new(true, true)
13
+ @cmd.program_name = 'alvalaxia'
14
+ @cmd.program_version = VERSION
15
+
16
+ generate_commands
17
+ end
18
+
19
+ # start the cli args parsing
20
+ def start
21
+ @cmd.parse
22
+ end
23
+
24
+ # create all the cli options
25
+ def generate_commands
26
+
27
+ # creating the setup command
28
+ setup = CmdParse::Command.new('setup', false)
29
+ setup.short_desc = 'Creates the config file needed to place the events in a google calendar.'
30
+ setup.description = 'This command creates the ~/.alvalaxiarc file with empty credentials. '
31
+ setup.description << 'User needs to fill in the google calendar credentials.'
32
+
33
+ setup.set_execution_block do
34
+ if has_config_file?
35
+ puts 'The config file already exists. If you need to change it, edit it ( ~/.alvalaxiarc )'
36
+ else
37
+ create_config_file
38
+ puts 'Config file created at ~/.alvalaxiarc, edit it with your credentials.'
39
+ end
40
+ end
41
+
42
+ # creating the run command
43
+ run = CmdParse::Command.new('run', false)
44
+ run.short_desc = 'Runs the program.'
45
+ run.description = 'Gets the SCP home games and places them as events on a given google calendar.'
46
+
47
+ run.set_execution_block do
48
+ if has_config_file?
49
+ main
50
+ else
51
+ puts 'You first need to run the "setup" option to generate a config file.'
52
+ end
53
+ end
54
+
55
+ @cmd.add_command(setup)
56
+ @cmd.add_command(run)
57
+ @cmd.add_command(CmdParse::HelpCommand.new, true)
58
+ @cmd.add_command(CmdParse::VersionCommand.new, false)
59
+ end
60
+
61
+ private
62
+
63
+ def main
64
+ puts 'Fetching SCP games calendar...'
65
+ games = Alvalaxia::Scrapper.new.fetch
66
+
67
+ # this should not be here... but its late, and I'm lazy :P
68
+ puts 'Saving games in Google Calendar...'
69
+ begin
70
+ cal = Alvalaxia::Calendar.new
71
+ cal.delete_all_events
72
+
73
+ games.each do |game|
74
+ # if game is during the week...
75
+ unless ['Sunday', 'Saturday'].include?(game.date.strftime("%A"))
76
+ cal.create_event(game.date)
77
+ end
78
+ end
79
+ puts 'Done!'
80
+ rescue
81
+ puts 'Error: config file not found or with incorrect data.'
82
+ end
83
+ end
84
+
85
+ def has_config_file?
86
+ File.exist?(CONFIG_FILE)
87
+ end
88
+
89
+ # creates the config file with dummy info to be edited
90
+ def create_config_file
91
+ FileUtils.touch(CONFIG_FILE)
92
+ content = { 'credentials' => { 'email' => 'YOUR_GOOGLE_CALENDAR_EMAIL_HERE', 'password' => 'YOUR_PASSWORD_HERE' } }
93
+ File.open(CONFIG_FILE, 'w') { |f| f.write(content.to_yaml) }
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+
5
+ module Alvalaxia
6
+
7
+ # Exception to raise when an error occurs scrapping the site
8
+ class UnableToParseDataError < Exception; end
9
+
10
+ class Scrapper
11
+
12
+ # page that lists the next SCP games
13
+ USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1'
14
+ NEXT_GAMES_URL = 'http://www.zerozero.pt/equipa.php?grp=0&ond=c&compet_id_jogos=0&epoca_id=141&id=16&menu=nextmatches&type=season'
15
+
16
+ def initialize
17
+ @games = Array.new
18
+ end
19
+
20
+ def fetch
21
+ doc = Nokogiri::HTML(open(NEXT_GAMES_URL, 'User-Agent' => USER_AGENT))
22
+ doc.css('table.statsn tr').each do |item|
23
+ tds = Array.new
24
+ item.css('td').each do |td|
25
+ tds << td.text
26
+ end
27
+ unless tds.empty?
28
+ # index 0 contains date, 5 the opponent and 6 the competition
29
+ game = Alvalaxia::Game.new(tds[0], tds[5], tds[6])
30
+ @games << game
31
+ end
32
+ end
33
+
34
+ if @games.empty?
35
+ raise UnableToParseDataError
36
+ end
37
+
38
+ return @games
39
+ end
40
+
41
+ end
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alvalaxia
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
10
+ platform: ruby
11
+ authors:
12
+ - "Rog\xC3\xA9rio Vicente"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-08-23 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: gcal4ruby
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ hash: 1
28
+ segments:
29
+ - 0
30
+ - 5
31
+ - 5
32
+ version: 0.5.5
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: cmdparse
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - "="
42
+ - !ruby/object:Gem::Version
43
+ hash: 9
44
+ segments:
45
+ - 2
46
+ - 0
47
+ - 3
48
+ version: 2.0.3
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: nokogiri
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - "="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 1
62
+ - 5
63
+ - 0
64
+ version: 1.5.0
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ description:
68
+ email: rogeriopvl@gmail.com
69
+ executables:
70
+ - alvalaxia
71
+ extensions: []
72
+
73
+ extra_rdoc_files: []
74
+
75
+ files:
76
+ - LICENSE
77
+ - README.md
78
+ - bin/alvalaxia
79
+ - lib/alvalaxia.rb
80
+ - lib/alvalaxia/runner.rb
81
+ - lib/alvalaxia/scrapper.rb
82
+ - lib/alvalaxia/calendar.rb
83
+ - lib/alvalaxia/game.rb
84
+ homepage: ""
85
+ licenses: []
86
+
87
+ post_install_message: "------------------------------------------------------------------------------\n Usage:\n alvalaxia run\n \n There are more options available. You can find more info by typing:\n \n alvalaxia help\n \n You can send feedback to < rogeriopvl@gmail.com >. Have fun!\n\
88
+ ------------------------------------------------------------------------------\n "
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.9
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Fetches all Sporting Clube de Portugal football home games and adds them as events to a Google Calendar.This is useful for (mainly portuguese) people that park their car daily on the SCP stadium park.
118
+ test_files: []
119
+