Created
April 11, 2018 03:43
-
-
Save nasust/96ea9e21bbffe36e216b614716512e96 to your computer and use it in GitHub Desktop.
はてなブログをtweetから探すスクリプト
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
require "rubygems" | |
require "bundler/setup" | |
require "twitter" | |
require 'net/http' | |
require 'uri' | |
require "json" | |
require 'rss' | |
require "sqlite3" | |
require "time" | |
$subscribes = 10 | |
$sqlitePath = "xxx"; | |
$twClient = Twitter::REST::Client.new do |config| | |
config.consumer_key = "xxxx" | |
config.consumer_secret = "xxxx" | |
config.access_token = "xxxx" | |
config.access_token_secret = "xxxx" | |
end | |
def expand_url(url) | |
begin | |
response = Net::HTTP.get_response(URI.parse(url)) | |
rescue | |
return url | |
end | |
case response | |
when Net::HTTPRedirection | |
expand_url(response['location']) | |
else | |
url | |
end | |
end | |
def get_hatena_blog_info(url) | |
uri = URI.parse("http://blog.hatena.ne.jp/api/init?blog=" + url) | |
req = Net::HTTP::Get.new(uri.request_uri) | |
req['X-Requested-With'] = 'XMLHttpRequest' | |
res = Net::HTTP.start(uri.host, uri.port) do |http| | |
http.request(req) | |
end | |
return JSON.parse(res.body) | |
end | |
$db = SQLite3::Database.new($sqlitePath); | |
result = $db.execute("select count(*) from sqlite_master where type='table' and name='blog';"); | |
if result.first.first == 0 | |
$db.execute("create table blog (id integer primary key , title text , url text unique );"); | |
end | |
blog_result = [] | |
$twClient.search("#はてなブログ", result_type: "recent").take(100).each{ | tweet | | |
if m = tweet.text.match( %r{(https://t.co/[a-zA-Z0-9…]+)}) | |
if m.length >= 2 | |
url = m[1] | |
if url.empty? == false && url.include?("…") == false | |
url = expand_url(url) | |
blogInfo = get_hatena_blog_info(url) | |
if blogInfo["subscribes"].to_i >= $subscribes | |
blog_name = JSON.parse( %Q{["#{blogInfo["blog_name"]}"]} )[0] | |
result = $db.execute( "select count(*) from blog where url = ?;" , blogInfo["blog_url"] ); | |
if result.first.first == 0 | |
blog_result << { | |
"blog_name" => blog_name , | |
"entry_url" => url , | |
"blog_url" => blogInfo["blog_url"] , | |
"subscribes" => blogInfo["subscribes"] , | |
"twurl" => tweet.uri.to_s | |
} | |
sql = "insert into blog (title,url) values (?,?);" | |
$db.execute( sql , blog_name , blogInfo["blog_url"] ); | |
end | |
end | |
end | |
end | |
end | |
} | |
$db.close | |
exit(0) if blog_result.empty? | |
feed = RSS::Maker.make("2.0") do |feed| | |
feed.channel.title = "hatena bloger" | |
feed.channel.link = "https://nasust.hatenablog.com" | |
feed.channel.about = "http://xxxx/feed.xml" | |
feed.channel.description = "twitter hatena bloger" | |
feed.channel.updated = Time.now | |
blog_result.each do | blog | | |
feed.items.new_item do |item| | |
item.title = blog["blog_name"] | |
item.link = blog["blog_url"] | |
item.description = "subscribes:" + blog["subscribes"] + " " + blog["twurl"] | |
end | |
end | |
end | |
File.open("xxx/feed.xml", 'w') { |f| f.puts(feed) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment