Created
February 12, 2012 16:07
-
-
Save madr/1809315 to your computer and use it in GitHub Desktop.
Weekly links to Posterous
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'net/https' | |
require "rexml/document" | |
require "date" | |
require 'net/smtp' | |
# delicious | |
@username = "YOU" | |
@password = "secr3t" | |
# smtp settings, asuming gmail | |
@smtp_server = 'smtp.gmail.com' | |
@smtp_port = 587 | |
@smtp_user = "YOU@gmail.com" | |
@smtp_passwd = "s3cr3t" | |
# outgoing mail | |
@mail_from = "YOU@gmail.com" | |
@mail_from_alias = "Name Surname" | |
@mail_subject = "Värt att uppmärksamma, vecka #{(Date.today.cweek)-1}" | |
# posterous | |
@posterous_email = "YOU@posterous.com" | |
####### | |
def get_description | |
description = gets.chomp | |
return description | |
end | |
# found at: http://blog.jerodsanto.net/2009/02/a-simple-ruby-method-to-send-emai/ | |
def send_email(to, opts={}) | |
opts[:server] ||= 'localhost' | |
opts[:from] ||= @mail_from | |
opts[:from_alias] ||= @mail_from_alias | |
opts[:subject] ||= @mail_subject | |
opts[:body] ||= "test" | |
msg = <<END_OF_MESSAGE | |
From: #{opts[:from_alias]} <#{opts[:from]}> | |
To: <#{to}> | |
Subject: #{opts[:subject]} | |
#{opts[:body]} | |
END_OF_MESSAGE | |
# found at: http://stackoverflow.com/questions/1183743/ruby-getting-netsmtp-working-with-gmail | |
smtp = Net::SMTP.new @smtp_server, @smtp_port | |
smtp.enable_starttls | |
smtp.start(@smtp_server, @smtp_user, @smtp_passwd, :login) | |
smtp.send_message(msg, to, to) | |
smtp.finish | |
end | |
####### | |
begin | |
resp = href = ""; | |
puts "From date, YYYY-MM-DD" | |
from_date = gets.chomp | |
puts "To date, YYYY-MM-DD" | |
to_date = gets.chomp | |
puts " " | |
url = "/v1/posts/all?fromdt=#{from_date}T00:00:00Z&todt=#{to_date}T23:59:59Z" | |
descriptions = [] | |
http = Net::HTTP.new("api.del.icio.us", 443) | |
http.use_ssl = true | |
http.start do |http| | |
req = Net::HTTP::Get.new(url, {"User-Agent" => | |
"juretta.com RubyLicious 0.2"} ) | |
req.basic_auth(@username, @password) | |
response = http.request(req) | |
resp = response.body | |
end | |
# XML Document | |
doc = REXML::Document.new(resp) | |
doc.root.elements.each do |elem| | |
puts " \n" | |
puts elem.attributes['description'] | |
puts "(" + elem.attributes['href'] + ")" | |
puts " =>" | |
descriptions.push(get_description) | |
end | |
body_txt = "<markdown>\n" | |
i = 0 | |
doc.root.elements.each do |elem| | |
if elem.attributes['extended'] != '' | |
body_txt += "* [" + elem.attributes['description'] + "]("+ elem.attributes['href'] + ") \n " + | |
descriptions[i] + "\n" #elem.attributes['extended'] + "\n" | |
i = i + 1 | |
end | |
end | |
body_txt += "</markdown>\n#end" | |
puts "\n\n" | |
puts body_txt | |
puts "\n\n" | |
puts "Sending to posterous ... " | |
#send_email @posterous_email, :body => body_txt | |
puts "Done." | |
puts " " | |
rescue SocketError | |
raise "Host " + host + " nicht erreichbar" | |
rescue REXML::ParseException => e | |
print "error parsing XML " + e.to_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment