Skip to content

Instantly share code, notes, and snippets.

@tommorris
Created August 14, 2010 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommorris/524820 to your computer and use it in GitHub Desktop.
Save tommorris/524820 to your computer and use it in GitHub Desktop.
cz_newdrafts script - licensed under the BSD license
#!/usr/bin/env ruby
require "rubygems"
require "open-uri"
require "bitly"
require "active_support"
require "nokogiri"
require "twitter_oauth"
# set up our bitly shortener
Bitly.use_api_version_3
shortener = Bitly.new("tommorris", "SECRET")
# pull in consent data
$consent = YAML.load_file("/home/tom/.cz_nd_twitter.yaml")
def grab_author_str(name)
filtered_consent = $consent.select {|i| i[:name] == name }
return filtered_consent.first[:twitter] if filtered_consent.first[:twitter] != nil
return filtered_consent.first[:name] if filtered_consent.first
return nil
end
# set up twitter connection
client = TwitterOAuth::Client.new(:consumer_key => "SECRET",
:consumer_secret => "SECRET",
:token => "SECRET",
:secret => "SECRET")
# parse XML
doc = Nokogiri::XML(open("http://en.citizendium.org/wiki?title=Special:NewPages&feed=rss&namespace=0"))
items = doc.xpath("//item").map do |i|
{
:title => i.xpath("title")[0].text,
:author => i.xpath("dc:creator")[0].text,
:link => i.xpath("link")[0].text,
:date => Time.parse(i.xpath("pubDate")[0].text),
:lemma => i.xpath("description").text["BASEPAGENAME"] ? true : false
}
end
if ARGV.member?("--no-lemmas")
items = items.select {|i| i[:lemma] != true }
end
# retrieve only recent posts
items = items.select {|i| i[:date] >= (10.minutes.ago - 15.seconds)}
items = items.select do |i|
i unless (i[:title] =~ /^.*\/Definition$/ || i[:title] =~ /^.*\/Related Articles$/ || i[:title] =~ /\/Catalogs$/ || i[:title] =~ /\/Bibliography$/ || i[:title] =~ /\/Addendum$/ || i[:title] =~ /^.*\/External Links$/)
end
# shorten URLs and replace author strings with Twitter URLs - this will get replaced with a call out to JSON file
items.map! do |i|
i[:link] = shortener.shorten(i[:link], :history => 1).short_url
i[:author] = grab_author_str(i[:author])
i
end
# send it to Twitter or debug with --debug
items.each do |i|
output = i[:title] + " " + i[:link]
output += " by " + i[:author] unless i[:author].nil?
if ARGV.member?("--debug")
puts output
else
client.update output
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment