Skip to content

Instantly share code, notes, and snippets.

@nasust
Last active January 3, 2016 08:48
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 nasust/8438046 to your computer and use it in GitHub Desktop.
Save nasust/8438046 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'rubygems'
require 'oauth'
require 'rexml/document'
require "sqlite3"
require 'net/http'
require 'net/https'
require 'uri'
require 'time'
HATENA_ID = "nasust";
BLOG_ID = "showrtpath.hatenablog.com";
DB_PATH = "blog_entry.db";
API_KEY = "xxxxxxxxx";
PAGE_CAGEGORY_URL = {
"ライフ" => "http://showrtpath.hatenablog.com/entry/life",
"Showrtpath" => "http://showrtpath.hatenablog.com/entry/showrtpath",
"Apple/ガジェット" => "http://showrtpath.hatenablog.com/entry/apple_gadget",
"IOS開発" => "http://showrtpath.hatenablog.com/entry/developp_ios",
"WEB" => "http://showrtpath.hatenablog.com/entry/wev_dev",
"UI/UX" => "http://showrtpath.hatenablog.com/entry/ui_ux",
"ブックマーク" => "http://showrtpath.hatenablog.com/entry/bookmark",
}
PAGE_CAGEGORY_ENTRY_ID = {
"ライフ" => "12921228815716456765",
"Showrtpath" => "12921228815716456900",
"Apple/ガジェット" => "12921228815716456972",
"IOS開発" => "12921228815716457009",
"WEB" => "12921228815716466347",
"UI/UX" => "12921228815716457088",
"ブックマーク" => "12921228815716457122",
}
@page_category = {
"ライフ" => ["ライフ","ガジェット"],
"Showrtpath" => ["Showrtpathブラウザの紹介"],
"Apple/ガジェット" => ["iphone","Mac "],
"IOS開発" => ["iOS開発","iOS開発の記事のまとめ" , "ソースコード"],
"WEB" => ["WEB"],
"UI/UX" => ["UI/UX"],
"ブックマーク" => ["nasustのbookmark"]
}
@page_category_data = {
"ライフ" => [],
"Showrtpath" => [],
"Apple/ガジェット" => [],
"IOS開発" => [],
"WEB" => [],
"UI/UX" => [],
"ブックマーク" => [],
}
@db = SQLite3::Database.new( DB_PATH );
result= @db.execute "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='blog_entry';"
if result[0][0] == 0 ;
sql = <<-SQL
create table blog_entry(
id INTEGER PRIMARY KEY,
title TEXT,
href TEXT UNIQUE,
image_url TEXT,
published TEXT,
tag TEXT
);
SQL
@db.execute sql
end
def get_blog( next_page_url )
http = Net::HTTP.new("blog.hatena.ne.jp",443);
http.use_ssl = true;
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
path = "/#{HATENA_ID}/#{BLOG_ID}/atom/entry";
if( next_page_url )
path = path + "?" + next_page_url;
end
request = Net::HTTP::Get.new(path);
request.basic_auth(HATENA_ID, API_KEY);
response = http.request(request);
if( response.code == "200" )
#puts response.body;
doc = REXML::Document.new(response.body);
next_url = nil;
doc.elements.each("*/link[@rel='next']"){ | element |
next_url = element.attributes["href"];
}
doc.elements.each("*/entry"){ | element |
blog_title = nil;
blog_link = nil;
published = nil;
image_path = nil;
category = [];
#block link
element.elements.each("link[@rel='alternate']"){ | element |
blog_link = element.attributes["href"];
}
skip = false;
PAGE_CAGEGORY_URL.each{ | key , url |
if( url == blog_link )
skip = true;
end
}
if( skip )
next;
end
element.elements.each("title"){ | element |
blog_title = element.text;
}
element.elements.each("published"){ | element |
published = element.text;
}
element.elements.each("category"){ | element |
category << element.attributes["term"];
#p element.attributes["term"];
}
element.elements.each("hatena:formatted-content"){ | element |
begin
html = element.text;
URI.extract( html , ["http","https"] ).each{ | url |
if( url =~ /\.(jpg|gif|png)$/ )
image_path = url;
break;
end
}
#p image_path;
rescue
p $!
end
}
if( image_path == nil ||image_path.empty? )
image_path = "http://cdn-ak.f.st-hatena.com/images/fotolife/n/nasust/20140104/20140104094040.jpg";
end
begin
@db.execute "insert into blog_entry(title,href,image_url,published,tag) values ( ?, ? , ? , ? , ?)" , [ blog_title , blog_link , image_path , published , category.join(",") ]
rescue
next_url = nil;
break;
end
}
if(next_url)
#puts next_url;
uri = URI.parse(next_url);
get_blog(uri.query);
else
puts "generate"
generate_static_page();
end
end
end
def generate_static_page()
@db.execute( "select * from blog_entry" ) do | row |
title = row[1];
href = row[2];
image_url = row[3];
published = row[4];
tags = row[5];
category_key = [];
tags_split = tags.split(",");
tags_split.each{ | tag |
@page_category.each{ | key , match_tags |
match_tags.each{ | match_tag |
if( tag == match_tag )
# puts key + ":" + title + " :" + tag + " : " + match_tag;
category_key << key;
break;
end
}
}
}
category_key.uniq!;
if( category_key.empty? )
category_key << "ライフ"
end
category_key.each{ | key |
@page_category_data[ key ] << {
:title => title,
:href => href,
:image_url => image_url,
:published => published,
:tags => tags,
};
}
#puts "blog_title: #{title} + blog_link : #{href} published:#{published} image_path:#{image_url} + category:#{ tags }\n\n"
end
@page_category_data.each{ | key , entrys |
post_url = PAGE_CAGEGORY_URL[key];
body = [];
entrys = entrys.sort{| a , b |
a_published = a[:published];
b_published = b[:published];
a_time = Time.parse(a_published);
b_time = Time.parse(b_published);
a_time <=> b_time;
}
entrys = entrys.reverse;
entrys.each{ | entry |
title = entry[:title];
href = entry[:href];
image_url = entry[:image_url];
published = entry[:published];
tags = entry[:tags];
html=<<-HTML
<div class="entry_item">
<div class="entry_image">
<a href="#{href}"><img src="#{image_url}"></a>
</div>
<div class="title"><a href="#{href}">#{title}</a></div>
</div>
HTML
body << html;
}
xml = <<-XML
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:app="http://www.w3.org/2007/app">
<title>#{key}</title>
<author><name>#{HATENA_ID}</name></author>
<updated>2013-07-14T00:00:00</updated>
<content type="text/plain">
<![CDATA[
<br>
<div id="entry_container" class="category_columns js-masonry" data-masonry-options='{ "columnWidth": 175, "itemSelector": ".entry_item" }'>
#{body.join("\n")}
</div>
<script type="text/javascript" src="http://showrtpath.com/hatena/masonry.pkgd.min.js"></script>
]]>
</content>
<app:control>
<app:draft>no</app:draft>
</app:control>
</entry>
XML
#puts xml;
entry_id = PAGE_CAGEGORY_ENTRY_ID[key];
http = Net::HTTP.new("blog.hatena.ne.jp",443);
http.use_ssl = true;
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req = Net::HTTP::Put.new( "/#{HATENA_ID}/#{BLOG_ID}/atom/entry/#{entry_id}");
req.basic_auth(HATENA_ID, API_KEY);
req.body = xml;
response = http.request(req);
p response;
}
end
get_blog(nil);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment