· 7 years ago · Apr 09, 2018, 12:42 AM
1# -*- coding: utf-8 -*-
2
3# ~/.termtter/config
4# config.amazon = {
5# :access_key => "your aws access key",
6# :secret_key => "your aws secret key"
7# }
8
9require 'rubygems'
10require 'amazon/ecs'
11require 'readline'
12
13module Termtter
14 class InBook
15 def initialize(args)
16 Amazon::Ecs.options = {
17 :aWS_access_key_id => args[:access_key],
18 :aWS_secret_key => args[:secret_key],
19 :country => :jp
20 }
21 end
22
23 def get_post_text(text)
24 search_and_puts_candidate text
25 num = Readline.readline("select book(0-9)> ").to_i
26 serif = Readline.readline("input serif> ")
27 page = Readline.readline("input page num> ").to_i
28
29 title = @search_result.items[num].get("title")
30 sep = (title.index("<") == nil) ? ["<", ">"] : ["(", ")"]
31
32 "@inbook_jp #{sep[0]}#{title},#{page}#{sep[1]} #{serif}"
33 end
34
35 private
36 def search_and_puts_candidate(text)
37 @search_result = Amazon::Ecs.item_search(text, {
38 :search_index => "Books",
39 :response_group => "Medium",
40 :sort => "salesrank"
41 })
42 for i in 0..(@search_result.items.length - 1)
43 puts " #{i}: #{@search_result.items[i].get('title')}"
44 end
45 end
46 end
47
48 module Client
49 register_command(
50 :name => :inbook,
51 :exec_proc => lambda do |arg|
52 ib = Termtter::InBook.new(config.amazon)
53
54 text = ib.get_post_text arg
55 Termtter::API.twitter.update text
56 end,
57 :help => ["inbook TITLE", "post serif to InBook"]
58 )
59 end
60end