Skip to content

Instantly share code, notes, and snippets.

@hyuki0000 hyuki0000/README
Last active Aug 29, 2015

Embed
What would you like to do?
AutoGetter - Twitterで自分のツイートから自分へのリプライをまとめるRubyスクリプト(連ツイまとめ作成用)
## AutoGetter - Twitterで自分のツイートから自分へのリプライをまとめるRubyスクリプト(連ツイまとめ作成用)
https://gist.github.com/hyuki0000/85989bcf78d1476d74d3
1. config.yamlを自分用に作成する。
2. ruby autogetter.rb を実行する。
3. 自分へのリプライ(リプライ連鎖)ごとに 1.html, 2.html, 3.html, ... とファイルにまとめる(既存ファイル上書き)。
4. HTMLが直接書けるサイトに貼り付ける。
require 'yaml'
require 'json'
require 'oauth'
require 'pp'
CONFIG = YAML.load_file('config.yaml')
user_id = CONFIG["user_id"]
consumer_key = CONFIG["consumer_key"]
consumer_secret = CONFIG["consumer_secret"]
access_token = CONFIG["access_token"]
access_token_secret = CONFIG["access_token_secret"]
count = CONFIG["count"]
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)
response = endpoint.request(:get, "https://api.twitter.com/1.1/statuses/user_timeline.json?user_id=#{user_id}&count=#{count}&trim_user=true")
result = JSON.parse(response.body)
table = Hash.new(nil)
result.each do |e|
e["REPLIES"] = []
e["MOVETO"] = nil
table[e["id"]] = e
end
table.each_key do |id|
rid = table[id]["in_reply_to_status_id"]
next if !rid
loop do
if !table[rid]
# Ignore "out of range" tweets.
table[id]["REPLIES"] = []
break
end
mid = table[rid]["MOVETO"]
if !mid
# Current "root" is found.
table[rid]["REPLIES"] << id
table[rid]["REPLIES"] += table[id]["REPLIES"]
table[id]["MOVETO"] = rid
table[id]["REPLIES"] = []
break
end
rid = mid
end
end
counter = 1
table.each_key do |id|
if table[id]["REPLIES"].length > 0
filename = "#{counter}.html"
puts "#{filename} #{table[id]["text"]}"
File::open(filename, "w") do |f|
table[id]["REPLIES"] << id
table[id]["REPLIES"].sort.each do |eid|
"https://api.twitter.com/1.1/statuses/oembed.json?id=#{eid}"
response = endpoint.request(:get, "https://api.twitter.com/1.1/statuses/oembed.json?id=#{eid}")
result = JSON.parse(response.body)
html = result["html"]
html.gsub!(/\<script.*script\>/, "")
html.gsub!(/blockquote/, %Q(blockquote data-conversation="none"))
f.puts html
end
f.puts %Q(<script src="http://platform.twitter.com/widgets.js" charset="utf-8"></script>)
counter += 1
end
end
end
# table.each_key do |id|
# if table[id]["REPLIES"].length > 0
# puts "=" * 40
# puts id
# puts "#{table[id]["text"]}"
# table[id]["REPLIES"].sort.each do |eid|
# puts eid
# puts "#{table[eid]["text"]}"
# end
# end
# end
user_id: 1234567
consumer_key: "XXXXXXXXXXXXXXXXXXXXXXXXX"
consumer_secret: "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"
access_token: "9999999-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
access_token_secret: "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
count: 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.