mdonoughe-wtth 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Matthew Donoughe
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,16 @@
1
+ # Welcome to the Horde
2
+
3
+ This is a program that watches [HVZ Source](http://humansvszombies.org/) and posts messages to [Twitter](http://twitter.com/) when people are zombified. See [@wtthumd](http://twitter.com/wtthumd) for an example(game starts 2009-09-27 at about 22:00).
4
+
5
+ Please only run one WTTH or similar per game. Check to see if your game already has one before starting your own. Nothing is gained by running duplicates, and it wastes the resources of Twitter and HVZ Source.
6
+
7
+ ## Installation
8
+ WTTH is written in [Ruby](http://www.ruby-lang.org/) and requires the [nokogiri](http://nokogiri.rubyforge.org/) and [twitter](http://twitter.rubyforge.org/) [gems](http://docs.rubygems.org/).
9
+
10
+ After you've got everything installed, run `twitterinit.rb` to give the program access to a Twitter account. I'd suggest not using your usual Twitter account for this.
11
+
12
+ Running `twitterinit.rb` will create a file called `.wtth.yml` in your home directory. You'll want to open that and change `player_list_uri` to point at the correct HVZ Source page for your game.
13
+
14
+ You'll probably want to make some sort of cron job to run `wtth.rb`. It makes at most one request to HVZ Source and posts at most one message to Twitter per run. Set this to a responsible interval. You can probably get away with as low as once a minute, but that's completely unnecessary in my opinion.
15
+
16
+ Please remember not to have the cron job running when the game is not in session.
data/bin/wtth ADDED
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'net/http'
5
+ require 'nokogiri'
6
+ require 'twitter'
7
+ require 'yaml'
8
+
9
+ wtth_path = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib', 'wtth')
10
+ require File.join(wtth_path, 'config')
11
+ require File.join(wtth_path, 'hvzsource')
12
+ require File.join(wtth_path, 'twitter')
13
+
14
+ module WTTH
15
+ VERSION = '1.0.0'
16
+ load_config
17
+
18
+ if ARGV.length > 0 and ARGV[0] == 'init'
19
+ twitter_init
20
+ exit 0
21
+ end
22
+
23
+ unless twitter_is_authorized?
24
+ puts 'You have not yet authorized WTTH to access Twitter.'
25
+ puts 'Please run `wtth init` first'
26
+ exit 1
27
+ end
28
+
29
+ # the presense of backlog indicates that we have at least one tweet worth of
30
+ # new zombies fetched already
31
+ to_welcome = CONFIG[:backlog]
32
+ to_welcome = get_new_zombies if to_welcome == nil
33
+
34
+ if to_welcome != []
35
+ this_batch = []
36
+ recovery = []
37
+ size = 0
38
+ last_welcomed = nil
39
+
40
+ # get the least recent kills, no more than 140 characters worth
41
+ until size > 140 or to_welcome.empty?
42
+ zombie = to_welcome.last
43
+ size += zombie[:name].length
44
+ size += 2 if this_batch.length > 0
45
+ if size <= 140
46
+ this_batch << zombie[:name]
47
+ last_welcomed = zombie
48
+ recovery << to_welcome.pop
49
+ end
50
+ end
51
+
52
+ # can we avoid hvzsource next run?
53
+ size = 0
54
+ to_welcome.reverse_each do |zombie|
55
+ size += 2 unless size == 0
56
+ size += zombie[:name].length
57
+ break if size >= 140
58
+ end
59
+ if size >= 140
60
+ CONFIG[:backlog] = to_welcome
61
+ else
62
+ CONFIG[:backlog] = nil
63
+ end
64
+
65
+ # welcome with the most recent first so things are in order on twitter
66
+ this_batch.reverse!
67
+
68
+ begin
69
+ tweet(this_batch.join(', '))
70
+ # remember where we left off
71
+ CONFIG[:last_welcomed] = last_welcomed
72
+ rescue Twitter::TwitterError
73
+ puts "TwitterError #{$!}"
74
+ # throw the message onto the backlog so we can try again next time
75
+ CONFIG[:backlog] = to_welcome + recovery.reverse
76
+ end
77
+ end
78
+
79
+ save_config
80
+ end
@@ -0,0 +1,20 @@
1
+ module WTTH
2
+ private
3
+ CONFIG_PATH = File::expand_path('~/.wtth.yml')
4
+ # set the defaults
5
+ CONFIG = {:player_list_uri => 'http://20cent.hvzsource.com/players.php'}
6
+
7
+ def self.load_config
8
+ if File.exists?(CONFIG_PATH)
9
+ File.open(CONFIG_PATH) do |file|
10
+ CONFIG.merge!(YAML::load(file.read))
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.save_config
16
+ File.open(CONFIG_PATH, 'w') do |file|
17
+ file.write(YAML::dump(CONFIG))
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,31 @@
1
+ module WTTH
2
+ private
3
+ def self.get_new_zombies
4
+ to_welcome = []
5
+
6
+ puts "fetching from hvzsource"
7
+
8
+ uri = URI.parse(CONFIG[:player_list_uri])
9
+ preq = Net::HTTP::Post.new(uri.path)
10
+ preq['user-agent'] = "welcome to the horde #{VERSION}"
11
+ preq.set_form_data({'faction' => 'h', 'sort_by' => 'kd', 'order' => 'd', 'show_killed' => '1', 'submit' => 'Refresh'}, '&')
12
+
13
+ req = Net::HTTP.new(uri.host, uri.port)
14
+ req.start do |http|
15
+ http.request(preq) do |resp|
16
+ doc = Nokogiri::HTML(resp.read_body)
17
+ # hvzsource produces some malformed html, so we can't use rows
18
+ # the table is <name> | horde | <tod>
19
+ cells = doc.xpath('//form/table//td')
20
+ ((cells.length - 3) / 3).times do |i|
21
+ name = cells[i * 3 + 3].content
22
+ tod = cells[i * 3 + 5].content
23
+ kill = {:name => name, :tod => tod}
24
+ break if kill == CONFIG[:last_welcomed]
25
+ to_welcome << kill
26
+ end
27
+ end
28
+ end
29
+ return to_welcome
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ module WTTH
2
+ private
3
+ APP_TOKEN = 'HgPXVDITRITul4peTLkyg'
4
+ APP_SECRET = 'iLoBnhZF3YDki5k4l09hptgg9SaRJkOYEyzsk1n0'
5
+
6
+ def self.tweet( message )
7
+ oauth = Twitter::OAuth.new(APP_TOKEN, APP_SECRET)
8
+ oauth.authorize_from_access(CONFIG[:access_token], CONFIG[:access_secret])
9
+ twitter = Twitter::Base.new(oauth)
10
+ twitter.update(message)
11
+ end
12
+
13
+ def self.twitter_is_authorized?
14
+ return (CONFIG[:access_token] != nil and CONFIG[:access_secret] != nil)
15
+ end
16
+
17
+ def self.twitter_init
18
+ oauth = Twitter::OAuth.new(APP_TOKEN, APP_SECRET)
19
+ puts "Please go to #{oauth.request_token.authorize_url} and enter the PIN number here:"
20
+ oauth.authorize_from_request(oauth.request_token.token, oauth.request_token.secret, STDIN.gets.to_i)
21
+ CONFIG[:access_token] = oauth.access_token.token
22
+ CONFIG[:access_secret] = oauth.access_token.secret
23
+
24
+ save_config
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mdonoughe-wtth
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Donoughe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: twitter
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ version:
35
+ description: Welcome to the Horde is a program that tweets when people join the zombie horde(in humans vs. zombies).
36
+ email: mdonoughe@gmail.com
37
+ executables:
38
+ - wtth
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.markdown
44
+ files:
45
+ - README.markdown
46
+ - bin/wtth
47
+ - lib/wtth/config.rb
48
+ - lib/wtth/hvzsource.rb
49
+ - lib/wtth/twitter.rb
50
+ - LICENSE
51
+ has_rdoc: true
52
+ homepage: http://github.com/mdonoughe/wtth
53
+ licenses:
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --main
57
+ - README.markdown
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.5
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: Welcome to the Horde is a program that tweets when people join the zombie horde(in humans vs. zombies).
79
+ test_files: []
80
+