Created
June 1, 2013 10:47
-
-
Save tsunekawa/5689978 to your computer and use it in GitHub Desktop.
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
#-*- coding:utf-8 -*- | |
require 'open-uri' | |
require 'json' | |
require 'sru' | |
require 'pry' | |
require 'nokogiri' | |
class Twitter | |
# Twitterのトレンドワードを配列で返す | |
def trends | |
url = 'https://api.twitter.com/1/trends/23424856.json' | |
trends = JSON.parse(open(url).read).first["trends"] | |
raise if trends.nil? | |
words = trends.map{|h| h["name"] }.reject{|s| s=~/#.*/ } | |
words | |
end | |
end | |
module NdlSearch | |
module SRU | |
class Client | |
SRU_AP = 'http://iss.ndl.go.jp/api/sru' | |
def initialize(opts={}) | |
@client = ::SRU::Client.new SRU_AP | |
end | |
#input: query(String) | |
#output: Array of Record instances | |
def search(query, opts={}) | |
defaults = {:version=>1.2, :maximumRecords=>10, :operation=>'searchRetrieve', :onlyBib=>'true', :recordSchema=>'dcndl', :recordPacking=>'xml'} | |
opts = defaults.update opts | |
response = @client.search_retrieve(query, opts) | |
::NdlSearch::Record.load(response) | |
end | |
end | |
end | |
# Data Model about bibliobraphic data of NDL | |
class Record | |
attr_accessor :doc | |
#load response of NDLSearch API with SRU, and generate array of Record instances | |
#input: response(instance of SRU::Response) | |
#output: Array of Record instances | |
def self.load(response) | |
records = Array.new | |
response.doc.elements.each('//record/recordData') do |el| | |
records << self.new(el) | |
end | |
records | |
end | |
#input: source(String) | |
#output: a Record instance | |
def initialize(source, opts={}) | |
@doc = ::Nokogiri::XML.parse(source.to_s) | |
@namespaces = @doc.collect_namespaces | |
end | |
def title | |
@title ||= @doc.xpath('//dc:title/rdf:Description/rdf:value/text()', @namespaces).map(&:text) | |
end | |
def title_transcription | |
@title_transcription ||= @doc.xpath('//dc:title/rdf:Description/dcndl:transcription/text()', @namespaces).map(&:text) | |
end | |
end | |
end | |
class TwNdl | |
def initialize | |
@tw = Twitter.new | |
@ns = NdlSearch::SRU::Client.new | |
@trends = [] | |
end | |
# update trend words of Twitter, and search related resources from NDLSearch | |
def update! | |
@trends = @tw.trends | |
@resources = @ns.search(@trends.map{|t| "anywhere=\"#{t}\"" }.join(' OR ')) | |
@result = {:terms=>@trends, :resources=>@resources} | |
end | |
def result | |
update! if @result.nil? | |
@result | |
end | |
def terms | |
result[:terms] | |
end | |
def resources | |
result[:resources] | |
end | |
def result_s | |
"#{terms.join(',')}\n\n"+resources.map(&:title).join("\n") | |
end | |
end | |
if __FILE__ == $0 then | |
tw_ndl = TwNdl.new | |
puts tw_ndl.result_s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Twitterのトレンドワードに関連した書誌をNDLSearchから引っ張ってくるプログラムです。