Created
February 22, 2012 14:19
-
-
Save shingara/1885322 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
source "https://rubygems.org" | |
gem 'mechanize' |
This file contains hidden or 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
require 'bundler' | |
Bundler.setup | |
LOGIN_TUMBLR = "[email protected]" | |
PASS_TUMBLR = "password" | |
TUMBLR_BLOG = 'xxx.tumblr.com' | |
CANALBLOG_NAME = "xxx" | |
require 'mechanize' | |
agent = Mechanize.new | |
page = agent.get("http://#{CANALBLOG_NAME}.canalblog.com/archives/index.html") | |
article_link = [] | |
page.links.each do |l| | |
if l.href =~ /\d{4}\/\d{2}/ | |
p_archive = l.click | |
p_archive.links.each do |la| | |
if la.href =~ /\d{4}\/\d{2}\/\d{2}\/\d+\.html$/ | |
article_link << la | |
end | |
end | |
end | |
end | |
require 'net/http' | |
article_link.each do |article| | |
year = article.href[/(\d{4})\/(\d{2})\/(\d{2})\/\d+\.html$/, 1] | |
month = article.href[/(\d{4})\/(\d{2})\/(\d{2})\/\d+\.html$/, 2] | |
day = article.href[/(\d{4})\/(\d{2})\/(\d{2})\/\d+\.html$/, 3] | |
page = agent.get(article.href) | |
title = page.search(".blogbody h2").text.gsub('Rétroliens', '') | |
text = page.search(".blogbody").to_s[/<\/h2>((\W|\w)*)<div class="itemfooter"/, 1] | |
while text.include?('itemfooter') | |
text = text[/((\W|\w)*)<div class="itemfooter"/, 1] | |
end | |
text = text.gsub(/class=".+"/, '') | |
text = text.gsub(/id=".+"/, '') | |
text = text.gsub(/style=".+"/, '') | |
p title | |
p text | |
p "date : #{year}-#{month}-#{day}" | |
options = { | |
:email => LOGIN_TUMBLR, | |
:password => PASS_TUMBLR, | |
:type => 'regular', | |
:date => "#{year}-#{month}-#{day}", | |
:format => 'html', | |
:group => TUMBLR_BLOG, | |
:title => title, | |
:body => text | |
} | |
p Net::HTTP.post_form(URI.parse("http://www.tumblr.com/api/write"), options) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment