Skip to content

Instantly share code, notes, and snippets.

@5t111111
Last active August 29, 2015 13:56
Show Gist options
  • Save 5t111111/8894473 to your computer and use it in GitHub Desktop.
Save 5t111111/8894473 to your computer and use it in GitHub Desktop.
My own twieve.
#!/usr/bin/env ruby
require 'twitter'
require "evernote_oauth"
require 'yaml'
require 'cgi'
YOUR_TWITTER_CONSUMER_KEY = 'Enter your API key'
YOUR_TWITTER_CONSUMER_SECRET = 'Enter yout API secret'
YOUR_TWITTER_ACCESS_TOKEN = 'Enter your Access token'
YOUR_TWITTER_ACCESS_TOKEN_SECRET = 'Enter your Access token secret'
YOUR_EVENOTE_DEVELOPER_KEY = 'Enter your Developer key'
class Twieve
attr_accessor :config
attr_reader :twitter_user
def initialize
@config = {}
end
def authorize
# Twitterクライアントを設定
begin
@twitter_client = Twitter::REST::Client.new do |config|
config.consumer_key = @config[:twitter_consumer_key]
config.consumer_secret = @config[:twitter_consumer_secret]
config.access_token = @config[:twitter_access_token]
config.access_token_secret = @config[:twitter_access_token_secret]
end
@twitter_user = @twitter_client.user
rescue => e
puts "ERROR: Cannot setup Twitter client. Please make sure you have entered valid access credentials. [#{e}]"
return false
end
# Evernoteクライアントを設定
begin
# Developer Tokenをセット
token = @config[:evernote_developer_token]
client = EvernoteOAuth::Client.new(:token => token, :sandbox => false)
@evernote_note_store = client.note_store
rescue => e
puts "ERROR: Cannot setup Evernote client. Please make sure you have entered valid access credentials. [#{e}]"
return false
end
end
def get_tweets_match_pattern(user_screen_name, pattern, since_id)
# ユーザーのタイムラインからパターンにマッチするツイートのみ抽出
begin
regexp = Regexp.new(pattern, Regexp::IGNORECASE)
tweets = @twitter_client.user_timeline(user_screen_name,
:since_id=>since_id,
:count=>300).select do |tweet|
if pattern == "all tweets"
tweet.text
else
tweet.text =~ regexp
end
end
rescue => e
puts "ERROR: Cannot fetch timeline data of #{user_screen_name}. [#{e}]"
return false
end
end
def get_notebook_guid(notebook_name)
begin
notebooks = @evernote_note_store.listNotebooks.select do |notebook|
notebook.name == notebook_name
end
rescue => e
puts "ERROR: Cannot fetch notebooks. [#{e}]"
return false
end
# ノートブックが無かったら作る
if notebooks.empty?
begin
notebook = Evernote::EDAM::Type::Notebook.new
notebook.name = notebook_name
@evernote_note_store.createNotebook(@config[:evernote_developer_token],
notebook).guid
rescue => e
puts "ERROR: Cannot create notebook. [#{e}]"
return false
end
else
notebooks.first.guid
end
end
def create_evernote_note(notebook_name, note_title, note_content)
notebook_guid = get_notebook_guid(notebook_name)
return false unless notebook_guid
# ノートのタイトル
note_title = note_title
# ノートのENMLヘッダ
enml_header = <<-HEADER
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
HEADER
# ノートの本文
note_content = <<-CONTENT
#{enml_header}
<en-note>#{CGI.escapeHTML(note_content)}</en-note>
CONTENT
note = Evernote::EDAM::Type::Note.new(:title => note_title,
:notebookGuid => notebook_guid,
:content => note_content)
# ノートの作成
begin
@evernote_note_store.createNote(@config[:evernote_developer_token], note)
rescue => e
puts "ERROR: Cannot create a note. [#{e}]"
return false
end
end
end
target = ARGV[0] || "all tweets"
target = target.downcase
twieve = Twieve.new
twieve.config[:twitter_consumer_key] = YOUR_TWITTER_CONSUMER_KEY
twieve.config[:twitter_consumer_secret] = YOUR_TWITTER_CONSUMER_SECRET
twieve.config[:twitter_access_token] = YOUR_TWITTER_ACCESS_TOKEN
twieve.config[:twitter_access_token_secret] = YOUR_TWITTER_ACCESS_TOKEN_SECRET
twieve.config[:evernote_developer_token] = YOUR_EVENOTE_DEVELOPER_KEY
unless twieve.authorize
puts 'ERROR: Failed to authorize.'
exit 1
end
history_file = Pathname.new(File.expand_path(File.dirname($PROGRAM_NAME))) + 'history.yml'
if File.exist?(history_file)
yaml_data = YAML.load_file(history_file)
end
if yaml_data && yaml_data.has_key?(target)
since_id = yaml_data[target]
else
since_id = 1
yaml_data = {}
end
tweets = twieve.get_tweets_match_pattern(twieve.twitter_user.id,
target,
since_id)
unless tweets
puts 'ERROR: Failed to get Twitter timeline.'
exit 1
end
unless tweets.empty?
tweets.reverse_each do |tweet|
#puts "DEBUG: #{tweet.text}... (ID: #{tweet.id})"
unless twieve.create_evernote_note("#{twieve.twitter_user.name}'s Tweets (#{target})",
"#{tweet.created_at} (ID: #{tweet.id})",
tweet.full_text)
puts 'ERROR: Failed to create Evernote note.'
exit 1
end
puts "INFO: #{tweet.text[0, 10]}... (ID: #{tweet.id}) has been successfully created."
end
# 取得したIDの一番新しいものを保存
yaml_data[target] = tweets.first.id
open(history_file, "w") do |file|
file.write(YAML.dump(yaml_data))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment